MySQL cannot connect to the database via localhost or 127.0.0.1

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

MySQL can not connect to the database through localhost solution

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 = Generally this item is empty and 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

Solutions that cannot be used with 127.0.0.1 connections


According to the error message "Warning:mysql_connect () [Function.mysql-connect]: Can ' t connect to MySQL server ' 127.0.0.1 '" using Google search Mys Ql_connect Some people say it's possible mysql_connect this function is disabled, using other connections, not tested.


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:

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.

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.