Next Series:
MySQL via Command Line 102: Basic User Interaction
Knowing how to delete a MySQL or MariaDB database via command line comes in handy. System administrators may need to delete databases when taking care of their tasks. It is a useful skill, and this tutorial shows you how to do it.
Prerequisites
- Server running CentOS or AlmaLinux with MySQL or MariaDB.
- Root access to the server and log in as root.
- Create a database if one does not exist.
Video by Andrew Casares
How to Delete a MySQL or MariaDB Database via Command Line
Step 1: Log into the MySQL Server
The following command allows logging in to the MySQL server from the command line as root. It specifies the root user with the -u flag and uses the -p flag to prompt for a password.
mysql -u root -pEnter your current password to log in. You can change a password for MySQL via the command line also, if necessary.
Once complete, you reach a MySQL prompt.
mysql>Step 2: Delete a Database in MySQL or MariaDB
Note: Removing a MySQL or MariaDB database cannot be undone.
The following command deletes your database in MySQL. For this tutorial, the command removes the database named tutorial_database.
DROP DATABASE tutorial_database;The following error appears only if a database with the name tutorial_database does not exist.
ERROR 1008 (HY000): Can't drop database 'tutorial_database'; database doesn't existA way to avoid seeing this error is using the following syntax, which removes the database only if it exists.
DROP DATABASE IF EXISTS tutorial_database;Step 3: View All MySQL Databases
To view a list of databases, issue the following command.
SHOW DATABASES;The following output displays.
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)Wrapping Up
This tutorial and video covers how to view and delete a MySQL database from the command line in Linux. It is useful for removing databases that are no longer in use.
Liquid Web offers Linux servers via our VPS hosting, dedicated cloud servers, and dedicated servers. Contact our sales team today to set up your server.
Ronald Caldwell