Home >Database >Mysql Tutorial >Why Use Both \'%\' and \'localhost\' When Defining MySQL User Hostnames?
MySQL User Hostnames: 'localhost' vs. '%'
In structuring a MySQL database, it's often necessary to create multiple users with distinct access privileges. One common debate arises when determining whether to specify a '%' or 'localhost' as the hostname for these users. This question delves into the difference between these two hostnames.
Problem:
A developer insists on creating four user accounts for two users (appuser and support):
The question here is why such a setup is necessary, considering the '%'-wildcard would seemingly cover 'localhost'.
Answer:
The hostname 'localhost' holds special significance in MySQL. Unlike '%'-wildcard, which applies to любых TCP/IP connections, 'localhost' exclusively refers to connections established via UNIX sockets (or named pipes on Windows systems).
In MySQL, the '%'-wildcard excludes localhost, making it necessary to explicitly specify it as a separate hostname. This setup allows for fine-grained control over user access from different network interfaces. By designating both '%' and 'localhost' for each user, the following is achieved:
The above is the detailed content of Why Use Both \'%\' and \'localhost\' When Defining MySQL User Hostnames?. For more information, please follow other related articles on the PHP Chinese website!