A summary of the difference between MySQL host 127.0.0.1 and localhost

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

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

2. When mysql-h localhost, it is not connected with TCP/IP and uses UNIX sockets; At this time, MySQL server thinks that the client is from "localhost"

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

Note: Although there are differences in the way they are connected, when localhost is the default 127.0.0.1, the permissions records used by both connections are the following 1.row records (because the record is first matched)

The code is as follows Copy Code
1. Row ***************************
Host:localhost
User:root
......
2. Row ***************************
host:127.0.0.1
User:root

Prove:

The code is as follows Copy Code

Shell> mysql-h 127.0.0.1
mysql> status;
Current User:root@localhost
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:root@localhost
Ssl:not in use
Current Pager:stdout
Using outfile: '
Using delimiter:;
Server Version:5.1.33-log Source Distribution
Protocol version:10
Connection:localhost via UNIX socket

Find problems
yesterday, when I was compiling my colleagues to install the Linux environment, I encountered a problem:

The Web server is Apache and the database is MySQL.

So I wrote a PHP page that tests the connection database:

The code is as follows Copy Code

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

Open http://localhost/test.php Test

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

Check for normal environment
Think that the database did not start, so look at the process, MySQL in the process, restart the MySQL.

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

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

Apache also restarted, as ineffective as

Doubt: Why Web page execution failed, command execution was successful
This is depressing, using the PHP command to execute directly on the success, through the Web implementation will fail. Could it be Apache that caused it? Search the Internet for a lot of data and did not find a solution, recompile installation Apache problem remains.

Change localhost to 127.0.0.1 success
Change the localhost to 127.0.0.1 after the successful connection, began to fall into the trap of thinking: localhost failure 127.0.0.1 but successful?

Ping localhost address is 127.0.0.1 Yes

Open hosts Join

The code is as follows Copy Code

127.0.0.1 QTTC

Use QTTC when host connection is normal, but do not recognize localhost.

LocalHost connection mode causes
In order to understand the PHP connection database, the host fills in the localhost and other differences read a lot of information, and finally learned:

MySQL will be connected with UNIX domain socket when the host is filled out as localhost

MySQL will be connected by TCP when the host is filled out as 127.0.0.1

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

Solving method
added in the [MySQL] section of MY.CNF

The code is as follows Copy Code

Protocol=tcp

Save reboot MySQL, problem solved!

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.