In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces what is the difference between MySQL database host 127.0.0.1 and localhost. What is introduced in this article is very detailed and has a certain reference value. Interested friends must read it!
There may be many friends will encounter a problem do not know what is the difference between 127.0.0.1 and localhost, but some time will find that the use of localhost connection can not, but changed to 127.0.0.1 to connect, then what will be the difference between them, I will introduce to you.
When mysql-h 127.0.0.1 uses a TCP/IP connection, mysql server thinks the connection comes from 127.0.0.1 or "localhost.localdomain"
Mysql-h localhost uses Unix socket instead of TCP/IP connections; in this case, mysql server believes that the client is from "localhost"
The word "localhost" in mysql rights management has a specific meaning:
Note: although there is a difference between the two connection methods, when localhost is 127.0.0.1 by default, the permission records used by both connection methods are the following 1.row records (because the records are matched first)
* * 1. Row * Host: localhost User: root. * * 2. Row * Host: 127.0.0.1 User: root
Proof:
Shell > mysql-h 127.0.0.1 mysql > status; Currentuser: root@localhost SSL: Notin 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; Currentuser: root@localhost SSL: Notin 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 a problem
Yesterday, I encountered a problem while compiling and installing the Linux environment for my colleagues:
The Web server is apache and the database is MySQL.
So I wrote a PHP page to test the connection to the database:
$mysql = mysql_connect ('localhost','root','')
Open the http://localhost/test.php test
Hint: Can't connect to local MySQL server through socket...
Check that the environment is normal
Thought that the database did not start, so check the process, MySQL in the process, restart MySQL.
Use mysql-u root-p to enter the MySQL interface
Directly use / usr/local/php5/bin/php / web/test.php execution to connect to the database
Apache has also been restarted, which is also invalid.
Doubt: why the execution of the web page failed, but the execution of the command succeeded
This is depressing, the direct execution of the php command is successful, and the execution through the web page fails. Is it caused by apache? After searching a lot of information on the Internet, I couldn't find a solution, and the problem of recompiling and installing apache is still the same.
Change localhost to 127.0.0.1 successfully
After changing localhost to 127.0.0.1, the connection was successful and began to fall into a thinking dilemma: localhost failed 127.0.0.1 but succeeded?
The ping localhost address is 127.0.0.1, yes.
Open hosts to join
127.0.0.1 qttc
Use qttc when the host connection is also normal, but do not recognize localhost.
Different ways of localhost connection result in
In order to understand when PHP connects to the database, the host reads a lot of materials by filling in localhost and other differences, and finally knows:
When the host is localhost, mysql will use unix domain socket connection.
When the host is 127.0.0.1, mysql will connect in tcp mode.
This is a feature of linux socket networks, and win platforms will not have this problem.
Solution method
Add in the [mysql] section of my.cnf
Protocol=tcp
The above is all the contents of this article entitled "what is the difference between MySQL database host 127.0.0.1 and localhost". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Typedef enum DBState {DB_STARTUP = 0, DB_SHUTDOWNED, DB_SHUTDOWNED_IN_RECOVERY
© 2024 shulou.com SLNews company. All rights reserved.