In this gude, we are going to see how to manage user and database permissions in mysql, i.e. Creating mysql database, creating mysql user, granting user access to mysql databases, updating user permissions and deleting user and database when no longer needed.
MySQL is the most popular open-source relational database management system.
Mysql is commonly installed as part of the popular LAMP or LEMP (Linux, Apache/Nginx, MySQL/MariaDB, PHP/Python/Perl) stack. It implements the relational model and Structured Query Language (SQL) to manage and query data.
MySQL server allows us to create numerous user accounts and grant appropriate privileges so that the users can access and manage databases.
Related Content:
# Prerequisites
- Admin access to mysql server
Note: All commands in this guide should be executed inside the MySQL shell as root
or as a user with administrative privileges.
To login to mysql shell, use the following command. Subtitute 127.0.0.1
with your database hostname and root
with the admin username:
|
|
You should see an output similar to this:
|
|
# Creating mysql database
In the mysql shell, execute this command to create a database
|
|
Example output:
|
|
# Create a new MySQL User Account
To connect to mysql, we will need a user and a password. Create a user with a password using this command:
|
|
Example output:
|
|
# Grant Privileges to a MySQL User Account
A number of mysql provileges can be granted. Check them here. Common privileges:
ALL PRIVILEGES
- Grants all privilegesCREATE
- Create databases and tablesDROP
- Drop databases and tablesDELETE
- Delete rows from a specific tableINSERT
- Insert rows into a specific tableSELECT
- Read databaseUPDATE
- Update table rows
When creating a user, you can either grant them a single permission, multiple permissions as a comma separated list or all permissions using the ALL PRIVILEGES
keyword.
This is the command format:
|
|
Example Output:
|
|
# Flush privileges
To reload the changes done when creating the user and applying them permissions, use this command:
|
|
Output:
|
|
# Checking users in the system
Once the user is created, you can connect using that user using this command:
|
|
Example Output:
|
|
To get a list of users in the system, use this command:
|
|
Sample Output:
|
|
# Checking user permissions
To check users permissions for mysql users, use the following commannds: For the currentlly logged in user:
|
|
For another user:
|
|
Sample output:
|
|
# Revoke Privileges from a MySQL User Account
To revoke all privileges from a user account over a specific database, run the following command:
|
|
Example output:
|
|
# Remove an Existing MySQL User Account
To delete a MySQL user account run this command:
|
|
Example output:
|
|
# Removing database
To delete a database, use this command:
|
|
Example output:
|
|
# Conclusion
This guide described how we would go about managing users and databases in mysql.
To summarize, the following are the commands used to create database, user and grant permissions:
|
|
These are the commands used to revoke user access, delete user and delete database:
|
|