MySQL cannot connect to the database through localhost or 127.0.0.1

Source: Internet
Author: User
Tags apache error log mysql command line

Today, let's take a look at some of the issues that mysql encountered when connecting to the database. The most common problems that mysql cannot connect to the database are that it cannot connect to the database through localhost or 127.0.0.1. The following describes the solutions.

MySQL cannot connect to the database through localhost

Problem: the PHP program of one server cannot connect to the database through the localhost address, but if it is set to 127.0.0.1, the connection can be normal, and the connection to other database servers is normal. The MySQL permission is correctly set and the mysql command line client can connect to the database normally.

Analysis: this is a typical scenario where the socket is not correctly set.

There are two ways to connect to the MySQL database: TCP/IP (the port that is generally understood) and Unix socket (usually socket or sock ). In most cases, you can use localhost to represent the local machine 127.0.0.1. However, the two cannot be mixed during MySQL connection, and localhost and 127.0.0.1 in MySQL permission settings are also set separately. When it is set to 127.0.0.1, the system connects to the database through TCP/IP; when it is set to localhost, the system connects to the database through socket.

Solution: First, check where the socket file of MySQL is located. The command is as follows:

Mysqld -- verbose -- help | the grep socket output result shows the location of the socket file. For example, this server displays

Socket/var/run/mysqld. sock


Then modify the php configuration file php. ini to match it.

Find this item:

Mysql. default_socket = in general, this item is empty and changed:

Mysql. default_socket =/var/run/mysqld. sock

Here, you should write the file found in the previous step and set it according to your situation. Now the php configuration has been modified. If it is CLI (command line) or CGI, it will take effect immediately. If it is FASTCGI, restart the fastcgi process.

Solution for connection failure of 127.0.0.1


According to the error message "Warning: mysql_connect () [function. mysql-connect]: Can't connect to MySQL server on '100. 0.0.1 '"use google to search for mysql_connect. Some people say that the function mysql_connect may be disabled. If other connections are used, the test will not work.


Modify the my. cnf file, remove skip_networking, bind_address = 127.0.0.1, and restart mysql.

 

For permission issues, grant all privileges on *. * to 'root' @ '127. 0.0.1 'identified by 'Password'; then flush privileges; the fault persists.

 

Modify the/etc/hosts file and add the 127.0.0.1 localhost item. The fault persists.

3. Check the apache error log. Only the log information such as the file does not exist is recorded, and no project related to accessing the mysql DATA connection file is found.

4. Check mysql logs and no related connection items are found.

5. Check the system log/var/log/messages. Clear echo "">/val/log/messages and access the data connection test file. The following information is found:

Nov 30 10:18:49 localhost setroubleshoot: SELinux is blocking the http daemon from connecting to network port 3306 For complete SELinux messages. run sealert-l a214e214-c710-4c84-9eb

2-000064f4b27de

The result is obvious. It is the fault of selinux. Close selinux and test "/usr/sbin/setenforce 0" to immediately disable SELINUX database connection. Everything is normal.

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.