在MySQL 8.0.11中,caching_sha2_password是默认的身份验证插件,而不是以往的mysql_native_password。有关此更改对服务器操作的影响以及服务器与客户端和连接器的兼容性的信息,请参阅caching_sha2_password作为首选的身份验证插件。(翻译自https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html)
今天在新服务上配置安装mysql8.0.11时,像往常一样设置mysql密码,设置成功后在shell下输入mysql -u root -p,再输入密码能正常进入,但在phpmyadmin或直接用http://php.net/manual/zh/mysqli.real-connect.php上的连接,均提示无法连接,具体报错信息为
mysqli_real_connect(): The server requested authentication method unknown to the client [sha256_password]
搜了一圈,找到官方文档才发现从8.0.11版本起,不再像mysql5.7及以前版本那样,设置用户密码时默认的验证方式为caching_sha2_password,如果发现升级mysql8.0.11后原有的程序不能连接mysql,可迅速用下面的命令设置成mysql5.7及以前版本的密码验证方式,同时MYSQL8.0.11下修改密码的方式与原先也不大一样,原先的部分修改密码的命令在mysql8.0.11下不能使用。
> use mysql
> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
> FLUSH PRIVILEGES;
另附上my.cnf配置的文件内容
[mysqld]
datadir=/mnt/data//mysql
socket=/mnt/local/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
port = 3309
max_connections=5000
wait_timeout=70
interactive_timeout=70
slow_query_log = 1
long-query-time = 1.0
slow_query_log_file = /mnt/logs/mysql/slow.log
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
default-authentication-plugin=mysql_native_password
[mysql]
default-character-set = utf8mb4
[client]
port=3309
max_allowed_packet=500M
[mysqld_safe]
log-error=/mnt/logs/mysql/error.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d