Home >Database >Mysql Tutorial >Why Use Both \'%\' and \'localhost\' When Defining MySQL User Hostnames?

Why Use Both \'%\' and \'localhost\' When Defining MySQL User Hostnames?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-30 18:08:31405browse

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):

  • appuser@'%'
  • appuser@'localhost'
  • support@'%'
  • support@'localhost'

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 '%' hostname grants access from any remote IP address.
  • The 'localhost' hostname grants access from within the same server using UNIX sockets (or named pipes).

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!

Statement:
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