MySQL host 127.0.0.1 and localhost difference summary

Source: Internet
Author: User
Tags mysql host unix domain socket

1. Mysql-h 127.0.0.1, when using TCP/IP connection, MySQL server considers the connection to be from 127.0.0.1 or "Localhost.localdomain"

2. When mysql-h localhost, it is not using TCP/IP connection, but the use of UNIX sockets, at this time, MySQL server that the client is from "localhost"

3. "localhost" in MySQL Rights management has a specific meaning:

Note: Although there is a difference between the two connection methods, when localhost is the default 127.0.0.1, the permission records used by both connections are the following 1.row records (because the records are first matched)

Code to copy code as follows
1. Row ***************************
Host:localhost
User:root
......
2. Row ***************************
host:127.0.0.1
User:root

Prove:

Code to copy code as follows
Shell> mysql-h 127.0.0.1
mysql> status;
Current User: [email protected]
Ssl:not in use
Current Pager:stdout
Using outfile: '
Using delimiter:;
Server Version:5.1.33-log Source Distribution
Protocol version:10
connection:127.0.0.1 via TCP/IP
shell> mysql-h locahostmysql> status;
Current User: [email protected]
Ssl:not in use
Current Pager:stdout
Using outfile: '
Using Delimi (www.111cn.net) ter:;
Server Version:5.1.33-log Source Distribution
Protocol version:10
Connection:localhost via UNIX socket

Discover problems
Yesterday, when I helped my colleague compile and install the Linux environment, I encountered a problem:

The Web server is Apache and the database is MySQL.

A PHP page was written to test the connection database:

Code to copy code as follows
$mysql = mysql_connect (' localhost ', ' root ', ');

Open http://localhost/test.php Test

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

Check for normal environment
Thought the database did not start, so look at the process, MySQL in the process, restarted MySQL.

Use Mysql-u root-p to access the MySQL operator interface

Directly using/usr/local/php5/bin/php/web/test.php execution can connect to the database

Apache also restarted, just as invalid

Doubtful: Why the execution of the webpage failed, but the command execution succeeded
This is depressed, using the PHP command to execute the success, through the Web page execution will fail. is Apache the cause? Search the web for a lot of data and found no solution, recompile the installation of Apache is still the problem.

Change localhost to 127.0.0.1 success
Change the localhost to 127.0.0.1 After the connection succeeded, began to fall into the thinking dilemma: LocalHost failed 127.0.0.1 but success?

Ping localhost address is 127.0.0.1, yes.

Open the hosts join

Code to copy code as follows
127.0.0.1 QTTC

The use of QTTC when the host connection is also normal, but do not recognize localhost.

localhost connection mode caused by different
In order to understand the PHP connection database, the host to fill out localhost and other differences read a lot of information, and finally learned:

MySQL uses a UNIX domain socket connection when the host is populated with localhost

When the host is filled in as 127.0.0.1, MySQL will connect using TCP

This is the feature of the Linux socket network, the win platform will not have this problem

Workaround
Added in My.cnf's [MySQL] section

Code to copy code as follows
Protocol=tcp

Save restart MySQL, problem solved!
Reprint Annotated Source: http://www.111cn.net/database/mysql/45518.htm

MySQL host 127.0.0.1 and localhost difference summary

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.