It perfectly solves the problem that MySQL cannot connect to the database through localhost, mysqllocalhost

Source: Internet
Author: User

It perfectly solves the problem that MySQL cannot connect to the database through localhost, mysqllocalhost

Problem:A server's PHP program 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 case 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 | grep socket

The output result shows the location of the socket file. For example, this server displays

socket      /var/run/mysqld/mysqld.sock

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

Find this item:

mysql.default_socket =

Generally, this item is empty and changed:

mysql.default_socket = /var/run/mysqld/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.

The above is a perfect solution to the problem that MySQL cannot connect to the database through localhost, that is, all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's house.

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.