Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在WordPress中建立MySQL数据库连接?_Php_Mysql_Wordpress - Fatal编程技术网

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

Php 如何在WordPress中建立MySQL数据库连接?,php,mysql,wordpress,Php,Mysql,Wordpress,下面的教程中,我正在尝试在Ubuntu 14.04上安装WordPress。编辑wp config.php,我遇到了“建立数据库连接时出错”错误 define('DB_NAME', 'wordpress'); /** MySQL database username */ define('DB_USER', 'wordpressuser'); /** MySQL database password */ define('DB_PASSWORD', 'wordpress'); /** MySQ

下面的教程中,我正在尝试在Ubuntu 14.04上安装WordPress。编辑
wp config.php
,我遇到了
“建立数据库连接时出错”
错误

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\u NAME
DB\u USER
DB\u PASSWORD
的值正确。我不确定DB_主机的
;我试过:

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外壳输出:

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用作db主机:

define('DB_HOST', '127.0.1.1');

如果用户没有使用权限,则不能将127.0.0.1用作db主机

wordpressuser只能连接到本地主机,因此如果尝试使用127.0.0.1,则会失败。尝试添加127.0.0.1作为用户的主机,并在配置文件中使用ip。您是否看到用户根目录有两个条目?一个以localhost作为主机,另一个以127.0.0.1作为主机。因为这是两种完全不同的连接方法,所以您的wp用户也需要两个条目。添加新条目,然后您应该能够在wpi的配置文件中使用127.0.0.1。我告诉您添加一个user=wordpressuser且host=127.0.0.1的条目,然后编辑配置文件并将DB_host更改为127.0.0.1