Create a Database User and Give Permissions

Asif Ahmed

Suppose you have a web server and have several different websites running on it. You most likely have a different database for each website and if you don’t you definitely should! Each database should grant access to one or multiple users. However each of these users should be specific to this database. One user should not be able to access more than one database. This is a security issue. If someone learns the password of one of your database users then they shouldn’t have access to multiple databases (theoretically). If your compromised user is the root user then you are in deep trouble anyways. Therefore let’s cover how to create a database, how to create a database user, and how to give permissions to that user. For my purposes I will be using MySQL.

First login to mysql via command line.

mysql -u root -p

Here is how you would create a database.

create database example_name

Here is how you would create a user.

create user 'example_user'@'localhost' IDENTIFIED BY 'example_password';

Then you give this example_user an all access pass to the example_name database you just created.

grant all privileges on example_name.* to example_user@localhost;

This is how you can break down the process into steps. If you want to do this all in one line, you can check out the Stack Overflow link below.

Here is the Stack Overflow link.