How to Create MySQL Database WordPress via Command Line

How to Create MySQL Database WordPress via Command LineWhen installing WordPress on a remote or local server, you need to create a MySQL database.  You can create a WordPress database using the PhpMyAdmin but there is another way to create WordPress MySQL database using the terminal. In this quick tutorial, I will explore how to create WordPress database via command line in a step by step guide. It is important that every WordPress developer learns how to create WordPress database on CLI since you can work on a server environment that does not have the PhpMyAdmin or MySQL database creation wizard.

Creating the database through the command line is one of the best ways to create a WordPress database. In this tutorial, I will use my localhost XAMPP to demonstrate how to create MySQL database WordPress through the CLI.

These steps can be repeated to create a WordPress database on a hosting remote environment through SSH access.

Start Your Local Server

The first step is to ensure your MySQL is running on the localhost. For XAMPP you can quickly start and stop MySQL from the XAMPP graphical user interface as shown on the image below:

How to Create a MySQL Database for WordPress Using the Command Line

You can use any other localhost server that you are running but you follow the same steps. Lately, I have moved from XAMPP to Laragon a localhost environment that I find to be faster and better than XAMPP. Nonetheless, let us create the WordPress database via CLI and test it.

Create MySQL WordPress Database Command Line

Open the command prompt if you are on Windows or the Terminal if you are in other operating systems. On Windows you can access the command prompt by typing CMD on the search bar:

MySQL on CLI

When the command line is running type MySQL without space and hit enter and you should see the MySQL welcome message as shown in the image below:

Create MySQL Database WordPress via Command Line

Locate MySQL Bin Folder

Depending on where you have installed your MySQL you should navigate to that folder using the command prompt so that you can log in and begin creating your databases. In my case the folder is located in drive C where Xampp is installed and the path as shown in the image below:

Create MySQL Database WordPress via Command Line

So we need to navigate to this directory on the command line, you can use this command cd / to move to the root then use cd command to navigate to the BIN folder as shown below :

How to Create MySQL Database WordPress via Command Line

Connect to MySQL Database via Command Line

Now we need to connect to the MySQL database so that we can begin creating the WordPress database via command line. To connect we need to have the password and the user name and we should begin by typing the following line:

mysql –u root –p –h 127.0.0.1

The -u stands for the username and in this case, its root and the –p stand for the password which for my case it’s empty and the –h stands for the host which in this case is the localhost which is the same as the number 127.0.0.1. When you press enter you will be prompted to add the password as shown below:

create wordpress database via command line

If you have default installation that requires no password just press enter again and you will see MySQL welcome message as shown below:

If you have passwords in your installation type the password and press enter to move to the next step. Now we are ready to begin creating WordPress database via command line.

Creating WordPress Database via Command Line or MySQL Database for Web Applications

Now we just need to use the basic SQL create database syntax to create the database. Before we create the database it may be helpful to first check out what other databases are already created. We should use the show databases; command as shown below:

How to Create MySQL Database WordPress via Command Line

As you can see it in the image above the command will display all the databases that have been created before and you can now create the database with the create database; command as follows:

Create MySQL Database WordPress via Command Line

I have created the database with the name wpexample , as you can see the message Query OK meaning the database has been created successfully. You can now go into that database using the command use;

use database

Finally, if you just want to confirm the database exists, you can check and see the database via the PHPMyAdmin and begin installing WordPress using this database.

How to Create MySQL Database WordPress via Command Line

Final Thoughts

Creating WordPress database via command line is rather a cumbersome procedure for beginners but a professional WordPress developer should know how to create the database via CLI. It is helpful to also know some other ways of using the command line with MySQL databases since it can greatly improve WordPress development productivity.

Comments are closed.