How to Delete a MySQL Database and User via Command Line in Linux

Login MySQL.
sudo mysql -u root -p

Deleting a MySQL database.

Identify the MySQL database.
You can list all available databases by using the following command.
mysql> SHOW DATABASES;

The output should be something like this.

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

Note the name of the database that you want to delete from the displayed list.

Delete the database.
You can now, delete that particular database by using the following command.
mysql> DROP DATABASE database_name;
Make sure you replace the database_name with the name of the database that you want to delete.

The output should be something like this:
Query OK, 0 rows affected (0.00 sec)

Deleting a MySQL user.

Identify the MySQL user.
Type the following command to list all MySQL users.
mysql> SELECT User FROM mysql.user;

The output should be something like this:

+-------+
| User |
+-------+
| root |
+-------+
| newuser |
+-------+

Delete the MySQL user.
You can delete a MySQL user with the following command.
mysql> DROP USER 'newuser'@'localhost';
Make sure to replace newuser with the name of the user that you want to delete.

The output should be something like this:
Query OK, 0 rows affected (0.01 sec)

Leave a Reply

Your email address will not be published. Required fields are marked *