How to Create and Select MySQL Databases

Updated on

3 min read

Create and Select a MySQL Database

MySQL is the most popular open-source relational database management system.

This tutorial explains how to create MySQL or MariaDB databases through the command line.

Before you begin

We are assuming that you already have MySQL or MariaDB server installed on your system.

All commands are executed as an administrative user (the minimum privilege required to create a new database is CREATE) or with a root account.

To access the MySQL shell type the following command and enter your MySQL root user password when prompted:

mysql -u root -p

If you haven’t set a password for your MySQL root user, you can omit the -p option.

If you need to change your MySQL root password, follow this tutorial on resetting a MySQL root password via the command line.

Create a MySQL Database

Creating a new MySQL database is as simple as running a single command.

To create a new MySQL or MariaDB database issue the following command, where database_name is the name of the database you want to create:

CREATE DATABASE database_name;
Query OK, 1 row affected (0.00 sec)

If you try to create a database that already exists, you will see the following error message:

ERROR 1007 (HY000): Can't create database 'database_name'; database exists

To avoid errors if the database with the same name as you are trying to create exists, use the IF NOT EXISTS statement:

Query OK, 1 row affected, 1 warning (0.00 sec)

In the output above, Query OK means that the query was successful, and 1 warning tells us that the database already exists, and no new database was created.

On Linux, MySQL database and table names are case sensitive.

View All MySQL Databases

To view the database you’ve created, from within the MySQL shell, execute the following command:


The command above will print a list of all databases on the server. The output should be similar to this:

| Database           |
| information_schema |
| database_name      |
| mysql              |
| performance_schema |
| test               |
5 rows in set (0.00 sec)

Select a MySQL Database

When you create a database, the new database is not selected for use.

To select a database before you begin a MySQL session, use the following statement:

USE database_name;
Database changed

Once you select a database, all the subsequent operations, such as creating tables, are performed on the selected database.

Each time you want to work on a database, you must select it with the USE statement.

You can also select the database when connecting to the MySQL server by appending the name of the database at the end of the command:

mysql -u root -p database_name

Create a MySQL Database with mysqladmin

You can also use the mysqladmin utility to create a new MySQL database from the Linux terminal.

For example, to create a database named database_name, you would use the following command:

mysqladmin -u root -p create database_name


We have shown you how to create and select MySQL databases using the MySQL shell and mysqladmin command.

Feel free to leave a comment if you have any questions.