Home >Database >Mysql Tutorial >How to Create a MySQL User with Restricted Access to a Single Database?

How to Create a MySQL User with Restricted Access to a Single Database?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-09 09:42:45995browse

How to Create a MySQL User with Restricted Access to a Single Database?

MySQL user security management: restricting single database access permissions

Database security is critical and creating user accounts with specific permissions is a best practice. This effectively prevents unauthorized access and data modification. This article will demonstrate how to create a MySQL user that can only access the database named dbTest.

Create User

Create a new user using the following command:

<code class="language-sql">CREATE USER '用户名'@'主机名';</code>

Replace 用户名 with the user name you want, and 主机名 with the hostname or IP address of the server the user is connected to.

Give database permissions

After the user is created, use the following command to give it permission to access the dbTest database:

<code class="language-sql">GRANT ALL PRIVILEGES ON dbTest.* TO '用户名'@'主机名' IDENTIFIED BY '密码';</code>

ALL PRIVILEGESGrants user dbTest all standard permissions on the database. To further restrict access, you can replace * with a specific table name or stored procedure. 主机名Specify the host where the user can connect to the database. 密码Set the user's password.

Detailed explanation of commands

  • GRANT: is used to grant permissions to a user or role.
  • ALL PRIVILEGES: Grants all standard permissions, except permissions granted to other users.
  • dbTest.*: Grants access to all objects in the dbTest database.
  • TO 'username'@'hostname': Specifies the username and the host to which the user can connect.
  • IDENTIFIED BY 'Password': Set the user's password.

Through the above steps, you can create a MySQL user that is limited to specific database access, thereby achieving safer and more granular database management.

The above is the detailed content of How to Create a MySQL User with Restricted Access to a Single Database?. 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