centos7 mysql数据库安装和配置

Posted ╰☆水の若蓝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7 mysql数据库安装和配置相关的知识,希望对你有一定的参考价值。

 

一、系统环境

 

yum update升级以后的系统版本为

[root@mysql ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

 

 二、mysql安装

 

 

一般网上给出的资料都是

#yum install mysql
#yum install mysql-server
#yum install mysql-devel

安装mysql和mysql-devel都成功,但是安装mysql-server失败,如下:

[root@mysql ~]# yum install mysql-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
No package mysql-server available.
Error: Nothing to do

 

 

  

 

 

查资料发现是CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了。

 

有两种解决办法:

1、方法一:安装mariadb

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

安装mariadb,大小59 M。

[root@mysql ~]# yum install mariadb-server mariadb 

 

mariadb数据库的相关命令是:

启动MariaDB:
[root@mysql ~]# systemctl start mariadb
停止MariaDB:
[root@mysql ~]# systemctl stop mariadb
重启MariaDB:
[root@mysql ~]# systemctl restart mariadb
设置开机启动:
[root@mysql ~]# systemctl enable mariadb
取消开机启动:
[root@mysql ~]# systemctl disable mariadb

 

 

 

 

 

 

 

 

 

 

 

 

 

所以先启动数据库

[root@mysql ~]# systemctl start mariadb

然后就可以正常使用mysql了  

[root@mysql ~]# systemctl start mariadb

[root@mysql ~]# mysql -u root -p
Enter password:    (首次登录密码为空
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------------+
| Database                    |
+--------------------------+
| information_schema   |
| mysql                   |
| performance_schema |
| test                            |
+--------------------------+
4 rows in set (0.00 sec)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

设置密码

MariaDB [(none)]> set password for \'root\'@\'localhost\' =password(\'redhat\');
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>

 

 

 

 

 

安装mariadb后显示的也是 MariaDB [(none)]> ,可能看起来有点不习惯。下面是第二种方法。

 

2、方法二:官网下载安装mysql-server

[root@mysql ~]# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

[root@mysql ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-release-el7-5 ################################# [100%]

 

[root@mysql ~]# ll /etc/yum.repos.d/
total 48
-rw-r--r-- 1 root root 1664 Sep 5 09:05 CentOS-Base.repo
-rw-r--r-- 1 root root 1309 Sep 5 09:05 CentOS-CR.repo
-rw-r--r-- 1 root root 649 Sep 5 09:05 CentOS-Debuginfo.repo
-rw-r--r-- 1 root root 314 Sep 5 09:05 CentOS-fasttrack.repo
-rw-r--r-- 1 root root 630 Sep 5 09:05 CentOS-Media.repo
-rw-r--r-- 1 root root 1331 Sep 5 09:05 CentOS-Sources.repo
-rw-r--r-- 1 root root 6639 Sep 5 09:05 CentOS-Vault.repo
-rw-r--r-- 1 root root 951 Oct 2 2017 epel.repo
-rw-r--r-- 1 root root 1050 Oct 2 2017 epel-testing.repo
-rw-r--r-- 1 root root 1209 Jan 29 2014 mysql-community.repo
-rw-r--r-- 1 root root 1060 Jan 29 2014 mysql-community-source.repo

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 [root@mysql ~]# yum install mysql-community-server

 安装成功后重启mysql服务。

 [root@mysql ~]# service mysqld restart

 

初次安装mysql,root账户没有密码。

[root@mysql ~]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \\g.
Your MySQL connection id is 2
Server version: 5.6.45 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.

mysql> show databases;
+--------------------------+
| Database                    |
+--------------------------+
| information_schema   |
| mysql                         |
| performance_schema |
+--------------------------+
3 rows in set (0.00 sec)

mysql>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

设置密码

mysql> set password for \'root\'@\'localhost\' =password(\'redhat\');
Query OK, 0 rows affected (0.00 sec)

mysql>

 

 

 

 

 

不需要重启数据库即可生效。

在mysql安装过程中如下内容:

 

 

 

所以安装完以后mariadb自动就被替换了,将不再生效。

[root@mysql ~]# rpm -qa |grep mariadb
[root@mysql ~]#

 

 

 

 

三、配置mysql

 

1、编码

mysql配置文件为/etc/my.cnf

最后加上编码配置

[root@mysql ~]# vim /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

symbolic-links=0

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

[mysql]
default-character-set =utf8

!includedir /etc/my.cnf.d

 

 

 

 

 

 

 

 

 

 

 

 

 

这里的字符编码必须和/usr/share/mysql/charsets/Index.xml中一致。

[root@mysql ~]# vim /usr/share/mysql/charsets/Index.xml

 

 

 

2、远程连接设置

把在所有数据库的所有表的所有权限赋值给位于所有IP地址的root用户。

[root@mysql ~]# mysql -uroot -p
Enter password:     (密码是:redhat)
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 6
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.

MariaDB [(none)]> grant all privileges on *.* to root@\'%\'identified by \'redhat\';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]>

 

 

 

 

 

 

 

 

 

 

 

如果是新用户而不是root,则要先新建用户

MariaDB [(none)]> create user \'admin\'@\'%\' identified by \'Xuanbao123456\';
Query OK, 0 rows affected (0.00 sec)

 

 

 

 

此时就可以进行远程连接了。

[root@c1 ~]# mysql -uadmin -p -h192.168.122.20
Enter password:     (密码是:Xuanbao123456)
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 7
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.

MariaDB [(none)]>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

新增用户并授权

 

新建mpm_config用户:

MariaDB [(none)]> create user \'mpm_config\'@\'%\' identified by \'8vVA2DhIC31yfDKW\';
Query OK, 0 rows affected (0.00 sec)

 

创建mpm_config库:

MariaDB [(none)]> CREATE DATABASE mpm_config /*!40100 DEFAULT CHARACTER SET utf8 */;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mpm_config |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

 

给用户授权对应库:

MariaDB [(none)]> grant update,insert,delete,select on mpm_config.* to mpm_config;
Query OK, 0 rows affected (0.03 sec)

以上是关于centos7 mysql数据库安装和配置的主要内容,如果未能解决你的问题,请参考以下文章

centos7 mysql数据库安装和配置

centos7 mysql数据库安装和配置

Centos7源码安装mysql

centos7 mysql数据库安装和配置

基于centos7搭建MySQL数据库

centos7 mysql数据库安装和配置