Create database user and assign it’s access to user in MySQL

If you want to create different users for your different database due to security reasons then this article is for you. In MySQL we can grant access/privileges of database to user as well as revoke or even delete user too.
We can also assign full access to any user for any database by using below snippet. If you are not on localhost then you can also use wild char % instead of localhost. Replace db_user/db_pass with your data.

CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'db_pass';
GRANT ALL PRIVILEGES ON * . * TO 'db_user'@'localhost';
FLUSH PRIVILEGES;

If you want to assign access to only some database only all then use below snippet.

CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'db_pass';
GRANT ALL PRIVILEGES ON db_name . * TO 'db_user'@'localhost';
FLUSH PRIVILEGES;

For limited access use below snippet.

CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'db_pass';
GRANT INSERT, UPDATE, DELETE PRIVILEGES ON db_name . * TO 'db_user'@'localhost';
FLUSH PRIVILEGES;

Leave a Reply

Your email address will not be published. Required fields are marked *