Php cannot use localhost to connect to the mysql server

Source: Internet
Author: User
Php cannot use localhost to connect to the mysql server. mysql is installed in redhat. in the mysql console, you can use localhost to connect to the server. However, when you connect to the server in php, you cannot connect to the server using 127.0.0.1, check the hosts file and find out which of the following heroes corresponding to 127.0.0.1 and localhost know what is going on ------ solution -------------- php cannot use localhost to connect to the mysql server
Mysql is installed in redhat. you can use localhost to connect to the mysql console, but it cannot be connected in php. you can use 127.0.0.1 to connect to and view the hosts file, it is found that there are 127.0.0.1 and localhost mappings.

Which of the following heroes knows what is going on?

------ Solution --------------------
Did you set the site to http: // 127.0.0.1 when creating the site?
------ Solution --------------------
Let's see how your mysql is configured,
Check whether/usr/local/mysql/var/mysql. sock exists after mysql is started.
------ Solution --------------------
Description
A frequent error message received ed when using the mysql command line utility is: Can't connect to local MySQL server through socket '/tmp/mysql. sock 'While this error message can be frustrating, the solution is simple.

Directions
When connecting to a MySQL server located on the local system, the mysql client connects thorugh a local file called a socket 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, using MySQL installations place this socket file somewhere else like/var/lib/mysql. sock.

While it is possible to make this work by specifying the socket file directly in the mysql client command

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


It is painful to type this in every time. if you must do so this way (because you don't have permissions to the file in the solution below), you cocould create an alias in your shell to make this work

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

To make your life easier, you can make a simple change to the MySQL configuration file/etc/my. cnf that will permanently set 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 pided into sections such

[Mysqld]
Datadir =/usr/local/mysql/data
Socket =/var/lib/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:

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

If there is already a [client] section 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 will 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.