Create MySQL Database, User And Password On The Terminal

back to tech articles
MySQL terminal

We are going to create a new database, user and password and grant that user access to the new database. Let’s first login to MySQL in the terminal.

1
$ mysql -uadmin -ppassword

Hit return and that will log you in, provided your credentials are valid! Note that there is no gap after the -p password switch. That is intentional, because otherwise MySQL would read the space as part of the password.

You don’t need to remove the space after the -u username switch, but I always do. As a side note, removing the space after the database selector switch (-B) will cause MySQL to look for the wrong database and fail.

Let us see what databases already exist.

1
2
3
4
5
6
7
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+

Ok, let us first create the new database.

1
2
mysql> CREATE DATABASE awesome_sauces;
Query OK, 1 row affected (0.00 sec)

List our databases again, and you will see the addition.

1
2
3
4
5
6
7
8
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| awesome_sauces     |
| information_schema |
| mysql              |
+--------------------+

Now we will need a user for this database, I guess.

1
2
mysql> CREATE USER 'saucemaker'@'localhost' IDENTIFIED BY 'aw3s0m3';
Query OK, 0 rows affected (0.00 sec)

The IDENTIFIED BY part specifies the password this user will have. Next we need to grant permissions to this user on our new database.

1
2
mysql> GRANT ALL PRIVILEGES ON awesome_sauces.* TO 'saucemaker'@'localhost';
Query OK, 0 rows affected (0.00 sec)

That is all we need. Now we can flush the privileges and go home.

1
2
3
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit;

All done.

Now you can populate your config file and away you go.

Leave a Reply

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