今天迁移站点到新环境,出现了SQLSTATE[HY000] [2002] 错误,如果用mysql或mysql连接的话,会出现mysql_connect(): No such file or directory的错误。最后发现是mysql的socket没有配置好。

step 1:打开 /etc/my.cnf 文件,找到socket的位置

socket=/var/lib/mysql/mysql.sock

或是在mysql中执行status命令也可以看到

MariaDB [lianhua]> status;
--------------
mysql  Ver 15.1 Distrib 10.0.14-MariaDB, for Linux (x86_64) using  EditLine wrapper

Connection id:		52
Current database:	lianhua
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server:			MariaDB
Server version:		10.0.14-MariaDB MariaDB Server
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8
Db     characterset:	utf8
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/var/lib/mysql/mysql.sock
Uptime:			4 hours 38 min 43 sec

Threads: 1  Questions: 459  Slow queries: 0  Opens: 135  Flush tables: 1  Open tables: 168  Queries per second avg: 0.027
--------------

step 2:修改php.ini的socket配置

pdo_mysql.default_socket=/var/lib/mysql/mysql.sock
mysql.default_socket=/var/lib/mysql/mysql.sock
mysqli.default_socket=/var/lib/mysql/mysql.sock

可能是因为fedora21提供的MariaDB默认的socket路径不一样,所以才要重新配置。

By charlie

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注