PHP cannot connect to MySQL server using localhost

Source: Internet
Author: User
Tags mysql command line
PHP cannot connect to MySQL server using localhost
MySQL is installed under Redhat, under the MySQL console, you can connect using localhost, but not when you connect in PHP. and using 127.0.0.1 can connect, view the Hosts file, found the corresponding 127.0.0.1 and localhost

Which Warrior knows what's going on?

------Solution--------------------
is not the site when you set up the site for http://127.0.0.1 it?
------Solution--------------------
Have a look at how your MySQL is configured,
Check if/usr/local/mysql/var/mysql.sock is present after MySQL startup.
------Solution--------------------
Description
A frequent error message received when using the MySQL command line utility Is:can ' t connect to local MySQL server throu GH socket '/tmp/mysql.sock ' While this error message can be frustrating, the solution are simple.

Directions
When connecting to a MySQL server located on the local system, the MySQL client connects thorugh a local file called a Soc Ket instead of connecting to the localhost loopback address 127.0.0.1. For the MySQL client, the default location of this socket file Is/tmp/mysql.sock. However, for a variety of reasons, many MySQL installations place this socket file somewhere else like/var/lib/mysql/mysq L.sock.

While it's possible to make this work by specifying the socket file directly in the MySQL client command

MySQL--socket=/var/lib/mysql/mysql.sock ...


It is painful to type the this in every time. If You must does the this (because you don't have permissions to the file in the solution below), you could create an AL IAS in your shell-to-make

(like alias mysql= "MySQL--socket=/var/lib/mysql/mysql.sock" depending on your shell).

To make your life easier, your can make a simple change to the MySQL configuration file/etc/my.cnf that would permanently s ET the socket file used by the MySQL client. After making a backup copy of/etc/my.cnf, open it in your favorite editor. The file is divided into sections such as

[Mysqld]
Datadir=/usr/local/mysql/data
Socket=/var/lib/mysql/mysql.sock

[Mysql.server]
User=mysql
Basedir=/usr/local/mysql

If There is not currently a section called [Client], add one at the bottom of the file and copy the socket= line under the [MYSQLD] section such as:

[Client]
Socket=/var/lib/mysql/mysql.sock

If there is already a [client] sections in the My.cnf file, add or edit the socket line as appropriate. You won ' t need to restart your server or any other processes. Subsequent uses of the MySQL client would use the proper socket file.

  • 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.