3v-Hosting Blog

Creating a New User and Granting Permissions in MySQL

Administration

5 min read


Creating and managing users in MySQL is a fundamental task for database administrators (DBAs). This task involves not only creating users, but also assigning appropriate permissions to ensure security and proper management of the database. In this article, we will take a detailed look at all the necessary steps to create a new user and grant permissions to that user in MySQL.

 

 

Introduction to User Management in MySQL

 

User management in MySQL involves several critical tasks, including creating new users, setting passwords, and granting or revoking permissions. These operations are essential for maintaining database security and ensuring that users have the appropriate level of access to perform their tasks.

 

Why is User Management Important?

    Security: Proper user management helps in protecting sensitive data by ensuring only authorized users have access.
    Organization: It allows for clear delineation of roles and responsibilities among users.
    Auditability: Tracking user activities becomes easier when each user has a unique account.

 

 

 

Step-by-Step Guide to Creating a New User in MySQL


Prerequisites

Before you begin, ensure you have administrative access to your MySQL server. Typically, this means you have access to the root user or another user with sufficient privileges.

Important! The root user password is assigned when MySQL is installed on the server, and it is this account that we are talking about below, and not the root user of the server operating system.

 


Connecting to MySQL

To connect to your MySQL server, use the following command in your terminal:


    mysql -u root -p


You'll be prompted to enter the root password. Upon successful login, you'll have access to the MySQL command-line interface.

 

 

Creating a New User

The CREATE USER statement is used to create a new user in MySQL. Here’s the basic syntax:


    CREATE USER 'username'@'host' IDENTIFIED BY 'password';

 

Example

To create a user named newuser with the password user_password, execute:


    CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'user_password';


This command creates a user newuser who can connect to the MySQL server from the local machine (localhost).

 

 

Granting Permissions to the New User

 

Once the user is created, the next step is to grant the necessary permissions. The GRANT statement is used for this purpose.

To grant all privileges on a specific database, use the following syntax:

 

    GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host';


Example

To grant all privileges on the database testdb to newuser, execute:


    GRANT ALL PRIVILEGES ON testdb.* TO 'newuser'@'localhost';

 

This command allows newuser full access to the testdb database.

 


Applying Changes

After granting permissions, it is crucial to apply these changes by flushing the privileges. This can be done using the following command:

 

    FLUSH PRIVILEGES;

 

 

Verifying the New User

 

To ensure that the new user has been created and the permissions are set correctly, you can use the SHOW GRANTS statement:

 

    SHOW GRANTS FOR 'newuser'@'localhost';

 

This will display all the privileges assigned to newuser.

 

 


 

Other articles on MySQL and administration:


    - A Comprehensive Guide: How to Find and Optimize Slow Queries in MySQL

    - How to remotely connect to MySQL

    - How to add or remove a user on Linux systems

    - How to make your website more secure for less money?

 


 

 

Detailed Breakdown of Permissions

 

MySQL allows for fine-grained control over user permissions. Here’s an overview of the most commonly used privileges:

    ALL PRIVILEGES: Grants all available privileges.
    SELECT: Allows reading data from tables.
    INSERT: Allows inserting data into tables.
    UPDATE: Allows updating existing data.
    DELETE: Allows deleting data.
    CREATE: Allows creating new databases and tables.
    DROP: Allows deleting databases and tables.
    ALTER: Allows modifying existing tables.

 

 

Granting Specific Privileges

 

To grant specific privileges, modify the GRANT statement accordingly. For example, to grant SELECT and INSERT privileges on testdb:

    GRANT SELECT, INSERT ON testdb.* TO 'newuser'@'localhost';

 

 

Revoking Privileges

 

If you need to revoke specific privileges, use the REVOKE statement:

 

    REVOKE privilege_type ON database_name.* FROM 'username'@'host';


Example

To revoke INSERT privilege from newuser on testdb:

 

    REVOKE INSERT ON testdb.* FROM 'newuser'@'localhost';

 

 

 

Advanced User Management

 

Creating a user with global privileges

 

If you need a user with global privileges, such as access to all databases, you can use the following command:


    GRANT ALL PRIVILEGES ON *.* TO 'username'@'host' WITH GRANT OPTION;

 

Example

To create an administrator user with global privileges and give him access to all databases on a given instance:


    CREATE USER 'adminuser'@'localhost' IDENTIFIED BY 'admin_password';
    GRANT ALL PRIVILEGES ON *.* TO 'adminuser'@'localhost' WITH GRANT OPTION;

 

 

Creating Users for Remote Access

 

To allow a user to connect from a remote host, replace localhost with the remote host's IP address or use % to allow connections from any host:

    CREATE USER 'remoteuser'@'%' IDENTIFIED BY 'remote_password';
    GRANT ALL PRIVILEGES ON *.* TO 'remoteuser'@'%' WITH GRANT OPTION;

 

 

Best Practices for User Management

    Least Privilege Principle: Grant the minimum required privileges to each user.
    Strong Passwords: Ensure that users have strong, unique passwords.
    Regular Audits: Periodically review and audit user permissions.
    Separation of Duties: Assign different roles to different users to enhance security.
    Backup User Data: Regularly back up user accounts and privileges.

 

 

Conclusion

Managing users and permissions in MySQL is a critical task for database administrators. By following the steps outlined in this article, you can create new users, grant appropriate permissions, and ensure the security and efficiency of your MySQL databases. Always adhere to best practices and regularly review user privileges to maintain a secure database environment.

And of course - MAKE BACKUPS! :)