How to Delete a MySQL Database on Linux via Command Line

Published on

3 min read

How to Delete / Drop a MySQL Database

MySQL is the most popular open-source relational database management system.

This tutorial describes how to delete (or drop) a MySQL or MariaDB database through the command line.

Before you begin

All commands are executed as an administrative user (the minimum privilege required to delete a database is DROP) or with a root account.

To access the MySQL console type the following command and enter your MySQL root user password when prompted:

mysql -u root -p

If you haven’t set a password for your MySQL root user you can omit the -p switch.

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

List All MySQL Databases

Before dropping the database, you may want to view a list of all the databases you’ve created . To do so from within the MySQL shell execute the following command:

SHOW DATABASES;

The command above will print a list of all databases on the server. The output should be similar to this:

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

Delete a Database in MySQL

Deleting a MySQL database is as simple as running a single command. This is a non-reversible action and should be executed with caution. Make sure that you are not removing a wrong database, as once you delete the database it cannot be recovered.

It is always a good idea to create a backup of the database before running the drop query.

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

DROP DATABASE database_name;
Query OK, 1 row affected (0.00 sec)

If you try to delete a database that doesn’t exist you will see the following error message:

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

To avoid seeing errors as above, use the following command instead:

DROP DATABASE IF EXISTS database_name;
Query OK, 1 row affected, 1 warning (0.00 sec)

In the output above, Query OK means that the query was successful, and 1 warning tells us that the database doesn’t exists and no database was deleted.

On Linux, MySQL database and table names are case sensitive.

Delete a MySQL Database with mysqladmin

You can also delete a MySQL database from the Linux terminal by using the mysqladmin utility.

For example, to delete a database named database_name, type the following command and enter your MySQL root user password when prompted:

mysqladmin -u root -p drop database_name

Conclusion

You have learned how to delete a MySQL database.

Feel free to leave a comment if you have any questions.