Centos搭建持续集成(四)----安装Mysql数据库

115 阅读4分钟
原文链接: blog.csdn.net

在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。


一、下载Mysql


使用下面的命令就直接下载了安装用的Yum Repository,大概25KB的样子

[html] view plain copy print?
  1. [root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm  
[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

二、安装Mysql


然后就可以直接yum安装了。

[html] view plain copy print?
  1. [root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm  
[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm


之后就开始安装MySQL服务器。

[html] view plain copy print?
  1. [root@localhost ~]# yum -y install mysql-community-server  
[root@localhost ~]# yum -y install mysql-community-server


安装完成后就会覆盖掉之前的mariadb。

三、配置Mysql


首先启动MySQL
[html] view plain copy print?
  1. [root@localhost ~]# systemctl start  mysqld.service  
[root@localhost ~]# systemctl start  mysqld.service

查看MySQL运行状态
[html] view plain copy print?
  1. [root@localhost bin]# systemctl status mysqld.service  
[root@localhost bin]# systemctl status mysqld.service




设置开机启动
[html] view plain copy print?
  1. systemctl enable mysqld  
  2. systemctl daemon-reload  
systemctl enable mysqld
systemctl daemon-reload

查看密码: 
[html] view plain copy print?
  1. [root@localhost ~]# cat /var/log/mysqld.log |grep password  
[root@localhost ~]# cat /var/log/mysqld.log |grep password




执行以下命令进入数据库: [html] view plain copy print?
  1. [root@localhost ~]# mysql -u root -p  
[root@localhost ~]# mysql -u root -p


然后执行一些语句: [html] view plain copy print ?
  1. mysql> show databases;  
  2. ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.  
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

报错,报错的原因就是必须重新设置密码才能执行语句,修改密码步骤如下:

1、修改/etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1
[html] view plain copy print?
  1. vi /etc/my.cnf  
vi /etc/my.cnf


这一行配置让 mysqld 启动时不对密码进行验证

2、重启mysqld 服务:

[html] view plain copy print?
  1. systemctl restart mysqld  
systemctl restart mysqld

3、使用 root 用户登录到 mysql:
[html] view plain copy print?
  1. mysql -u root -p  
mysql -u root -p

4、切换到mysql数据库,更新 user 表:

[html] view plain copy print?
  1. UPDATE mysql.user SET authentication_string=password('root') WHERE  User='root'  AND Host='localhost';  
UPDATE mysql.user SET authentication_string=password('root') WHERE User='root'  AND Host='localhost';



在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string

5、退出 mysql,编辑 /etc/my.cnf 文件,删除 skip-grant-tables=1的内容

6、重启mysqld 服务,再用新密码登录即可

发现执行SQL语句还会报错: [html] view plain copy print?
  1. mysql> show databases;  
  2. ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.  
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

然后直接执行SQL修改密码: [html] view plain copy print?
  1. set password=password("root");  
set password=password("root");



又出现错误了、这种错误是与validate_password_policy的值有关。



如果不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。
必须修改两个全局参数:

首先,修改validate_password_policy参数的值,设置安全级别为0 [html] view plain copy print ?
  1. mysql> set global validate_password_policy=0;  
  2. Query OK, 0 rows affected (0.00 sec)  
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

这样,判断密码的标准就基于密码的长度了。这个由validate_password_length参数来决定。默认密码长度为8,可以设置为其它值,最小4位
[html] view plain copy print?
  1. mysql> set global validate_password_length=4;  
  2. Query OK, 0 rows affected (0.00 sec)  
mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)

修改好之后,再次执行修改密码SQL [html] view plain copy print?
  1. mysql> set password=password('123456');  
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)  
mysql> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

然后执行SQL测试,输出如下: [html] view plain copy print?
  1. mysql> set password=password('123456');  
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)  
  3.   
  4. mysql> show databases;  
  5. +--------------------+  
  6. | Database           |  
  7. +--------------------+  
  8. | information_schema |  
  9. | mysql              |  
  10. | performance_schema |  
  11. | sys                |  
  12. +--------------------+  
  13. 4 rows in set (0.00 sec)  
mysql> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

最后配置下远程连接 [html] view plain copy print?
  1. mysql> grant all privileges  on *.* to root@'%' identified by "root" WITH GRANT OPTION;  
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)  
  3.   
  4. mysql> FLUSH PRIVILEGES;  
  5. Query OK, 0 rows affected (0.00 sec)  
mysql> grant all privileges  on *.* to root@'%' identified by "root" WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)