If the above method is still not used, you can refer to the following solutions
Problem: A server's PHP program cannot connect to the database through the localhost address, but if set to 127.0.0.1 you can connect normally, and connecting to other database servers is normal. MySQL permissions are set correctly and the database can be connected properly through the MySQL command-line client.
Analysis: This is the case where the typical socket is not set correctly.
There are two ways to connect to a MySQL database: TCP/IP (a generally understood port type) and UNIX sockets (usually called sockets or sock). Most of the time, you can use localhost to represent the local 127.0.0.1, but when the MySQL connection, the two are not mixed, and MySQL in the permissions set in localhost and 127.0.0.1 are set separately. When set to 127.0.0.1, the system connects the database via TCP/IP, and when set to LocalHost, the system connects the database via a socket.
Solution: First look at the local MySQL socket socket file where the view command is:
Mysqld--verbose--help | grep socket
The output shows the location of the socket file, for example: This server displays a
Socket/var/run/mysqld/mysqld.sock
Then modify the PHP configuration file php.ini and corresponding to the good.
Find this item:
Mysql.default_socket =
In general, this one is empty and is changed to:
Mysql.default_socket =/var/run/mysqld/mysqld.sock
Here you should write a query to the file and set it according to your situation. This PHP configuration is modified, if the CLI (command line) or CGI way, immediately effective, if it is fastcgi way, need to restart the fastcgi process.
In Windows system may encounter
Error message: Error 2003 (HY000): Can ' t connect to MySQL server on ' hostxxxxx ' (10061)
Reason: The MYSQLD database service is not started.
Check: In Windows Task Manager, or under Unix/linux Ps-aux | grep MySQL look. Confirm that the service has started.
Processing: Start mysqld Service
Solutions
Method One: net start MySQL (or other service name)
Method Two: Under XP to start MySQL service MySQL installation directory:
"D:mysql" enters the command Line Input window (DOS command input) and runs the "mysqld-u Root" command in the "D:mysqlbin" directory. Writing: D:mysqlbinmysqld-u root (Enter) running a MySQL process in process management, running successfully (you may need to close the command window manually) and then running the "MySQL" command in the "D:mysqlbin" directory: D: Mysqlbin (Enter)