LNMP WordPress 建立数据库连接时出错

帮朋友的博客网站从apache 换成 nginx 之后wordpress 出错提示 “建立数据库连接时出错”.

而apache 是可以正常连接,本地mysql 也可以正常登录上去.

1、使用 mysql -uroot -p 登录上mysql 并用 show variables like ‘socket'; 查询mysql socket的文件存在什么地方,代码如下:

  1. [root@iZ23482kqn1Z default]# mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 18509
  5. Server version: 5.1.73 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> show variables like 'socket';
  12. +---------------+-----------------+
  13. | Variable_name | Value |
  14. +---------------+-----------------+
  15. | socket | /tmp/mysql.sock |
  16. +---------------+-----------------+
  17. 1 row in set (0.00 sec)

2、编辑 /etc/php.ini 文件找到 mysql.default_socket =,修改成以下代码:

  1. ; Default socket name for local MySQL connects. If empty, uses the built-in
  2. ; MySQL defaults.
  3. ; http://php.net/mysql.default-socket
  4. --phpfensi.com
  5. mysql.default_socket =/tmp/mysql.sock

3、重起php-fpm 服务

service php-fpm restart

问题解决.