Create a MySQL Database on Linux via Command Line
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 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.
Related Articles:
- Using MySQL Command Line to Create a User
- How to Use Disk Quotas in Dedicated Linux Servers With cPanel
- How to Use Disk Quotas in Dedicated Linux Servers with Plesk
- Remove a MySQL User on Linux via Command Line
- Remove Permissions for a MySQL User on Linux via Command Line
- Grant Permissions to a MySQL User on Linux via Command Line

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.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
Latest Articles
2024 cPanel and Plesk pricing breakdown
Read ArticleCentOS Linux 7 EOL — everything you need to know
Read ArticleHow to install Node.js on Linux (AlmaLinux)
Read ArticleUpgrading or installing PHP on Ubuntu — PHP 7.2 and Ubuntu 22.04
Read ArticleWhy is your IP blocked?
Read Article