Home > Database > Mysql Tutorial > Why Does MySQL Treat `localhost` Differently Than `127.0.0.1`?

Why Does MySQL Treat `localhost` Differently Than `127.0.0.1`?

DDD
Release: 2024-11-25 02:47:10
Original
775 people have browsed it

Why Does MySQL Treat `localhost` Differently Than `127.0.0.1`?

MySQL localhost != 127.0.0.1?

This discrepancy arises due to MySQL's socket usage when invoked without a hostname or with the 'localhost' hostname. As demonstrated below, using MySQL with the hostname '127.0.0.1' connects via TCP/IP sockets:

$ mysql -u root -h 127.0.0.1 -e 'show tables' created_from_host;
+-----------------------------+
| Tables_in_created_from_host |
+-----------------------------+
| test                        |
+-----------------------------+
Copy after login

However, using 'localhost' connects via UNIX sockets, resulting in the following error:

$ mysql -u root -h localhost -e 'show tables' created_from_host;
ERROR 1049 (42000): Unknown database 'created_from_host'
Copy after login

How to grant ALL privileges on ALL databases from ALL hosts for root?

To grant unrestricted privileges to the 'root' user, execute the following SQL statement:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Copy after login

Additional Considerations:

  • skip_networking variable: Ensure this variable is set to 'OFF' to enable TCP/IP connections.
  • 'user' table: Check the 'mysql.user' table to verify that the 'root' user has host records for both 'localhost' and '127.0.0.1'.

The above is the detailed content of Why Does MySQL Treat `localhost` Differently Than `127.0.0.1`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template