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.
- Server running CentOS or AlmaLinux.
- Root-level access to the server.
Create MySQL Database on Linux using Command Line
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.
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.
Here is the resulting output.
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | tutorial_database | +--------------------+ 4 rows in set (0.00 sec)
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.
- 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
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.