How To Create a New MySQL User with Grant Permissions

Channel: Linux
Abstract: Grant MySQL User Permissions Here is the frequently used options with assigning privileges to user in MySQL. ALL – This will allow a mysql user the co

MySQL is a relational database management system used for storing data in tabular format. It provides high flexibility for the user account and grant permissions.

This tutorial will provide you a short overview to create MySQL user account and grant permissions on database.

Create A MySQL User with Permissions

Here we are running all queries as root account having full privileges to all databases. You can create MySQL user account with required privileges.

  1. Let’s create a new MySQL user within MySQL shell:
    mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'pa$$word';
    

    Here newuser is the username to be created. The localhost is defines that this user is only accessible from localhost only. To connect MySQL from any remote host, change the localhost with remote system ip address. You can also use % to allow any remote host to connect with this account.

  2. Now, assign the required privileges to the newly created MySQL user.
    mysql> GRANT ALL ON *.* TO 'newuser'@'localhost';
    

    Read next step to know more about various grant option for mysql account.

  3. After adding or modifying any privileges, make sur to reload the privilege’s to apply changes in running MySQL instance.
    mysql> FLUSH PRIVILEGES;
    
Grant MySQL User Permissions

Here is the frequently used options with assigning privileges to user in MySQL.

  • ALL – This will allow a mysql user the complete access to the specified database or full access to all databases
  • SELECT – Allow user to select data from tables
  • INSERT – Allow user to insert data into tables
  • UPDATE – Allow user to update data in tables
  • DELETE – Allow user to delete rows in tables
  • CREATE – Allow user to create new database and tables
  • DROP – Allow user to delete databases and tables
  • ALTER – Allow user to alter the structure of a table or to create triggers on a table.
  • GRANT OPTION -Allow user to grant or remove other user privileges

Use the following option to grant all privileges on specific database to [email protected]

mysql> GRANT ALL ON dbname.* TO 'newuser'@'localhost';

Use the following option to grant specific permissions like SELECT,INSERT,DELETE on a specific database to [email protected]

mysql> GRANT SELECT,INSERT,DELETE ON dbname.* TO 'newuser'@'localhost';

All all priviledges to [email protected] on specific database including permissions to grant other users.

mysql> GRANT ALL ON dbname.* TO 'newuser'@'localhost' WITH GRANT OPTION;

You can also view the allowed permission to a user in MySQL.

mysql> SHOW GRANTS FOR 'newuser'@'localhost';
Login to MySQL Shell

Login to MySQL shell with newly created user and password.

mysql -u newuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4817
Server version: 5.7.32-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Ref From: tecadmin

Related articles