MySQL localhost Unable to connect to database solution

Source: Internet
Author: User
Tags apache error log mysql in socket system log

Locahost cannot connect to the MySQL database.

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 | The grep socket 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.


There is also a situation is only connected 127.0.0.1 cannot connect localhost solution

1, according to the error of the message "Warning:mysql_connect () [Function.mysql-connect]: Can ' t connect to the MySQL server on ' 127.0.0.1 '" using Google search m Ysql_connect Some people say that it is possible to mysql_connect this function is disabled, using other connections, the test has not.

2, use Google search MySQL can use localhost connection, can not use 127.0.0.1 connection, the result of most people's solution is:

Modify MY.CNF file, remove skip_networking,bind_address=127.0.0.1, restart MySQL fault still.

Permission issue, grant all privileges on *.* to ' root ' @ ' 127.0.0.1 ' identified by ' password ';  then flush privileges; The fault remains.

Modify/etc/hosts file, add 127.0.0.1 localhost item, the failure is still.

3, check the Apache error log, found that only the file does not exist log information, not found and access to MySQL data connection files related to the project.

4, view the MySQL log, did not find the relevant connection items.

5, finally I think up to check the system log/var/log/messages information too much, clean up: echo "" >/val/log/messages and then access the data connection test file. The following information was found:

The code is as follows Copy Code

Nov 10:18:49 localhost setroubleshoot:selinux is blocking the HTTP daemon from connecting to the network port 3306 for complete SELinux. Run Sealert-l A214e214-c710-4c84-9eb

2-12764f4b27de

The result is very obvious, is SELinux trouble, close SELinux test "/usr/sbin/setenforce 0 immediately shut down SELinux" database connection everything is OK.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.