1、下载 MySQL官方的 yum Repository
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
2、安装 yum 源
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
3、开始安装MySQL服务器
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# yum -y install mysql-community-server
如果出现如下错误(centos7.8以上版本):
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# yum -y install mysql-community-server
MySQL Connectors Community 124 kB/s | 79 kB 00:00
MySQL Tools Community 482 kB/s | 476 kB 00:00
MySQL 5.7 Community Server 1.5 MB/s | 1.9 MB 00:01
All matches were filtered out by modular filtering for argument: mysql-community-server
Error: Unable to find a match: mysql-community-server
解决:
先执行:yum module disable mysql
再执行:yum -y install mysql-community-server
4、配置mysql数据库
启动 mysql 服务
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# systemctl start mysqld
查看初始密码
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# grep "password" /var/log/mysqld.log
登录进入数据库,使用上边的密码
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# mysql -uroot -p
如果无法登录,如下报错:
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
解决方案:
在 /etc/my.cnf 的 [mysqld] 的最后一行加上“skip-grant-tables”用来跳过密码验证的过程
如下:
//重启mysql
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# service mysqld restart
直接enter进入mysql
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# mysql -uroot -p
Enter password:
mysql> UPDATE mysql.user SET authentication_string=PASSWORD('root') where USER='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
执行上边的报错则执行UPDATE mysql.user SET Password=PASSWORD(‘root’) where USER=‘root’;试一下
更改密码校验强度
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
设置新密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
配置root账号可远程连接
mysql> use mysql;
mysql> select host,user FROM user WHERE user='root';
mysql> update user set host= '%' WHERE user ='root';
mysql> flush privileges;
修改/etc/my.cnf,在[mysqld]下添加如下
#表名不区分大小写(linux默认区分大小写)
lower_case_table_names=0
注:安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉
[root@iZbp19ftqv2b7y3oba2i6tZ ~]# yum -y remove mysql57-community-release-el7-10.noarch