mysql單表備份語句
-
mysql單表備份
SELECT CONCAT("mysqldump -uroot -p123456 ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql") FROM information_schema.tables WHERE table_schema NOT IN('sys','performance','information_schema') INTO OUTFILE '/tmp/bak.sh';
INTO COUTFILE '/tmp.bak.sh'; --將查詢結果輸出保存到一個文件中
FIELDS TERMINATED BY "," ENCLOSED BY '"'; -- 以逗號分割,引號包裹
多表備份
mysqldump -rp2p_sit -p --database p2p_sit --tables tablename1 tablename2 > ~/p2p_init.sql
-
查詢整個數據庫中所有的庫對應的表明
select table_schema, table_name from information_schema.tables;
-
查詢world和school庫下所有的表明
select table_schema, table_name from information_schema.tables where table_name = 'world' union all select table_schema, table_name from information_schema.tables where table_name = 'school';
-
查詢整個數據庫中所有的庫對應的表明,每個庫顯示一行
select table_schema, group_concat(table_name) from information_schema.tables group by table_schema;
-
統計每個庫下的表的個數
select table_schema, count(table_name) from information_schema.tables group by table_name;
-
統計每個庫的真實數據量 (感覺有問題)
# 每個表的數據量=AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH SELECT sum(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024/1024 as total_nb from information_schema.tables;
-
Concat拼接命令
select concat(user,"@","'",host,"'") from mysql.user;
-
對數據庫下的單張表進行單獨備份
# world庫下的city表 mysqldump -uroot -p****** world city > /tmp/world_city.sql
-
對整個數據庫下的1000張表進行單獨備份,排除sys,performance,information_schema。
select concat("mysqldump -uroot -p******",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql") from information_schema.tables where table_schema not in ("sys","performance","information_schema") into outfile '/tmp/bak.sh';