In a warehouse such as Globus, Manor or Lafayette there are different user categories with different authorizations and privileges. According to the required responsibilties the general manager e.g. has full privileges, while the apprentice has much fewer. And as we know, a database is basicly a warehouse, not for goods, but for data.
MySQL user management and privileges are essential for controlling access to the database system, ensuring security, and managing user roles. MySQL allows database administrators (DBAs) to create, manage, and assign permissions to users, limiting what actions users can perform on the database. This control helps protect sensitive data, prevent unauthorized changes, and maintain the overall integrity of the database.
To create a new user in MySQL, you use the CREATE USER statement:
CREATE USER 'username'@'localhost' IDENTIFIED WITH 'caching_sha2_password' BY 'password';
This creates a user username that can only connect from localhost (the MySQL server machine).
Note
MySQL supports several types of password authentication mechanisms. These include:
To assign privileges to a user, the GRANT statement is used. For example, to grant a user the ability to SELECT, INSERT, and UPDATE on a specific database:
GRANT SELECT, INSERT, UPDATE ON database_name.* TO 'username'@'localhost';
The * symbol specifies that the privileges apply to all tables within database_name.
You can check a user’s privileges with the SHOW GRANTS command:
SHOW GRANTS FOR 'username'@'localhost';
This displays all the privileges granted to the user username.
To remove privileges from a user, the REVOKE statement is used. For example, to revoke the INSERT privilege:
REVOKE INSERT ON database_name.* FROM 'username'@'localhost';
If a user is no longer needed, you can remove the account using the DROP USER statement:
DROP USER 'username'@'localhost';
This removes both the user and their associated privileges.
To activate changes to privileges in MySQL after modifying them, you can use the following command:
FLUSH PRIVILEGES;
This command forces MySQL to reload the privilege tables, applying any recent changes made to user privileges. Normally, after using GRANT, REVOKE, or ALTER USER, MySQL applies changes automatically, but if you've made manual adjustments directly in the mysql database tables, running FLUSH PRIVILEGES ensures that the changes take effect immediately.
Please note that the change will only take effect AFTER a new login. This means that you must open a new console window with your new login data in order to recognize the change in permissions.
How to Create a New User in MySQL | |
---|---|
Create User, Grant ReadWrite Privileges and Test Privileges Granted in MySQL |