1.加入mysql8官方源
yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm yum clean all yum makecache
2.安装
yum install mysql-community-server
3.启动
systemctl start mysqld.service
4.获取密码登录
[root@localhost mysql]# grep "A temporary password" /var/log/mysqld.log 2020-04-17T16:08:17.317445Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost:cRh/8dVezqBy
5.登录,首次登录需要强制修改密码
[root@localhost mysql]# mysql -uroot -pcRh/8dVezqBy mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 10 Server version: 8.0.19 Copyright (c) 2000, 2020, 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> use mysql; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> ALTER user 'root'@'localhost' IDENTIFIED BY 'root'; Query OK, 0 rows affected (0.01 sec) mysql>
6.添加远程登录用户
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select host,user,authentication_string,plugin from user; +-----------+------------------+------------------------------------------------------------------------+-----------------------+ | host | user | authentication_string | plugin | +-----------+------------------+------------------------------------------------------------------------+-----------------------+ | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | | localhost | root | $A$005$g!1g[XW+A^#{<<xECB5ys.ANfnfz2rlgkuEGUHf.cS21sYsPO5Glj4FES/ | caching_sha2_password | +-----------+------------------+------------------------------------------------------------------------+-----------------------+ 4 rows in set (0.00 sec) 添加用户 mysql> create user sxmd@'%' identified by 'Sxmd.2020'; Query OK, 0 rows affected (0.01 sec) 授权 mysql> grant all privileges on *.* to sxmd@'%' with grant option; Query OK, 0 rows affected (0.00 sec) 刷新 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
因为mysql8使用的是caching_sha2_password加密规则,正常的远程连接会出错:Unable to load authentication plugin 'caching_sha2_password'.最简单的方法是修改远程连接用户的加密规则:
mysql> ALTER USER 'sxmd'@'%' IDENTIFIED WITH mysql_native_password BY 'Sxmd.2020';
Query OK, 0 rows affected (0.01 sec)
7.补充问题
7.1假如获取密码登录失败,目录中/var/log/mysqld.log文件丢失怎么办?可以删除/var/lib/mysql目录 重新初始化
[root@localhost mysql]# rm -rf /var/lib/mysql [root@localhost mysql]# mysqld --initialize --console 2020-04-17T04:02:52.195472Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.19) initializing of server in progress as process 25506 2020-04-17T04:02:56.304216Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: cRh/8dVezqBy
7.2ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 一般是权限问题,修改目录权限,需要重启服务
[root@localhost mysql]# chown -R mysql:mysql /var/lib/mysql [root@localhost mysql]# mysql -uroot -p Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) [root@localhost mysql]# systemctl restart mysqld.service