【问题标题】:Force wordpress to connect to database over TCP强制 wordpress 通过 TCP 连接到数据库
【发布时间】:2012-07-29 10:17:23
【问题描述】:

我正在尝试将在我的机器上运行的 wordpress 连接到远程 mysql 数据库,该数据库在我的机器(本地主机)上建立隧道。数据库连接通过向mysql CLI 客户端提供以下参数来工作

$ mysql --protocol=TCP -P 10000 -h localhost -u username -p'password' db_name

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 93438893
Server version: 5.5.8-log Source distribution

Copyright (c) 2000, 2011, 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                |
| db_name                           |
+-----------------------------------+
2 rows in set (1.38 sec)

在 wordpress 的wp-config.php 文件中,我尝试了以下值:

define('DB_NAME', 'db_name');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
define('DB_PORT', 10000);

这不起作用并引发以下错误:

Warning: include(/home/gaurish/Dropbox/code/projects/blog/wp-content/advanced-cache.php): failed to open stream: No such file or directory in /home/gaurish/Dropbox/code/projects/blog/wp-settings.php on line 62 
Warning: include(): Failed opening '/home/gaurish/Dropbox/code/projects/blog/wp-content/advanced-cache.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/gaurish/Dropbox/code/projects/blog/wp-settings.php on line 62 
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/gaurish/Dropbox/code/projects/blog/wp-includes/wp-db.php on line 1038

上述错误中的最后一行 (Can't connect to local MySQL server through socket) 给出了连接失败的原因,因为 wordpress 试图通过 unix 套接字进行连接。

现在,我需要设置哪些参数才能让 wordpress 以mysql CLI 客户端的方式连接到数据库?

【问题讨论】:

    标签: php mysql wordpress


    【解决方案1】:

    我能够使用wp-config.php 中的以下设置进行连接。

    define('DB_NAME', 'db_name');
    define('DB_USER', 'username');
    define('DB_PASSWORD', 'password');
    define('DB_HOST', '127.0.0.1:10000');
    define('DB_PORT', 10000);
    

    数据库连接成功:)

    【讨论】:

      【解决方案2】:

      如果您想通过 TCP 而不是 Unix 套接字进行连接,请尝试将主机从 localhost 更改为 127.0.0.1

      define('DB_HOST', '127.0.0.1');  // forces TCP
      

      【讨论】:

      • 试过了但这也会产生错误:Warning: mysql_connect(): Can't connect to MySQL server on '127.0.0.1' (111) in /home/gaurish/Dropbox/code/projects/blog/wp-includes/wp-db.php on line 1038 还有什么想法吗?
      • 错误 111 表示访问被拒绝,您是否添加了允许通过 TCP 连接的用户?
      • 我能弄明白,感谢您建议 127.0.0.1 而不是 localhost
      猜你喜欢
      • 2022-08-14
      • 2020-09-06
      • 2012-04-26
      • 2014-01-15
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多