在CentOS中默认安装有MariaDB,这个是MysqL的分支,但为了需要,还是要在系统中安装MysqL,而且安装完成之后可以直接覆盖掉MariaDB。
一、下载MysqL
使用下面的命令就直接下载了安装用的Yum Repository,大概25KB的样子
[root@localhost ~]# wget -i -c http://dev.MysqL.com/get/MysqL57-community-release-el7-10.noarch.rpm
二、安装MysqL
然后就可以直接yum安装了。
[root@localhost ~]# yum -y install MysqL57-community-release-el7-10.noarch.rpm
之后就开始安装MysqL服务器。
[root@localhost ~]# yum -y install MysqL-community-server
安装完成后就会覆盖掉之前的mariadb。
三、配置MysqL
首先启动MysqL
[root@localhost ~]# systemctl start MysqLd.service
查看MysqL运行状态
[root@localhost bin]# systemctl status MysqLd.service
设置开机启动
systemctl enable MysqLd systemctl daemon-reload
查看密码:
[root@localhost ~]# cat /var/log/MysqLd.log |grep password
执行以下命令进入数据库:
[root@localhost ~]# MysqL -u root -p
然后执行一些语句:
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
vi /etc/my.cnf
这一行配置让 MysqLd 启动时不对密码进行验证
2、重启MysqLd 服务:
systemctl restart MysqLd
3、使用 root 用户登录到 MysqL:
MysqL -u root -p
4、切换到MysqL数据库,更新 user 表:
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 服务,再用新密码登录即可
6、重启MysqLd 服务,再用新密码登录即可
发现执行sql语句还会报错:
MysqL> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
然后直接执行sql修改密码:
set password=password("root");
又出现错误了、这种错误是与validate_password_policy的值有关。
如果不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。
必须修改两个全局参数:
首先,修改validate_password_policy参数的值,设置安全级别为0
MysqL> set global validate_password_policy=0; Query OK,0 rows affected (0.00 sec)
这样,判断密码的标准就基于密码的长度了。这个由validate_password_length参数来决定。默认密码长度为8,可以设置为其它值,最小4位
MysqL> set global validate_password_length=4; Query OK,0 rows affected (0.00 sec)
MysqL> set password=password('123456');
Query OK,0 rows affected,1 warning (0.00 sec)
然后执行sql测试,输出如下:
MysqL> set password=password('123456');
Query OK,1 warning (0.00 sec)
MysqL> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| MysqL |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
最后配置下远程连接
MysqL> grant all privileges on *.* to root@'%' identified by "root" WITH GRANT OPTION; Query OK,1 warning (0.00 sec) MysqL> FLUSH PRIVILEGES; Query OK,0 rows affected (0.00 sec)