Knowledgebase

How to Create a Database in MySQL With Command Line?

MySQL is one of the most widely used database management systems, powering everything from small websites to enterprise applications. In that, creating a database is the first and most crucial step in organising and storing data efficiently. Whether you are setting up a new project, managing an existing application, or working on a remote server, knowing how to create a MySQL database is essential.

While many people prefer using graphical tools like phpMyAdmin or MySQL Workbench to manage databases, there are situations where using the command line is more efficient and even necessary.


Also Read: How to Create a MySQL Database By Using cPanel?


Scenarios like:

  • When you are working with cloud servers or VPS, GUI-based tools may not be available. 
  • The best part is that the command line allows you to automate database tasks through scripts. 
  • Some tasks execute faster through direct command execution rather than clicking through a UI.
  • Command-line tools are lightweight and do not require additional software.

If you’re comfortable with basic Linux commands, using MySQL through the terminal is straightforward. Let’s walk through the steps to create a MySQL database using the command line!

Before we get started to create a MySQL database from the Command line, let’s know its prerequisites:


Also Read: How to Create a Database in the Plesk Panel?


Prerequisites to Create a MySQL Database From the Command Line

Before creating a database, you need to ensure that:

  • You have MySQL installed on your system.
  • You have access to a MySQL user account with privileges to create databases.

Also Read: How to export/import a MySQL database via SSH


Steps to Create a MySQL Database From the Command Line

➔ If your MySQL is not installed, you need to install it by using the following command:

On Ubuntu/Debian:

sudo apt update
sudo apt install mysql-server

➔ After installation, start and secure MySQL by using the below command:

sudo systemctl start mysql
sudo mysql_secure_installation

On CentOS/RHEL:

sudo yum install mysql-server
sudo systemctl start mysqld

➔ Secure MySQL with the following command:

sudo mysql_secure_installation

On macOS:

In macOS, you have to use Homebrew to install MySQL:

brew install mysql
brew services start mysql

On Windows:

For Windows, you have to download the MySQL installer from the official MySQL website and follow the setup wizard.

Once your MySQL has been installed, it’s time to create a database. Let’s get started!

➔ To start using MySQL, you have to open the terminal or command prompt and log in with your MySQL user account by adding the following command.

On Linux/macOS:

sudo mysql -u root -p

On Windows (Command Prompt):

mysql -u root -p

After entering the correct password, you will see the MySQL command prompt:

mysql>

Also Read: How to reset the password for a MySQL database?


➔ The time has come to create a database. For this, you have to use the following command to create a database:

CREATE DATABASE mydatabase;

You need to replace it with ‘mydatabase’with your database name.

For example, If you are creating a database for an eCommerce project, you might name it ecommerce_db. In that case, your command will be:

CREATE DATABASE ecommerce_db;

➔ Once the database is created, you need to verify that it was created. For this, you have to use the following command:

SHOW DATABASES;

➔ This will list all databases, and you should see your new database in the list. It will look like this:

+ ————————————————— +
| Database          |
+ ————————————————— +
| information_schema|
| ecommerce_db      |
| mysql             |
| performance_schema|
| sys               |
+ ————————————————— +

➔ To start using the newly created database, select it by using the following command:

USE ecommerce_db;

That’s it; you have successfully created a database in MySQL with the command line. Now, any tables you create will be part of this database.


Also Read: How to Create a Table in MySQL?


Conclusion

Creating a database in MySQL using the command line is an essential skill for developers and system administrators. Whether you are working on a local machine or managing a cloud server, the command line provides a fast, efficient, and flexible way to handle databases.

Now, you are ready to store and manage data efficiently in MySQL!

Happy Coding!