How to create MySQL Database and User

This lesson is short and to the point. I don’t use MySQL a lot but when I do I always forget the exact syntax to create a database and grant permissions to a username.

First we need to login at MySQL:

# mysql -u root -p
Enter password:

We can create a database like this:

create database MYDATABASE;

Next we will create a username and ensure it is allowed to connect to MySQL:

mysql> grant usage on *.* to MYUSER@localhost identified by 'MYPASSWORD';
Query OK, 0 rows affected (0.00 sec)

The final step is to allow the user all permissions for the database we just created:

mysql> grant all privileges on MYDATABASE.* to MYUSER@localhost ;
Query OK, 0 rows affected (0.00 sec)

In case you want to delete a database or username, this is how to do it:

mysql> drop database MYDATABASE ;
Query OK, 0 rows affected (0.00 sec)

This will remove the database and here’s how to delete the user:

mysql> drop user MYUSER@localhost ;
Query OK, 0 rows affected (0.00 sec)

If this has been useful to you.