Home  >  Article  >  Database  >  How to set MySQL permissions in Linux system

How to set MySQL permissions in Linux system

PHPz
PHPzOriginal
2023-04-21 11:27:131216browse

In Linux systems, MySQL is one of the most commonly used relational database management systems. MySQL permission settings allow us to control which databases and tables users can use, which operations they can perform, which data they can modify, etc. Below, this article will introduce how to set MySQL permissions in a Linux system.

  1. Login to MySQL

First, we need to log in to the MySQL system. In Linux systems, you can use the following command:

$ mysql -u [username] -p

where [username] is the user name in the MySQL system.

  1. View the current MySQL users and permissions

Next, we need to check the users and permissions in the current MySQL system. You can use the following command:

mysql> SELECT user, host, password FROM mysql.user;

This will return the username, hostname and encrypted password of all users. If you want to check the permissions of a certain user, you can use the following command:

mysql> SHOW GRANTS FOR [username]@[host];

where, [username] is the user name in the MySQL system, [host] Is the host name or IP address of the user.

  1. Create a new user

If you want to create a new user for MySQL, you can use the following command:

mysql> CREATE USER '[username]'@'[host]' IDENTIFIED BY '[password]';

Where, [username] is the username of the new user, [host] is the host name or IP address of the user, [password] is the user's password. Next, we need to grant permissions to this user.

  1. Authorization

In order to authorize, we need to use the GRANT command. The following example grants user "testuser" all permissions on the "testdb" database:

mysql> GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost' IDENTIFIED BY '[password]';

In this example, [password] is testuser's password. If you need to grant other permissions, you can use specific permissions after "ALL PRIVILEGES", for example:

mysql> GRANT SELECT, INSERT, UPDATE ON testdb.* TO 'testuser'@'localhost';

After granting permissions, don't forget to refresh MySQL:

mysql> FLUSH PRIVILEGES;
  1. Revoke permissions

If you need to revoke permissions, you can use the REVOKE command. For example, the following command will revoke all permissions to the "testdb" database from the user "testuser":

mysql> REVOKE ALL PRIVILEGES ON testdb.* FROM 'testuser'@'localhost';
  1. Delete user

Finally, if you need to delete the user, you can Use the following command:

mysql> DROP USER '[username]'@'[host]';

For example, the following command will delete the user "testuser":

mysql> DROP USER 'testuser'@'localhost';

Summary

Setting the permissions of MySQL in a Linux system is a necessary process. By understanding MySQL authorization and revocation commands, we can master the management method of MySQL users in Linux. At the same time, we can use these commands to create, delete and grant permissions to different MySQL users to better manage and protect the MySQL server.

The above is the detailed content of How to set MySQL permissions in Linux system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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