1. Why Create Users in MySQL?
The Importance of User Management in MySQL
MySQL is a database management system widely used by companies and individual projects, and user management is essential for maintaining its security and efficiency. For example, in large-scale applications, multiple developers and administrators need access to the database. In such cases, it is important to assign different privileges to each user and properly restrict data access and operations.
By granting specific privileges, you can reduce the risk of data loss or security issues caused by unnecessary operations. In addition, implementing proper password management for each user helps prevent unauthorized external access. As a result, both data security and system performance are improved.
2. Steps to Create a User in MySQL
Basic User Creation Command
To create a user in MySQL, use the CREATE USER command. The following command creates a new user.
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';'username': The name of the new user to create.'localhost': The location from which the user can connect (if set to localhost, access is limited to the same server).'password': The password assigned to the user.
By default, a user can access the database from localhost. If you want to allow access from another IP address, replace localhost with the specified IP address.
User Creation Options
In MySQL, you can also configure additional options when creating a user. For example, you can specify a particular authentication plugin as shown below.
CREATE USER 'username'@'localhost' IDENTIFIED WITH 'auth_plugin' BY 'password';The auth_plugin specifies the MySQL authentication method (for example, caching_sha2_password). This helps enhance password security.

3. Granting Privileges to Users
Overview and Importance of Privileges
By default, a newly created MySQL user cannot perform any operations. To allow a user to perform actions, you must grant privileges using the GRANT command. For example, to grant all privileges to a user, use the following command.
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';This command allows the specified user to perform all operations on all tables within database_name. However, granting full privileges poses security risks, so it is recommended to allow only the operations that are truly necessary.
How to Grant Specific Privileges
In MySQL, you can grant only specific privileges as shown below.
- SELECT privilege (read data)
GRANT SELECT ON database_name.* TO 'username'@'localhost';- INSERT privilege (insert data)
GRANT INSERT ON database_name.* TO 'username'@'localhost';- UPDATE privilege (update data)
GRANT UPDATE ON database_name.* TO 'username'@'localhost';These privileges can be set for an entire database or for specific tables. For example, to allow operations on only a specific table, specify it as follows.
GRANT SELECT ON database_name.table_name TO 'username'@'localhost';4. Checking and Managing User Privileges
Checking a User’s Privileges
To check the privileges of a created user, use the SHOW GRANTS command. For example, the following command displays the current privileges of a specific user.
SHOW GRANTS FOR 'username'@'localhost';The result of this command lists all privileges currently granted to the user. This allows you to verify whether the privileges are correctly configured and adjust them if necessary.
Revoking Privileges (REVOKE Command)
To remove unnecessary privileges, use the REVOKE command. For example, to remove the SELECT privilege, execute the following command.
REVOKE SELECT ON database_name.* FROM 'username'@'localhost';After revoking privileges, be sure to confirm that the changes have been applied using the SHOW GRANTS command.
5. Best Practices for MySQL User Management
The Principle of Least Privilege
As a fundamental principle of database management, it is recommended to grant users only the minimum privileges necessary. This helps prevent data loss caused by accidental operations or unauthorized access. For example, a user who only needs to read data from the database should be granted only the SELECT privilege and no others.
Strengthening Password Policies
It is important to change MySQL user passwords regularly and implement strong password policies. You can use the ALTER USER command to change passwords or configure expiration settings.
ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password';To set a password expiration policy, configure it as follows.
ALTER USER 'username'@'localhost' PASSWORD EXPIRE INTERVAL 90 DAY;This requires the password to be updated every 90 days.
Regular Review of Privileges
User privileges should be reviewed periodically, and any unnecessary privileges should be removed. This helps maintain system security and reduce future risks. Use SHOW GRANTS regularly to check user privileges and manage them appropriately.
6. Conclusion
User management in MySQL is a critical component of maintaining system security and performance. By creating new users with the CREATE USER command and granting appropriate privileges using the GRANT command, you can operate your database efficiently and securely. Additionally, applying the principle of least privilege and strong password policies reduces the risk of unauthorized access and operational mistakes.
Regularly reviewing privileges and strengthening overall system security will lead to optimal database management practices.


