How to create MySQL user accounts and grant permissions

MySQL is the most popular open source relational database management systems. MySQL server allows us to create a large number of user accounts and grant the appropriate permissions so that users can access and manage databases.

This tutorial shows you how to create a MySQL user accounts and grant permissions.

Before you begin

We assume you have MySQL or MariaDB server installed on the system. If not, you can easily install it in one of the following tutorial:

All commands as an administrative user in the MySQL shell (create user accounts and define the minimum required permissions are the permissions CREATE USER and GRANT) or root account to perform.

To access the MySQL shell, type the following command, and enter your MySQL root user password when prompted:

mysql -u root -p

If you have not set a password for the MySQL root user, you can omit the -p label.

Create a new MySQL user accounts

MySQL in a user account consists of a user name and host name part.

To create a new MySQL user account, run the following command:

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

Newuser replace the placeholder values ​​for your new user name, the placeholder value user_password replaced with the user password. such as:

How to create MySQL user accounts and grant permissions

In the above command, hostname portion is set to localhost, which means that users only from localhost (that is, running the MySQL Server system) to connect to the MySQL server.

To grant access to other hosts, use the remote to change the computer IP host name portion (localhost). For example, from IP 192.168.12.189 computer to grant access, you will run:

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

To create a user can connect from any host, use the "%" wildcard as the host part:

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

MySQL user account permissions granted

User accounts can be granted several types of permissions. You can find here list of MySQL support full rights.

The most common permissions are:

  • ALL PRIVILEGES- granted all permissions for the user account.
  • CREATE - allows user accounts to create databases and tables.
  • DROP - allows user accounts and delete database tables.
  • DELETE - allows the user to delete rows from a specific account table.
  • INSERT - allows the user to insert a row into account specific table.
  • SELECT - allows the user to read the account database.
  • UPDATE - allows user accounts to update table rows.

To grant specific permissions for the user account, you can use the following syntax:

GRANT permission1, permission2 ON database_name.table_name TO 'database_user'@'localhost';

Some examples are as follows:

  • All permissions granted to a specific user account on the database:

GRANT ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';

  • Grant all permissions for all user accounts on the database:

GRANT ALL PRIVILEGES ON *.* TO 'database_user'@'localhost';

  • All access to the user account database through a specific table:

GRANT ALL PRIVILEGES ON database_name.table_name TO 'database_user'@'localhost';

  • Grant permissions to multiple user accounts through a specific database:

GRANT SELECT, INSERT, DELETE ON database_name.* TO database_user@'localhost';;

  • Display MySQL user account permissions

To find the MySQL permissions granted to a particular user account, use the SHOW GRANTS statement:

SHOW GRANTS FOR 'database_user'@'localhost';


+---------------------------------------------------------------------------+
|  Grants for database_user@localhost                                        |
+---------------------------------------------------------------------------+
|  GRANT USAGE ON *.* TO 'database_user'@'localhost'                        |
|  GRANT ALL PRIVILEGES ON `database_name`.* TO 'database_user'@'localhost'  |
+---------------------------------------------------------------------------+
2  rows in set (0.00 sec)

MySQL user account privileges revoked

Undo one or more privileges from the user account is almost the same syntax when granted.

For example, to revoke all permissions for a user account through a specific database, use the following command:

REVOKE ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';

Delete an existing MySQL user account

To delete a MySQL user account, use the DROP USER statement:

DROP USER 'user'@'localhost'

The above command will delete user accounts and their privileges.

to sum up

This tutorial covers only the basics, but for those who want to learn how to create a new MySQL user accounts and to grant rights, it should be a good start.

If you have any questions or feedback, please feel free to comment.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160321.htm