Many of my friends may encounter a problem and don’t know what the difference is between 127.0.0.1 and localhost. However, sometimes they find that they cannot connect using localhost, but they can connect by changing to 127.0.0.1. So what is the difference between them? Let me introduce them to you.
Note: Although the two connection methods are different, when localhost is the default 127.0.0.1, the permission records used by both connection methods are the following 1.row records (because the record comes first and is matched first) *************************** 1. row *************************** Host: localhost User: root ...... *************************** 2. row *************************** Host: 127.0.0.1 User: root prove: shell>mysql -h 127.0.0.1 mysql> status; Currentuser: root@localhost SSL: Not used 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: Not used Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.1.33-log Source distribution Protocol version: 10 Connection: Localhost via UNIX socket Find the problem Yesterday, when I was helping my colleague compile and install the Linux environment, I encountered a problem: 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 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 and found MySQL was in the process, so I restarted MySQL. Use Directly use I restarted apache, but it didn't work. Question: Why the web page execution failed, but the command execution succeeded This is very frustrating. It succeeds when I execute it directly using the PHP command, but fails when I execute it through the web page. Is it caused by apache? I searched a lot of information online but couldn't find a solution. The problem still exists after recompiling and installing Apache. Changing localhost to 127.0.0.1 succeeded After changing localhost to 127.0.0.1, the connection was successful. I began to think in a dilemma: why did localhost fail but 127.0.0.1 succeed? Ping localhost address is 127.0.0.1 Open hosts and join
Using qttc as the host connection is normal, but it does not recognize localhost. Different localhost connection methods lead to In order to understand the difference between filling in localhost and other hosts when PHP connects to the database, I read a lot of information and finally learned: When the host is filled in as localhost, MySQL will use When the host is filled in as 127.0.0.1, MySQL will use TCP to connect This is a feature of the Linux socket network. Windows platforms will not have this problem. Workaround Add in the [mysql] section of my.cnf Summarize The above is the difference between MySQL database host 127.0.0.1 and localhost introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Detailed explanation of Nginx timed log cutting
>>: Summarize the problems encountered in using Vue Element UI
vertical-align attribute is mainly used to change...
Overview Operations on any one database are autom...
The test is passed in the nodejs environment. The...
Table of contents question extend Solving the pro...
Recent projects involve the creation of a lot of ...
1. Flex layout .father { display: flex; justify-c...
Table of contents 1. The direction of this in the...
Table of contents Preface Do not use strings to s...
Table of contents 1. JavaScript can change all HT...
Sometimes, while working with files in the Linux ...
Table of contents 1. Demand 2. Database Design 3....
When I first started setting up an ftp server on ...
Table of contents What is insert buffer? What are...
1. MHA By monitoring the master node, automatic ...
Table of contents Discover: Application of displa...