【问题标题】:How to establish a MySQL database connection in WordPress?如何在 WordPress 中建立 MySQL 数据库连接?
【发布时间】:2017-05-18 15:31:21
【问题描述】:

我正在尝试按照this 教程在 Ubuntu 14.04 上设置 WordPress。编辑wp-config.php,遇到"Error establishing a database connection"错误。

define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'wordpress');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

define('WP_ALLOW_REPAIR', true);

DB_NAMEDB_USERDB_PASSWORD 的值是正确的。我不确定DB_HOST;我试过了:

define('DB_HOST', 'localhost:3306');
define('DB_HOST', '127.0.1.1');
define('DB_HOST', '127.0.1.1:3306');
define('DB_HOST', '159.203.70.104');
define('DB_HOST', '159.203.70.104:3306');

但没有人解决问题。 MySQL 和 Apache 日志没有显示任何关于该错误的信息。 MySQL shell 输出:

mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6291
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| wordpress          |
+--------------------+
4 rows in set (0.00 sec)

mysql> select User,Host from mysql.user;
+------------------+-----------------------------------+
| User             | Host                              |
+------------------+-----------------------------------+
| root             | 127.0.0.1                         |
| root             | ::1                               |
| debian-sys-maint | localhost                         |
| root             | localhost                         |
| wordpressuser    | localhost                         |
| root             | npvr-vivek-2016-10-17-4gb-nyc3-01 |
+------------------+-----------------------------------+
6 rows in set (0.00 sec)

重新启动 MySQL 没有帮助。 wordpressuser 的外壳输出:

mysql -u wordpressuser -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6646
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| wordpress          |
+--------------------+
2 rows in set (0.00 sec)

【问题讨论】:

  • wordpressuser 只能连接到本地主机,因此如果您尝试使用 127.0.0.1 它将失败。尝试添加 127.0.0.1 作为用户的主机并使用配置文件中的 ip
  • 你看到你有两个root用户条目吗?一个以 localhost 作为主机,一个以 127.0.0.1 作为主机。由于这是两种完全不同的连接方法,因此您的 wp 用户还需要两个条目。添加新条目,然后您应该能够在 wp 的配置文件中使用 127.0.0.1
  • 我告诉你添加一个 user= wordpressuser 和 host=127.0.0.1 的条目
  • 这样做后编辑配置文件并将 DB_HOST 更改为 127.0.0.1

标签: php mysql wordpress


【解决方案1】:

您应该可以通过以下方式修复:

创建一个新用户:

CREATE USER 'wordpressuser'@'127.0.0.1' IDENTIFIED BY 'password';

(更改“密码”)为您的密码

在 wordpress db 上授予此用户权限:

GRANT ALL PRIVILEGES ON wordpress . * TO 'newuser'@'127.0.0.1';

最后通过编辑配置文件使用 127.0.0.1 作为 db 主机:

define('DB_HOST', '127.0.1.1');

如果用户没有使用权限,则不能使用 127.0.0.1 作为数据库主机

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2019-05-27
    • 2014-01-24
    • 2021-05-28
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多