Server Gigabit Guide

Comprehensive Guide to Creating and Managing User Accounts in MySQL

You are here:
Estimated reading time: 2 min

MySQL is a popular relational database management system (RDBMS) that allows users to create, read, update, and delete (CRUD) data. Managing user accounts and permissions is crucial for maintaining database security and ensuring that users have the appropriate level of access to perform their tasks. This guide provides a comprehensive overview of creating and managing user accounts in MySQL, including granting and revoking permissions.

Prerequisites/Requirements 

To follow this guide, you will need access to a MySQL database. The tutorial assumes that the database is installed on a virtual private server (VPS) running Ubuntu or Debian. However, the process for creating a new MySQL user and assigning permissions is generally similar, regardless of the underlying operating system of your server.

Creating A MySQL User 

  1. Login to the MySQL server as root:
mysql -u root -p
  1. Enter your root password when prompted.

  2. Create a new user using the CREATE USER command:

CREATE USER 'username'@'host' IDENTIFIED BY 'password';

Replace username with the desired username, host with the hostname from which the user will connect, and password with the desired password. Enclose the username and host in single quotes for consistency and to avoid potential errors.

Granting Permissions to a User 

  1. To grant permissions to a user, use the GRANT command:
GRANT PRIVILEGE ON database.table TO 'username'@'host';

Replace PRIVILEGE with the specific privilege you want to grant, database with the name of the database the privilege applies to, table with the name of the table the privilege applies to, username with the username of the user to grant the privilege to, and host with the hostname from which the user will connect.

  1. For example, to grant a user the SELECT privilege on all tables in the mydatabase database:
GRANT SELECT ON mydatabase.* TO 'username'@'localhost';
  1. To grant multiple privileges in a single command, separate them with commas:
GRANT SELECT, INSERT, UPDATE ON mydatabase.* TO 'username'@'localhost';
  1. To grant global privileges (permissions that apply to all databases and tables), use asterisks (*) instead of specific database and table names:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';
  1. After granting permissions, refresh the database to ensure the changes are applied:
FLUSH PRIVILEGES;

Revoke Permissions from a User

To revoke permissions from a user, use the REVOKE command:

REVOKE type_of_permission ON database_name.table_name FROM 'username'@'host';

Replace type_of_permission with the specific permission you want to revoke, database_name with the name of the database the permission applies to, table_name with the name of the table the permission applies to, username with the username of the user to revoke the privilege from, and host with the hostname from which the user will connect.

Other Useful MySQL Commands 

  1. Display the permissions of a user:
SHOW GRANTS FOR 'username'@'host';
  1. Delete a user:
DROP USER 'username'@'localhost';
  1. Exit MySQL:
exit;

Conclusion

This guide has provided a detailed explanation of how to create and manage user accounts in MySQL. By following these steps, you can effectively control user access and maintain the security of your database. Remember to grant users the minimum permissions necessary for them to perform their tasks and revoke permissions when they are no longer needed.

Was this article helpful?
Dislike 0
Views: 8