Summary of differences between MySQL host 127.0.0.1 and localhost, mysqllocalhost

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

Summary of differences between MySQL host 127.0.0.1 and localhost, mysqllocalhost

1. mysql-h 127.0.0.1 is connected using TCP/IP. mysql server considers the connection from 127.0.0.1 or "localhost. localdomain"

2. When mysql-h localhost is used, Unix socket is used instead of TCP/IP connection. At this time, mysql server considers that the client is from "localhost"

3. "localhost" in mysql permission management has specific meanings:

Note: although there are differences between the two connection methods, when localhost is 127.0.0.1 by default, the permission records used by the two connection methods are the following 1. row record (because the record is first matched)

The Code is as follows:
* *************************** 1. row ***************************
Host: localhost
User: root
......
* *************************** 2. row ***************************
Host: 127.0.0.1
User: root

Proof:

The Code is as follows:
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 delimi (www.111cn.net) ter :;
Server version: 5.1.33-log Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket

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!
Reprinted with the source: http://www.111cn.net/database/mysql/45518.htm


What is the difference between database connection question 127001 and localhost?

Your hosts file is not defined

[C: \ WINDOWS \ system32 \ drivers \ etc] type hosts

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# Entry shoshould be kept on an individual line. The IP address shold
# Be placed in the first column followed by the corresponding host name.
# The IP address and the host name shocould be separated by at least one
# Space.
#
# Additionally, comments (such as these) may be inserted on individual
# Lines or following the machine name denoted by a' # 'symbol.
#
# For example:
#
#102.54.94.97 rhino.acme.com # source server
#38.25.63.10 x.acme.com # x client host

127.0.0.1 localhost

[C: \ WINDOWS \ system32 \ drivers \ etc]

Localhost is different from the web page opened during the 127001 test.

What is opened ??

Is it another URL on your host?
You are running the command CMD.
Ping localhost

Check the IP address.

Localhost indicates local
Generally, the IP address is 127.0.0.1.
That is to say, I ping 127.0.0.1 = ping localhost

127.0.0.1 represents the loop address of your machine. Whether connected or not, 127.0.0.1 and 127 are reserved segments.

Relationship between localhost and 127.0.0.1:
Localhost can be regarded as the host name, 127 -- it can be regarded as the host ip address, which was previously mapped from a host name to an ip address in the middle!
If you connect to the database in mysql, use localhost instead of 127.0.0.1. In mysql, the user logon location is limited. For details, see the user table in mysql database (database Name Is mysql). The ip address and domain name are different, mysql does not automatically convert the ip address to the domain name when matching. You authorize a user to access from this ip address. It does not mean authorizing the user to access from this ip address.
1. Use TCP/IP to connect to mysql-h 127.0.0.1,
Mysql server considers the connection from 127.0.0.1 or "localhost. localdomain ".
2. For mysql-h localhost, Unix socket is used instead of TCP/IP connection;
Mysql server considers the client as from "localhost ".

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.