總所周知,mysql下要想刪除某個庫下的某張表,只須要切換到該庫下,執行語句"drop table tablename"便可刪除!但如果該庫下有成百上千張表,要是再這樣一次次執行drop語句,就太費勁了!mysql
正確的批量刪除某個庫下的全部表的方法只需以下兩步:
1)第一步(只需將下面的"庫名"替換成實際操做中的庫名便可)
select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='庫名';
2)第二步
切換到這個庫下,把第一步的執行結果導出,而後所有執行sql
例如:
批量刪除kevin庫下的全部表vim
mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin'; +--------------------------------------+ | concat('drop table ',table_name,';') | +--------------------------------------+ | drop table haha; | #只須要複製這裏的drop語句,放在一塊兒批量粘貼執行便可!(這裏kevin庫下就2張表,若是是N張表,就執行復制->粘貼執行) | drop table heihei; | +--------------------------------------+ 2 rows in set (0.00 sec) mysql> use kevin; #切換到kevin庫下,而後執行將上面複製的drop語句,直接粘貼執行便可! Database changed mysql> drop table haha; Query OK, 0 rows affected (0.09 sec) mysql> drop table heihei; Query OK, 0 rows affected (0.08 sec)
確實採用上面直接複製拼接語句查詢出來結果的方法,在多表狀況下比較複雜,卻須要調整格式。優化方案是:將查詢結果直接導出到文件,再直接source 執行文件。作法以下:bash
1)在mysql配置文件裏添加數據導出導入權限。 [root@localhost ~]# vim /etc/my.cnf ....... [mysqld] secure_file_priv=/opt/mysql/data //表示打開mysql數據導出導入權限,且限制導出導入只能發生在/opt/mysql/data目錄下 給指定的/opt/mysql/data目錄授予sql用戶權限 [root@localhost ~]# chown -R mysql.mysql /opt/mysql 重啓mysql服務 [root@localhost ~]# /etc/init.d/mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ] 將kevin庫下的全部表導出到受權的/opt/mysql/data目錄下 mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin'; +--------------------------------------+ | concat('drop table ',table_name,';') | +--------------------------------------+ | drop table anan; | | drop table beibei; | | drop table haha; | | drop table hehe; | | drop table heihei; | | drop table huihui; | | drop table huohuo; | | drop table jiajia; | | drop table jingjing; | | drop table liuliu; | | drop table popo; | | drop table qiuqiu; | | drop table renren; | | drop table youyou; | +--------------------------------------+ 14 rows in set (0.00 sec) 如今開始批量刪除kevin庫下的全部表 [root@localhost ~]# cat /opt/mysql/data/table.txt drop table anan; drop table beibei; drop table haha; drop table hehe; drop table heihei; drop table huihui; drop table huohuo; drop table jiajia; drop table jingjing; drop table liuliu; drop table popo; drop table qiuqiu; drop table renren; drop table youyou; mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin' into outfile '/opt/mysql/data/table.txt'; Query OK, 14 rows affected (0.00 sec) mysql> source /opt/mysql/data/table.txt; Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> show tables; Empty set (0.00 sec) mysql>
==================關於Mysql數據導出導入參數secure_file_priv說明===================
MySQL 5.7版本後引入了secure_file_priv參數,這個參數用來限制數據導入和導出操做的效果,好比用來限制LOAD DATA, SELECT ... OUTFILE, and LOAD_FILE()傳到哪一個指定目錄的,這些操做須要用戶具備FILE權限。 在mysql配置文件my.cnf的[mysqld]區域下配置:
1)若是這個參數設爲空或"/",則MySQL服務容許將將數據導出到任意目錄。
2)若是這個參數設爲一個具體的目錄名,則MySQL服務只容許在此目錄中執行文件導入和導出操做。這個目錄必須存在且設置爲mysql用戶權限,MySQL服務不會建立它;
3)若是這個參數爲NULL,則MySQL服務會禁止導入和導出操做。
4)若是沒有這個參數配置,則Mysql服務默認是沒有打開這個功能,即不能進行數據導入導出操做。優化
配置示例:ui
1)容許mysql數據導入導出,且導出到任意目錄(注意導出到的目錄權限要是mysql.mysql) [root@localhost ~]# vim /usr/local/my.cnf [mysqld] ...... secure_file_priv= //或者配置成secure_file_priv="/" [root@localhost ~]# /etc/init.d/mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ] 好比導出到/mnt/data目錄下,則 [root@localhost ~]# mkdir /mnt/data [root@localhost ~]# chown -R mysql.mysql /mnt/data/ 登陸mysql查看secure-file-priv參數的值: mysql> select version(); +------------+ | version() | +------------+ | 5.6.39-log | +------------+ 1 row in set (0.02 sec) mysql> show global variables like '%secure%'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | secure_auth | OFF | | secure_file_priv | / | +------------------+-------+ 2 rows in set (0.00 sec) 導出到指定的/mnt/data/data.txt文件中,該文件不能是已存在狀態,mysql數據導出的時候會自動建立該文件。 [root@localhost ~]# mysql -p123456 mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin' into outfile '/mnt/data/table.txt'; 刪除kevin庫下的全部表 mysql> use kevin; mysql> source /mnt/data/table.txt; mysql> show tables; //查詢kevin庫,發現全部表已經被刪除。 2)若是容許mysql數據導出導入,並限制導出到指定的/opt/kevin/data下,則配置: [root@localhost ~]# vim /usr/local/my.cnf [mysqld] ...... secure_file_priv= /opt/kevin/data [root@localhost ~]# mkdir -p /opt/kevin/data [root@localhost ~]# chown -R mysql.mysql /opt/kevin/data 3)禁止mysql服務進行數據導出導入操做。 [root@localhost ~]# vim /usr/local/my.cnf [mysqld] ...... secure_file_priv= NULL [root@localhost ~]# mkdir -p /opt/kevin/data [root@localhost ~]# chown -R mysql.mysql /opt/kevin/data mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin' into outfile '/opt/data/table.txt'; ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement mysql> 4)若是my.cnf裏沒有secure_file_priv這個參數配置,則默認是關閉這個功能,即不容許數據導入導出操做。 mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin' into outfile '/opt/data/table.txt'; ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement