为什么MySQL CLI可以连接,但WordPress却无法连接?

10

我有一个全新的CentOS Wordpress (php-fpm/nginx)服务器,还有一个由vanilla mysql-server镜像构建的docker容器。

MySQL配置

MySQL运行在端口3306上,并且所有MySQL用户都在MySQL中使用主机172.17.0.1注册(例如:root@172.17.0.1),这是docker的网关IP。Docker容器使用Ansible Playbook安装。所有配置设置都是参数化的,并且在适当的地方使用这些参数,包括Wordpress配置文件和环境变量,在MySQL docker安装期间填充类似于根密码之类的内容。这里是我配置相关数据库的代码:

WordPress配置:

这分为两部分,其中我将包括相关部分。在我的Ansible Playbook中,此代码成功设置了WordPress表和用户:

- name:             Create WordPress database
  mysql_db:
    name:           '{{ wp_db_name }}'
    state:          present
    login_user:     root
    login_password: '{{ mysql_root_password }}'
    login_host:     '{{ docker_mysql_ip }}'

- name:             Create WordPress database user
  mysql_user:
    name:           '{{ wp_db_user }}'
    password:       '{{ wp_db_password }}'
    priv:           '{{ wp_db_name }}.*:ALL'
    state:          present
    login_user:     root
    host:           '{{ docker_gateway_ip }}'
    login_password: '{{ mysql_root_password }}'
    login_host:     '{{ docker_mysql_ip }}'

并且相关的wp-config.php:

define('DB_NAME', 'wordpress');
define('DB_USER', '{{ wp_db_user }}');
define('DB_PASSWORD', '{{ wp_db_password }}');
define('DB_HOST', '{{ docker_mysql_ip }}');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

当我导航到wp-admin时,具体问题如下:"Warning: mysql_connect(): Permission denied in /srv/wordpress/wp-includes/wp-db.php on line 1473",标题为“Error establishing a database connection”,状态码500。

令人困惑的部分

查看 wp-config.php 文件后发现,用户名、密码和数据库名称都与应该的完全相同。将 wp-config.php 中的主机、用户和密码复制/粘贴到命令行的相应位置(例如:mysql -u wordpress -p -h 172.17.0.2),我可以连接,并查看 Wordpress 数据库。

总结

当使用命令行时,MySQL 可以正常连接,但 Wordpress 却无法连接。由于我对 Wordpress 或 PHP 不是很熟悉,因此任何有关问题相关日志可能位于何处的想法都将不胜感激。


这里不应该指向本地主机:define('DB_HOST', '127.0.0.1:{{ mysql_port }}'); - Robert
抱歉,我分3次写了这个程序,忘记更新wp-config.php文件了。已经修改过了。 - Eric Miller
CentOS主机是否正在运行SELinux?默认情况下,可能不允许建立到MySQL的TCP连接。请检查getenforce命令。 - Michael Berkowski
请参见 https://dev59.com/dmsz5IYBdhLWcg3wDjto#8139560。 - Michael Berkowski
你是否将Docker容器的3306端口暴露给外部(无论是在Dockerfile中还是在运行时作为标志)? - Jos
2个回答

1

我在Debian 9.1上使用MariaDB遇到了这个问题,但在Oracle MySQL中应该是一样的。我可以轻松地从命令行登录,但PHP无法访问数据库。

花了我几分钟才想出来,但我记得以前在旧服务器上使用Arch Linux时(顺便说一句,那是个愚蠢的想法),需要使用mysql_secure_installation命令才能使其接受连接。

试试吧,这对我有用。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

0

MySQL 容器 中,使用 MySQL 原生密码进行身份验证,使用以下环境:

MYSQL_DEFAULT_AUTH=mysql_native_password

然后重新启动或重建MySQL容器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接