十一、Mysql常見的show命令及information_schema.tables視圖說明


一、常見的show命令

show  databases;                          #查看所有數據庫
show tables;                              #查看當前庫的所有表
SHOW TABLES FROM                          #查看某個指定庫下的表
show create database world                #查看建庫語句
show create table world.city              #查看建表語句
show  grants for  root@'localhost'        #查看用戶的權限信息
show  charset;                           #查看字符集
show collation                            #查看校對規則
show processlist;                         #查看數據庫連接情況
show index from                           #表的索引情況
show status                               #數據庫狀態查看
SHOW STATUS LIKE '%lock%';                #模糊查詢數據庫某些狀態
SHOW VARIABLES                            #查看所有配置信息
SHOW variables LIKE '%lock%';             #查看部分配置信息
show engines                              #查看支持的所有的存儲引擎
show engine innodb status\G               #查看InnoDB引擎相關的狀態信息
show binary logs                          #列舉所有的二進制日志
show master status                        #查看數據庫的日志位置信息
show binlog evnets in                     #查看二進制日志事件
show slave status \G                      #查看從庫狀態
SHOW RELAYLOG EVENTS                      #查看從庫relaylog事件信息
desc  (show colums from city)             #查看表的列定義信息
http://dev.mysql.com/doc/refman/5.7/en/show.html

二、information_schema.tables視圖說明

1、information_schema.tables視圖的結構說明

information_schema.tables視圖常用列屬性

DESC information_schema.TABLES
TABLE_SCHEMA    ---->所有數據庫的庫名
TABLE_NAME      ---->所有表的表名
ENGINE          ---->引擎
TABLE_ROWS      ---->表的行數
AVG_ROW_LENGTH  ---->表中行的平均行(字節)
INDEX_LENGTH    ---->索引的占用空間大小(字節)

2、information_schema.tables視圖的案例說明

1)查詢整個數據庫中所有庫和所對應的表信息

SELECT TABLE_SCHEMA,GROUP_CONCAT(TABLE_NAME)
FROM information_schema.tables
GROUP BY TABLE_SCHEMA;

2)統計所有庫下的表個數

SELECT TABLE_SCHEMA,COUNT(TABLE_NAME)
FROM information_schema.tables
GROUP BY TABLE_SCHEMA;

3)查詢所有innodb引擎的表及所在的庫

SELECT TABLE_SCHEMA,TABLE_NAME,ENGINE
FROM information_schema.TABLES
WHERE ENGINE='innodb';

4)統計mysql數據庫下每張表的磁盤空間占用

數據庫下表所占用空間的計算

 
表中的平均行(字節)*表的行數+索引的占用空間大小
AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH
單位為字節
SELECT TABLE_SCHEMA,CONCAT((TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024,"KB") AS size_KB
FROM information_schema.TABLES
WHERE TABLE_SCHEMA='mysql';

#CONCAT((TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024,"KB")
#拼接單位KB

5)統計每個數據庫所占用的磁盤空間

 
SELECT
TABLE_SCHEMA,
CONCAT(SUM(TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024," KB") AS Total_KB
FROM information_schema.tables
GROUP BY table_schema;

3、"concat"命令拼接用法及"INTO OUTFILE" 的導出

6)生成整個數據庫下的所有表的單獨備份語句(不備份'information_schema','performance_schema','sys')

表的備份語句

mysqldump -uroot -p123 mysql user > /tmp/mysql_user.sql
SELECT CONCAT('mysqldump -uroot -p123456 ',table_schema,' ',table_name,' > /tmp/',table_schema,'_',table_name,'.sql')
FROM information_schema.tables 
WHERE table_schema NOT IN('information_schema','performance_schema','sys')
INTO OUTFILE '/tmp/bak.sh' ;
#INTO OUTFILE '/tmp/bak.sh';
#把拼接的整個數據庫下的所有表的單獨備份語句導出到/tmp/bak.sh
注意INTO OUTFILE導出命令需要在my.cnf中添加secure-file-priv=xxx目錄路徑,不添加該項執行會報---secure-file-priv錯誤
INTO OUTFILE后只能接xxx目錄路徑/xxx文件名
例如:只能導出到/tmp目錄
[root@vm01 ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/app/mysql
datadir=/data/mysql
server_id=201
port=3306
socket=/tmp/mysql.sock
log_bin=mysql-bin
log_error=/tmp/mysqld_err.log
secure-file-priv=/tmp
[mysql]
socket=/tmp/mysql.sock
prompt= [\\d]>

重啟數據庫

執行結果

[root@vm01 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

[(none)]>SELECT CONCAT('mysqldump -uroot -p123456 ',table_schema,' ',table_name,' > /tmp/',table_schema,'_',table_name,'.sql')
    -> FROM information_schema.tables 
    -> WHERE table_schema NOT IN('information_schema','performance_schema','sys')
    -> INTO OUTFILE '/tmp/bak.sh' ;
Query OK, 39 rows affected (0.00 sec)
[root@vm01 ~]# ll /tmp/bak.sh
-rw-rw-rw- 1 mysql mysql 2837 Nov 12 06:00 /tmp/bak.sh

執行bak.sh看看數據庫是否備份成功

[root@vm01 ~]# cd /tmp
[root@vm01 tmp]# bash bak.sh 
[root@vm01 tmp]# ls *.sql
mysql_columns_priv.sql   mysql_help_keyword.sql        mysql_proc.sql     mysql_tables_priv.sql                school_sc.sql
mysql_db.sql             mysql_help_relation.sql       mysql_proxies_priv.sql          mysql_time_zone_leap_second.sql      school_stu.sql
mysql_engine_cost.sql    mysql_help_topic.sql          mysql_server_cost.sql           mysql_time_zone_name.sql             school_teacher.sql
mysql_event.sql          mysql_innodb_index_stats.sql  mysql_servers.sql               mysql_time_zone.sql                  world_city.sql
mysql_func.sql           mysql_innodb_table_stats.sql  mysql_slave_master_info.sql     mysql_time_zone_transition.sql       world_countrylanguage.sql
mysql_general_log.sql    mysql_ndb_binlog_index.sql    mysql_slave_relay_log_info.sql  mysql_time_zone_transition_type.sql  world_country.sql
mysql_gtid_executed.sql  mysql_plugin.sql              mysql_slave_worker_info.sql     mysql_user.sql                       ywx_king.sql
mysql_help_category.sql  mysql_procs_priv.sql          mysql_slow_log.sql              school_course.sql

數據庫的表已經全部備份成功

7)107張表,都需要執行以下2條語句

ALTER TABLE world.city DISCARD TABLESPACE;
ALTER TABLE world.city IMPORT TABLESPACE;
SELECT CONCAT("alter table ",table_schema,".",table_name," discard tablespace")
FROM information_schema.tables
WHERE table_schema='world'
INTO OUTFILE '/tmp/dis.sql';

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM