Linux Commands

How to Create a MySQL Database from the Command Line – Linux

The MySQL is a relational database management system that is used by many developers and organizations to manage large amounts of data. In this article, we will discuss how to create a database in MySQL from the command line on a Linux system.

How to Create a MySQL Database from the Command Line

Step 1: Before creating a MySQL database, you need to ensure that MySQL is installed on your Linux system, you can do this by running the following command:

sudo apt-get install mysql-server -y


Step 2: Once MySQL is installed, you can log in to the MySQL server using the following command:

sudo mysql -u root


Step 3: Now, to create a new database, you can use the following command:

CREATE DATABASE <database-name>


Step 4: By default, the newly created database can only be accessed by the root user and If you want to allow other users to access the database, you need to grant them permissions so to  view the databases, execute:



Step 5: Next to begin working with new database use:

USE <Database-Name>;


If you are done with doing changes to the database then to exit it use the following command:



How to Remove MySQL Database through Command Line

Now in case you need to delete a MySQL database then just use the below given syntax:

DROP DATABASE <Database-Name>;



Creating a MySQL database from the command line on a Linux system is an easy process that can be completed in a few steps. By performing the given procedure, you can easily create a new database, grant permissions, and exit MySQL. This is a useful skill for developers and system administrators who need to manage large amounts of data using MySQL.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.