php - 如何在WordPress中建立MySQL数据库连接?

标签 php mysql wordpress

我正在尝试在 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 的 Shell 输出:

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)

最佳答案

您应该能够通过执行以下操作来修复:

创建新用户:

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

(将'密码')更改为您的密码

授予此用户对 wordpress 数据库的权限:

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

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

define('DB_HOST', '127.0.1.1');

如果用户没有使用它的授权,则不能将 127.0.0.1 用作数据库主机

关于php - 如何在WordPress中建立MySQL数据库连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44052021/

相关文章:

php - 显示sql增加时间

php - Wordpress,如果循环没有结果,则不显示标题

php - wordpress 页面的大问题,不会在 IE7 中滚动

php - 显示选项卡而不刷新整个页面

php - 如何防止 PHP 中的 SQL 注入(inject)?

php - 在 PHP 中验证后数据未插入数据库

php - 在 openerp 上创建 sale.order 时访问被拒绝

php - 限制 SQL Select 查询

php - 这是使用 PHP 更新/插入记录的正确方法吗?

java - 如何让用户通过 Stripe 而不是我的 Wordpress 网站进行付款?