2 min read

How to Create a Mysql User with Only Read Permissions?

how to create a mysql user with only read permissions?

How to Create a MySQL User with Only Read Permissions

When managing a MySQL database, one often needs to create users with specific permissions to ensure data security and integrity.

A common requirement is to set up a user with read-only access to prevent accidental or malicious modifications. In this guide, we will walk you through the steps to create such a user in MySQL.

Steps to Create a Read-Only User in MySQL

  1. Log into MySQL Server

    Begin by logging into your MySQL server using the root account or an account with sufficient privileges to create new users.

    mysql -u root -p
    
  2. Create a New User

    Once logged in, create a new user. Replace read_only_user and password with your desired username and password.

    CREATE USER 'read_only_user'@'localhost' IDENTIFIED BY 'password';
    

    Note: Adjust localhost if the user needs to connect from a different host.

  3. Grant Read-Only Permissions

    Grant the new user read-only access to the desired database. Replace database_name with the actual name of your database.

    GRANT SELECT ON database_name.* TO 'read_only_user'@'localhost';
    

    This command gives the user permission to execute SELECT statements on all tables within the specified database.

  4. Flush Privileges

    To apply changes immediately, flush the privileges.

    FLUSH PRIVILEGES;
    
  5. Verify Permissions

    It is a good practice to verify that the read-only user cannot perform any write operations. Log in as the new user and attempt different operations to ensure the permissions are correctly set.

    mysql -u read_only_user -p
    

    Inside the MySQL shell, try running a command like INSERT, which should fail due to insufficient privileges.

    INSERT INTO table_name (column1) VALUES ('value1');
    

    If the operation is denied, you've successfully set up the user with read-only permissions.

By following the steps outlined above, you ensure that your database remains secure against unauthorized modifications, while also improving your server administration skills. Remember to review related resources to deepen your understanding and keep your database fully optimized and secure.


This Markdown article is SEO optimized and includes links to relevant resources as requested.