How to Create a MySQL Database via Command Line

By Raju

December 19, 2016

create a database, database, MySQL

In this tutorial you are going to learn how to create a MySQL database via command line on Linux server. You'll learn basic MySQL database operations through below steps.

Let's get straight to the point.

How to access MySQL command line or shell?

If MySQL server already installed on your server, then you can access it through shell or command line.

First, login to the server as a root and then enter below command to access MySQL shell interface.

You can use any of the below commands to access MySQL command line.

mysql
mysql -u root -p

In the above command u flag indicates root user, and -p flag for password authentication.

You have to enter password to access MySQL.

mysql>

Now, you are ready to create a database!

How to create MySQL Database?

Let’s create a sample database named "sampledb" with below command.

Tip: All database and table names under MySQL are case sensitive so use lowercase letters. It’s always better to follow best practices for various reasons.

 mysql> CREATE DATABASE sampledb; 

You are going to see following output when you execute above command.

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

Note: You may receive error if you try to create a database which is already exists. Then, you can chose different name for your database.

 mysql> CREATE DATABASE sampledb; 
ERROR 1007 (HY000): Can't create database 'sampledb'; database exists

Show MySQL Databases

Now, let’s list out all the databases available on the server using below command.

  mysql> SHOW DATABASES;  

You may see following output on screen with database list including your database.

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sampledb               |
+--------------------+
4 rows in set (0.01 sec) 

Conclusion

In this article, we covered basic MySQL commands to create a database through command line tool. As you have seen, it's pretty easy to create database or execute commands in MySQL rather than using GUI tools.

​We really hope that the above article would be useful to you. Feel free to give your valuable feedback via below comment box. Cheers!

Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Create a website in 3 simple steps

Choose a website template, add features, then customise! - Free Online Website builder.