If you run your own LAMP stack there will probably come a day when you are ready to run more than one CMS driven website on the server. When the time comes, it becomes necessary to create a new database for each one. It is a very bad idea to provide anyone with root access to the database so creating unique users for each new database is recommended.
When I first moved from Microsoft to LAMP servers there was nothing more confusing to me than interacting with a database through shell. However, since I’ve grown used to working this way, I’ve actually come to prefer it. I also think it is an important skill for any programmer or ‘code-curious’ individual to get familiar with so we’ll approach the task this way.
Once logged into shell it will be necessary to log into MYSQL. I use the mysql command with the root user as follows:
Breaking this command down, I entered ‘sudo’ so the system recognizes me as an administrator for this command, then mysql. -u is to indicate the next string is a user and -p indicates it should prompt me for a password.
The entire exchange looks like this:
jake@Poseidon:~$ sudo mysql -u root -p
[sudo] password for jake:
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 37855
Server version: 5.1.37-1ubuntu5 (Ubuntu)
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input s
mysql>
Once successfully logged into the database there are only two things to do:
- Create a new user
- Grant the new user rights to an existing database
The first step, creating a new user is accomplished using the CREATE USER command. The syntax is as follows:
The items in capital letters are parts of the command that don’t change. The parts within the single quotes are all variables which can be changed. However, if you are working on one machine like I do, ‘localhost’ does not change. Give the new user a custom name and password and don’t forget the trailing semicolon!
The final command will give the new user rights to a specified, existing database using the GRANT command:
In this case the word database and the elements within single quotes are all variables. Database should be replaced with the name of the database you want to give the user access to. User should be replaced with the name of the user you just created and the same rules apply for localhost.
- In order to confirm the correct spelling of the database name use the command SHOW DATABASES;
- Be sure to use sufficiently complex passwords for database users. We recommend at least 8 characters in length using upper and lowercase letters, numbers and special characters.

