How to delete MySQL database through command line ?

Delete/Drop MySQL database through command line.

  1. Bhawani Singh
    Sometimes we need to delete a MySQL database through command line. Before deleting a database you must check these points like you must have root access, before use DROP DATABASE you should check you have DROP privilages or not.

    First, we will login to the MySQL server through command line :
    -----
    # mysql -u root -p ******
    -----
    Here, -u option is for username and -p option is for password.

    Now, you will be at MySQL prompt and it looks like this :
    -----
    # mysql>
    -----

    Now, to delete a database from server you have to run this command :
    -----
    # mysql> DROP DATABASE datbase-name; //Syntax
    # mysql> DROP DATABASE example_database; //Example

    -----

    If database is not present on the server it will shows an error like this :
    -----
    ERROR 1008 (HY000): Can't drop database 'example_database'; database doesn't exist
    -----

    To avoid this message you can use this command :
    -----
    # mysql> DROP DATABASE IF EXISTS datbase-name; //Syntax
    # mysql> DROP DATABASE IF EXISTS example_database; //Example

    -----

    To List all MySQL databases you can check our this article too.