Solution to connection to Mysql using localhost in PHP: localhostmysql_PHP tutorial

Source: Internet
Author: User
Tags unix domain socket
In PHP, localhost is used to connect to Mysql. In PHP, how to use localhost to connect to Mysql is unsuccessful. localhostmysql encountered a problem when compiling and installing the Linux environment for a colleague yesterday: the WEB server is apache, how to use localhost in PHP to connect to Mysql is unsuccessful. localhostmysql

Problems found

I encountered a problem yesterday when I was helping my colleagues compile and install the Linux environment:
The WEB server is apache and the database is MySQL.

So I wrote a PHP page to test the connection to the database:

The code is as follows:

$ Mysql = mysql_connect ('localhost', 'root ','');

Open http: // localhost/test. php to test

Tip: Can't connect to local MySQL server through socket...

Check that the environment is normal

I thought the database was not started, so I checked the process. MySQL restarted MySQL in the process.

Use mysql-u root-p to enter the MySQL operation interface

You can directly use/usr/local/php5/bin/php/web/test. php to connect to the database.
Apache is also restarted, which is also invalid.

Question: Why does the Web page fail to be executed but the command is successfully executed?

This is depressing. if you use the php command to directly execute the command, the execution will fail through the web page. Is it caused by apache? No solution was found after searching a bunch of materials on the Internet, and the problem of re-compiling and installing apache still persists.

Changed localhost to 127.0.0.1.

After I changed localhost to 127.0.0.1, the connection was successful and I began to think about the dilemma: Why did the localhost fail 127.0.0.1 but it succeeded?

The ping localhost address is 127.0.0.1.

Open hosts to join

The code is as follows:


127.0.0.1 qttc


When qttc is used, the host connection is normal, but localhost is not recognized.

Due to different localhost connection methods

To learn about the differences between localhost and other settings when using PHP to connect to the database, I read a lot of information and finally learned:

When the host is set to localhost, mysql uses unix domain socket connection.
Mysql uses tcp connection when the host is set to 127.0.0.1.
This is a feature of the linux socket network, and the win platform does not have this problem.

Solution

In the [mysql] section of my. cnf, add

The code is as follows:

Protocol = tcp


Save and restart MySQL. The problem is solved!


An error occurred while connecting to mysql in php. if this problem can be solved, the append value is 100 points.

Baidu HI me

Is the database password correct?

$ Mysql = mysql_connect ("localhost", "username", "password ");
If ($ mysql ){
Echo "database connection successful ";
} Else {
Echo "failed ";
}

An error occurred while connecting to mysql using php.

Is mysql support not enabled?
Starting with php5, mysql is no longer a built-in support. you need to set the php configuration file php. ini to enable mysql extension.

Hacker found the problem yesterday when he helped his colleagues compile and install the Linux environment, he encountered a problem: the WEB server is apache, count...

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.