wordpress MySQL 8.0 报错:Error establishing a database connection

更新日期: 2020-11-16 阅读次数: 3420 字数: 406 分类: MySQL

迁移了旧有的一套 wordpress 网站到新的服务器上,更改域名解析之后,报错:

Error establishing a database connection

系统环境

  • Ubuntu 20.04
  • PHP 7.0
  • MySQL 8.0

问题排查

看上去是很简单的账号,密码问题。我确认了半天,发现 mysql 可以正常登陆,wp-config.php 中的也没有写错。

但是依旧报这个错误。感觉没有头绪了。

wordpress debug 开关打开

define( 'WP_DEBUG', true );

发现详细错误如下:

Warning: mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in wordpress/wp-includes/wp-db.php on line 1633

Warning: mysqli_real_connect(): (HY000/2054): The server requested authentication method unknown to the client in wordpress/wp-includes/wp-db.php on line 1633
The server requested authentication method unknown to the client

Error establishing a database connection

This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at localhost. This could mean your host’s database server is down.

Are you sure you have the correct username and password?
Are you sure you have typed the correct hostname?
Are you sure the database server is running?
If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

解决方法

利用关键词在 stackoverflow 上查到了解决方法:

ALTER USER your_user_name@localhost IDENTIFIED WITH mysql_native_password BY 'password';

但是仅仅修改这个还不够,还需要修改 mysql 的配置文件 my.cnf,

/etc/mysql/my.cnf

在最后加上配置

[mysqld]
default_authentication_plugin= mysql_native_password

重启 mysql 服务

service mysql restart

再次访问网站,就一切正常了。

教训:部分服务升级带来的问题

由于这次升级只升级了 mysql,而没有升级 php。所以遇到了兼容性问题。

而最新的 php 7.4 就很好兼容了 mysql 8.0 的这个特性。

tags: ubuntu 20.04

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式