In the previous article, we considered several of the most popular operating systems that are usually chosen for installation on VPS servers. And today we want ...
3v-Hosting Blog
5 min read
Creating and managing users in MySQL is a fundamental task for database administrators (DBAs). This task involves not only creating users, but also assigning appropriate permissions to ensure security and proper management of the database. In this article, we will take a detailed look at all the necessary steps to create a new user and grant permissions to that user in MySQL.
User management in MySQL involves several critical tasks, including creating new users, setting passwords, and granting or revoking permissions. These operations are essential for maintaining database security and ensuring that users have the appropriate level of access to perform their tasks.
Security: Proper user management helps in protecting sensitive data by ensuring only authorized users have access.
Organization: It allows for clear delineation of roles and responsibilities among users.
Auditability: Tracking user activities becomes easier when each user has a unique account.
Before you begin, ensure you have administrative access to your MySQL server. Typically, this means you have access to the root user or another user with sufficient privileges.
Important! The root user password is assigned when MySQL is installed on the server, and it is this account that we are talking about below, and not the root user of the server operating system.
To connect to your MySQL server, use the following command in your terminal:
mysql -u root -p
You'll be prompted to enter the root password. Upon successful login, you'll have access to the MySQL command-line interface.
The CREATE USER statement is used to create a new user in MySQL. Here’s the basic syntax:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
Example
To create a user named newuser with the password user_password, execute:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'user_password';
This command creates a user newuser who can connect to the MySQL server from the local machine (localhost).
Once the user is created, the next step is to grant the necessary permissions. The GRANT statement is used for this purpose.
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host';
Example
To grant all privileges on the database testdb to newuser, execute:
GRANT ALL PRIVILEGES ON testdb.* TO 'newuser'@'localhost';
This command allows newuser full access to the testdb database.
After granting permissions, it is crucial to apply these changes by flushing the privileges. This can be done using the following command:
FLUSH PRIVILEGES;
To ensure that the new user has been created and the permissions are set correctly, you can use the SHOW GRANTS statement:
SHOW GRANTS FOR 'newuser'@'localhost';
This will display all the privileges assigned to newuser.
MySQL allows for fine-grained control over user permissions. Here’s an overview of the most commonly used privileges:
ALL PRIVILEGES: Grants all available privileges.
SELECT: Allows reading data from tables.
INSERT: Allows inserting data into tables.
UPDATE: Allows updating existing data.
DELETE: Allows deleting data.
CREATE: Allows creating new databases and tables.
DROP: Allows deleting databases and tables.
ALTER: Allows modifying existing tables.
To grant specific privileges, modify the GRANT statement accordingly. For example, to grant SELECT and INSERT privileges on testdb:
GRANT SELECT, INSERT ON testdb.* TO 'newuser'@'localhost';
If you need to revoke specific privileges, use the REVOKE statement:
Example
To revoke INSERT privilege from newuser on testdb:
REVOKE INSERT ON testdb.* FROM 'newuser'@'localhost';
If you need a user with global privileges, such as access to all databases, you can use the following command:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'host' WITH GRANT OPTION;
Example
To create an administrator user with global privileges and give him access to all databases on a given instance:
CREATE USER 'adminuser'@'localhost' IDENTIFIED BY 'admin_password';
GRANT ALL PRIVILEGES ON *.* TO 'adminuser'@'localhost' WITH GRANT OPTION;
To allow a user to connect from a remote host, replace localhost with the remote host's IP address or use % to allow connections from any host:
CREATE USER 'remoteuser'@'%' IDENTIFIED BY 'remote_password';
GRANT ALL PRIVILEGES ON *.* TO 'remoteuser'@'%' WITH GRANT OPTION;
Least Privilege Principle: Grant the minimum required privileges to each user.
Strong Passwords: Ensure that users have strong, unique passwords.
Regular Audits: Periodically review and audit user permissions.
Separation of Duties: Assign different roles to different users to enhance security.
Backup User Data: Regularly back up user accounts and privileges.
Managing users and permissions in MySQL is a critical task for database administrators. By following the steps outlined in this article, you can create new users, grant appropriate permissions, and ensure the security and efficiency of your MySQL databases. Always adhere to best practices and regularly review user privileges to maintain a secure database environment.
And of course - MAKE BACKUPS! :)
Learn how IP addresses work: IPv4 vs IPv6, public and private IPs, DNS resolution, routing, security basics, and how IPs are used in real server and cloud infra...
Accelerating WordPress at the Nginx level: correct PHP-FPM settings, try_files, static files, caching, Brotli, wp-login protection, and secure headers for stabl...
Effective backup strategies for Docker applications: how to protect volumes, data, and configurations while avoiding common mistakes, and quickly restore servic...