Delete MySQL database using command line

02-17-2024

Before starting, we need to ensure that we have MySQL administrator privileges and root access privileges, as well as backup and confirm that deleted database data cannot be restored.

The following are the steps to delete a MySQL database:


Step 1: Connect to the MySQL database server

To connect to the MySQL server, we need to use the MySQL command executable file and the credentials of the root user. Type the following command in the terminal:

mysql -u root -p

This will start the MySQL server and log in as root. Enter the root user password to complete the login.

Step 2: List the existing databases

Once we successfully log in to the MySQL server, at the MySQL command prompt, we can list all existing databases using the following command:

SHOW DATABASES;

When we press the enter key, the system will display all existing databases on the command line.


Step 3: Select the database to be deleted

To delete a database, we need to use the DROP DATABASE command, but before executing it, we need to determine the name of the database to be deleted. Select the database to be deleted using the following command:

USE database_name;

In this command, replace "databasename" with the actual database name to be deleted. This will make the selection database active, allowing the DROP DATABASE command to be executed.


Step 4: Delete the database

Once we have selected the database to be deleted, we can use the following command to delete it:

DROP DATABASE database_name;

After executing this command, MySQL will delete the specified database and all its contents from the server. Please note that the deletion operation is irreversible, so be sure to backup before executing DROP DATABASE.


Step 5: Exit MySQL

After deleting the required database, we can exit the MySQL server. At the MySQL command line, enter the following command:

EXIT;

After completion, we will be taken back to the terminal prompt.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us

high perspicacity