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/

相关文章:

sql - MySQL bool 全文搜索中的 "Show all except"

php - 如何在带有 PHP 字符串的 Flex 中使用换行符

javascript - 在高度不足100%的高度下拍照

php - 是否可以让 wordpress 用户知道他们已经阅读了哪些帖子?

javascript - 为登录表单设置 cookie

php - 用PHP在MySQL数据库中插入数据

php - 更快的mysql查询

php - 即使内部连接不匹配如何返回一些东西

php - 如果 css 中的某个位置无法移动元素怎么办?

Windows 托管错误上的 php 联系表单