Mac OS solves the problem of not using localhost to connect to Mysql _mysql

Source: Internet
Author: User

Today on the Mac set up the environment of PHP, the previous in Windows, Linux running a good program on the Mac, incredibly inaccessible database, database connection host with the localhost, you can confirm that the database configuration is correct, Because using localhost access in Java is normal, it is also accessible through command-line tools. When the localhost into 127.0.0.1 or native IP, incredibly normal. It has always been thought that localhost and 127.0.0.1 are one thing, and now it turns out that there is a difference.

On the Internet to search for their differences, someone has said very clearly, the specific can refer to: http://www.jb51.net/article/50422.htm. The main difference is that the localhost is connected by means of a socket, while 127.0.0.1 is the TCP protocol to go.

The difference mentioned above is why the database cannot be connected to the Mac. Then why can't the socket way to connect? Since the Mac OS Lion has its own Apache and PHP, my environment uses the default, the MySQL service is in the official website download DMG Install the most recent version, the PHP configuration file (/ect/php.ini) is set in three places (mysql.default_ Socket, Mysqli.default_socket, Pdo_mysql.default_socket) set the location of the MySQL socket file, the default value is/var/mysql/mysql.sock. There are two ways to view the socket files used by the MySQL service
Method 1:

Copy Code code as follows:

echo "Show variables" | MySQL | grep "Socket"

Method Two:

Copy Code code as follows:

echo "Status" | MySQL | grep "Socket"

Note: If MySQL sets a password, use-u,-p to specify the username and password
Here you see the MySQL socket file location for/tmp/mysql.sock.

See here should know what the problem is, the use of localhost connection failed because in the way of the socket, PHP default configuration of the MySQL service socket file does not exist, so the connection failed, if the connection failed error information dump out, You should see that the error message contains "No such file or directory."

Know the reason is easy to modify, just to the PHP configuration file mysql.default_socket, Mysqli.default_socket, Pdo_mysql.default_ Socket these three values modified for the MySQL service configuration corresponding value can be modified after the need to restart the Apache service. Of course, you can modify the MySQL configuration file socket information, so that it is consistent can be modified to restart the MySQL service.

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.