Problem description: php cannot connect to the local database when connecting to the local database through localhost. Analysis: php connects to the local database through localhost through socket connection. Therefore, you must find the socket file corresponding to mysql. Php connects to mysql through mysql, mysqli, and pdo_mysql in localhost mode and reads the php. ini file.
Problem description: php cannot connect to the local database when connecting to the local database through localhost. Analysis: php connects to the local database through localhost through socket connection. Therefore, you must find the socket file corresponding to mysql. Php connects to mysql through mysql, mysqli, and pdo_mysql in localhost mode and reads the php. ini file.
Problem description
Php cannot connect to the local database through localhost
Problem Analysis
Php uses localhost to connect to the local database through socket. Therefore, you must find the socket file corresponding to mysql. Php connects to mysql through mysql, mysqli, and pdo_mysql in localhost mode and reads the default-socket settings in the php. ini file. My php and mysql are both compiled and installed. At that time, the mysql socket file location was not specified. mysql generated the file location and the default value in php (/tmp/mysql. sock.
Solution
1. connect through an IP address. For example, mysql_connect ('192. 0.0.1 ', 'root ',");
2. Specify the mysql socket file address.
First, find the mysql socket file address.
Sudo find/-name mysqld. sock or
Echo "show variables" | mysql-uroot-ppassword | grep "socket"
The socket file address of mysql is returned as follows:
/Run/mysqld. sock
Modify the settings in three places in the php. ini file.
Mysql. default_socket, mysqli. default_socket, pdo_mysql.default_socket
3. Modify socket in my. cnf? ? ? ? ? =/Var/run/mysqld. sock (this parameter is available in several places and is not recommended for modification)
4. Soft link
Ln-s? /Var/run/mysqld. sock? /Tmp/mysql ?. Sock
Undoubtedly, the first method is the simplest.
Original article address: Can't connect to local MySQL server through socket, thanks to the original author for sharing.