MYSQL is a Relational DBS used to manage data in databases. While using MySQL, the data (stored in a database) or the whole MySQL database is no longer needed. It is recommended to delete that specific database to vacate some space on the server.
This post will address the most feasible approaches to deleting MySQL databases on Linux systems through commands. The guideline of this article is as follows:
Prerequisites: Check the MySQL Database Status
Before moving on to the deletion of the “MySQL” database, check that the “MySQL” database is active on the current system. For this purpose, enter the “systemctl status” command on the terminal:
$ sudo systemctl status mysql
The output confirms that the “MySQL” services are in an “active” state in the current Linux distribution Ubuntu 22.04.
If it is not active, use the below command to activate or start its service:
$ sudo systemctl start mysql
How to Delete a MySQL Database Using Terminal?
Linux OS is well-known for its effective command line functionalities, and the whole system can easily be managed through commands. Considering MySQL in today’s guide, the following steps are carried out to delete a MySQL database without affecting the overall working of the system:
Step 1: Access the MySQL Console as a Root
First, access the MySQL account as a root user using the command as follows:
$ sudo mysql -u root -p
After that, you would be inside the MySQL shell, where you can execute the MySQL queries or commands.
Step 2: Show the MySQL Databases
In the “MySQL” database console, execute the following command to open all the existing databases:
> SHOW DATABASE;
Step 3: Delete the MySQL Database
To delete the “MySQL” database on Linux below mentioned syntax is to be followed:
> DROP DATABASE database_name;
In the syntax, the “DROP DATABASE” is the main keyword while the “database_name” represents the specific database that you want to delete or drop.
Example 1: Delete the Existing MySQL Database
After checking all the existing databases, choose the database you want to delete from the system. For example, we selected the “db_employees” database and used the command on the “mysql” shell to delete it:
> DROP DATABASE db_employees;
The output displays that “db_employees” has been deleted without affecting any row.
Otherwise, run the “SHOW DATABASE” command again for verification.
Example 2: Check and Delete the Non-Existent MySQL Database
Sometimes, when the user uses the delete the “ DROP DATABASE” command in the “mysql” shell, it generates an error as shown below screenshot:
The reason for this error is that the selected database is not available in the current system. Then it is recommended that use the “IF EXISTS” condition with the “DROP” command to delete the database on Linux if it exists:
> DROP DATABASE IF EXISTS db_student;
The output shows the warning that the “db_student” is not present in the system. So it can’t be deleted or dropped.
In Linux, the “MySQL” database can be deleted by executing the “DROP DATABASE database_name;” command in the “MySQL” console. If the database that you want to delete is not available, then the above command will generate an error. To resolve this error, use the “DROP DATABASE IF EXISTS databse_name;” command instead of the first one. You have learned the possible ways to delete the database in MySQL.