Home > Database > Mysql Tutorial > How to Restrict MySQL User Access to a Single Database?

How to Restrict MySQL User Access to a Single Database?

Susan Sarandon
Release: 2025-01-09 09:37:46
Original
493 people have browsed it

How to Restrict MySQL User Access to a Single Database?

Secure MySQL User Access: Limiting to a Single Database

Database security is paramount. This guide demonstrates how to create a MySQL user account with access restricted solely to a specific database, a crucial task for database administrators.

First, establish the target database using: CREATE DATABASE dbTest;

User Account Creation and Privilege Assignment

Create the new user account with the command:

<code class="language-sql">CREATE USER 'new_user'@'localhost';</code>
Copy after login

Replace 'new_user' with your desired username. 'localhost' restricts access to connections originating from the same server; adjust this to a specific IP address or '%' for all hosts if needed.

Next, grant the user full privileges on the dbTest database:

<code class="language-sql">GRANT ALL PRIVILEGES ON dbTest.* TO 'new_user'@'localhost' IDENTIFIED BY 'strong_password';</code>
Copy after login

Remember to replace 'strong_password' with a robust, unique password.

Command Breakdown:

  • GRANT: The core command for assigning privileges.
  • ALL PRIVILEGES: Grants all standard database privileges (SELECT, INSERT, UPDATE, DELETE, etc.). Consider using more specific grants for enhanced security if needed.
  • dbTest.*: Targets all tables and objects within the dbTest database.
  • TO 'new_user'@'localhost': Specifies the user and the allowed host.
  • IDENTIFIED BY 'strong_password': Sets the user's password.

This process ensures the new user can only access and manipulate data within the designated dbTest database, significantly improving your database's security posture. Always remember to regularly review and adjust user privileges as needed.

The above is the detailed content of How to Restrict MySQL User Access to a Single Database?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template