next up previous contents
Next: 實機操作練習題 Up: Database 資料庫 Previous: 選取工作資料庫   Contents   DYWANG_HOME

刪除資料庫

  1. 文字命令列直接刪除資料庫 dyw。
    [root@kvm8 ~]# mysqladmin -uroot -p123qwe drop dyw
    Dropping the database is potentially a very bad thing to do.
    Any data stored in the database will be destroyed.
    
    Do you really want to drop the 'dyw' database [y/N] y
    Database "dyw" dropped
    
  2. 使用 php 刪除資料庫 dywphp。
    [root@kvm8 ~]# vim /var/www/html/dropdb.php
    [root@kvm8 ~]# cat /var/www/html/dropdb.php
    <?php
    $dbhost = 'localhost:3306';
    $dbuser = 'root';
    $dbpass = '123qwe';
    $conn = mysqli_connect($dbhost, $dbuser, $dbpass)
            or die(mysqli_connect_error().PHP_EOL);
    echo 'Connected successfully'.PHP_EOL;
    $sql = 'DROP DATABASE dywphp';
    mysqli_query( $conn, $sql )
            or die('Error: '.mysqli_error($conn).PHP_EOL);
    echo "Database dywphp deleted successfully\n";
    mysqli_close($conn);
    ?>
    
  3. 以 curl 連線 dropdb.php,回應連線成功且刪除資料庫 dywphp。
    [root@kvm8 ~]# curl -s http://127.0.0.1/dropdb.php
    Connected successfully
    Database dywphp deleted successfully
    
  4. 再以 curl 連線 dropdb.php,回應連線成功但資料庫 dywphp 不存在。
    [root@kvm8 ~]# curl -s http://127.0.0.1/dropdb.php
    Connected successfully
    Error: Can't drop database 'dywphp'; database doesn't exist
    
  5. 直接在命令列查詢 mysql 中的資料庫,dyw 及 dywphp 兩個資料庫及不見。
    [root@kvm8 html]# mysql -uroot -p123qwe -e "show databases"
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    



De-Yu Wang 2020-04-07