Macos cannot connect to mysql using localhost _ MySQL

Source: Internet
Author: User
Macos solved the problem of being unable to use localhost to connect to mysql. Today, the php environment has been set up on mac, and programs that run well in Windows and linux have been put on mac, so they cannot access the database, the host connected to the database uses localhost. you can confirm that the database configuration is correct, because the localhost access is normal in java, and the access can also be normal through the command line tool. When you change localhost to 127.0.0.1 or the IP address of the local machine, it is normal. I used to think that localhost and 127.0.0.1 are the same thing. now it turns out that there is a difference between them.

I searched for their differences on the internet and some people have already understood them. for details, see http://www.bitscn.com/article/50422.htm. Their main difference is that localhost is connected through socket, while 127.0.0.1 is the TCP protocol.

The difference mentioned above is the reason why the database cannot be connected in mac. Why cannot I connect through socket? Because apache and php are already provided on mac OS lion, the default version is used in my environment. the mysql service downloads the latest version of dmg on the official website, php configuration file (/ect/php. ini) is set in three places (mysql. default_socket, mysqli. default_socket, pdo_mysql.default_socket) sets the location for storing mysql socket files. the default value is/var/mysql. sock. There are two ways to view the socket file used by mysql service:
Method 1:


Echo "show variables" | mysql | grep "socket"

Method 2:


Echo "status" | mysql | grep "socket"

Note: If a password is set for mysql, use-u and-p to specify the user name and password.
The mysql socket file is stored in/tmp/mysql. sock.

You should know the problem here. the reason for the localhost connection failure is that, in the socket mode, the socket file of the mysql service configured by php by default does not exist, resulting in connection failure, if you dump the connection failure error information, you should see that the error message contains "No such file or directory ".

Knowing the cause is easy to modify. you only need to put mysql in the php configuration file. default_socket, mysqli. modify the values default_socket and pdo_mysql.default_socket to the corresponding values of the mysql service configuration. after modification, restart the apache service. Of course, you can also modify the socket information in the mysql configuration file to make it consistent. after modification, restart the mysql service.

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.