Create a MySQL Database on Linux via Command Line

Posted on by Ronald Caldwell | Updated:
Reading Time: 2 minutes

While the are many ways to create databases, system administrators benefit from knowing how to create a MySQL database in Linux via the command line. Knowing more than one way to accomplish a task provides flexibility in case the usual way you perform it is not an option. Use this tutorial to create a MySQL database via the command line.

Prerequisites

  • Server running CentOS or AlmaLinux.
  • Root-level access to the server.
Create a MySQL Database on Linux via Command Line

Create MySQL Database on Linux using Command Line

Step 1:

Log into the MySQL server from the command line with the following command, specifying the user root with the -u flag, and prompt for a password using the -p flag. Enter your current password once prompted to complete the login.

mysql -u root -p

You can change the MySQL password for the root user or any other user in the database via the command line.

A prompt displays like the one below once you log in.

mysql>

Step 2:

To create a database with the name tutorial_database, type the following command.

CREATE DATABASE tutorial_database;

If a database of the same name already exists, the system will not create a new database, and you will receive this error.

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

You can avoid this error by using the following command. It only creates the database tutorial_database if a database of that name does not already exist.

CREATE DATABASE IF NOT EXISTS tutorial_database;

View All MySQL Databases

Use the following command to view the database you've created.

SHOW DATABASES;

Here is the resulting output.

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
| tutorial_database  |
+--------------------+
4 rows in set (0.00 sec)

Wrapping Up

Creating a MySQL database from the Linux command line is an easy process. System administrators would do well to have this process in their toolkit to add flexibility to their workflow.

Are you looking for a server solution that includes MySQL out of the box? Liquid Web offers VPS Hosting, Cloud Dedicated Servers, and Dedicated Servers with the LAMP stack built in. Contact our sales team today to start or migrate your project.

Series Navigation
<< Previous ArticleNext Article >>
Avatar for Ronald Caldwell

About the Author: Ronald Caldwell

Ron is a Technical Writer at Liquid Web working with the Marketing team. He has 9+ years of experience in Technology. He obtained an Associate of Science in Computer Science from Prairie State College in 2015. He is happily married to his high school sweetheart and lives in Michigan with her and their children.

Latest Articles

Onboard to Liquid Web: A Step-By-Step Guide to Migrating Your Data to a Virtual Server

Read Article

Onboard to Liquid Web: A Step-By-Step Guide to Migrating Your Data to a Virtual Server

Read Article

Onboard to Liquid Web: A Step-By-Step Guide to Migrating Your Data to a Virtual Server

Read Article

Best Practices For Changing Your Email Marketing Provider

Read Article

Best Practices For Changing Your Email Marketing Provider

Read Article