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 | +-----------------------------+
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'
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;
Additional Considerations:
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!