Jan 12, 2023 2 min read

How to Delete a MySQL Database on Linux from the Command Line

In this tutorial, you will delete (or drop) a MySQL or MariaDB database through the command line.

How to Delete a MySQL Database on Linux from the Command Line
Table of Contents

Introduction

MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used in web applications due to its ease of use and fast performance.

MySQL is used to store, organize, and retrieve data for a variety of applications. It is the underlying technology for many web-based applications, including content management systems (CMS), ecommerce websites, and online forums. MySQL can be used on many platforms, including Linux, Windows, and Mac OS X.

In this tutorial, you will delete MySQL Database on Linux from the Command Line.

Before you begin

The root account or an administrative user (the minimum privilege required to delete a database is DROP) is used to execute all commands.

When prompted, type the following command to access the MySQL console and enter your MySQL root user password:

mysql -u root -p

You can omit the -p switch if you haven't specified a password for your MySQL root user.

👉
If you need to reset your MySQL root password, follow this tutorial on resetting a MySQL root password via the command line.

List of All MySQL Databases

You might want to view a list of all the databases you have created before dropping the database. Use the following command from within the MySQL shell to accomplish this:

SHOW DATABASES;

The above command will print a list of all the available databases on the server. The output should resemble the following:

Output
+--------------------+
| Database           |
+--------------------+
| information_schema |
| database_name      |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

Delete a Database in MySQL

Running a single command is all that is required to delete a MySQL database. Since this is an irreversible action, it should be carried out carefully. If you delete a database, it cannot be retrieved, so be careful not to remove the wrong one.

⚠️
Before executing the drop query, it is advisable to create a backup of the database.


Type the following command to delete a database, where database_name is the name of the database you want to delete:

DROP DATABASE database_name;
Output

Query OK, 1 row affected (0.00 sec)

The following error message may appear if you attempt to delete a database that does not exist:

Output

ERROR 1008 (HY000): Can't drop database 'database_name'; database doesn't exist

Use the following command instead to prevent seeing the above errors:

DROP DATABASE IF EXISTS database_name;
Output

Query OK, 1 row affected, 1 warning (0.00 sec)

Query OK indicates that the query was successful, and 1 warning indicates that the database does not exist and that no databases were deleted.

ℹ️
MySQL database and table names are case-sensitive on Linux.

Delete a MySQL Database with mysqladmin

Using the mysqladmin utility, you can also delete a MySQL database from the Linux terminal.

To delete a database named database_name, for example, run the command below and your MySQL root user password when prompted:

mysqladmin -u root -p drop database_name

Conclusion

You now understand how to delete a MySQL database.

If you have any queries, feel free to leave a comment below, and we'll be happy to help.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.