您好,登錄后才能下訂單哦!
查看幫助命令
mysql> help; --總的幫助
mysql> help show; --查看show的幫助命令
mysql> help create;--查看create的幫助命令
mysql> help select;--查看select的幫助命令
mysql> help flush;--查看flush的幫助命令
mysql> help reset;--查看reset的幫助命令
查詢實例的基本信息
status
查看數據庫db1的創建腳本
mysql> show create database db1;
查看表table1的創建腳本
mysql> show create table table1\G
查詢table1表哪些字段有索引,Key有值代表該字段有索引
desc table1
查詢table1表的索引,還能看到cardinality信息
show index from table1
查看select語句的執行計劃
explain extended select * from t1;
desc extended select * from t1;
查看某個參數
show global variables like '%XX%';
show global variables where variable_name in ('XX');
查看數據庫是否只讀
show variables like 'read_only';
查看某個狀態
show status like '%YY%';
查看當前連接的客戶端數量
show status like 'Threads_connected';
查看服務器的連接次數
show status like 'Connections';
查看曾經的最大連接數
show status like 'Max_used_connections';
查看mysql線程
show full processlist;
查看有多少個數據庫
show databases;
查看當前數據庫下有多少張表
show tables;
查看各種引擎信息,Support列為DEFAULT表示為當前實例的默認存儲引擎
show engines;
查看當前實例的存儲引擎設置
show variables like '%engi%'
查看LSN(Log sequence number當前redo log的最新號)
show engine innodb status;
查看當前數據庫
select database();
查看當前數據庫服務器版本
select version();
查看當前用戶
select user();
查詢未提交會話的具體SQL
show engine innodb status;查看lock struct信息,比如下面查看到線程是8
1 lock struct(s), heap size 1136, 0 row lock(s), undo log entries 1
MySQL thread id 8, OS thread handle 1358473536, query id 1271 localhost root cleaning up
select sql_text from performance_schema.events_statements_current where THREAD_ID in (select THREAD_ID from performance_schema.threads where PROCESSLIST_ID=8)
或
select b.conn_id,b.thd_id,a.last_statement from sys.session a,sys.processlist b where a.thd_id=b.thd_id and a.conn_id=b.conn_id and b.conn_id=8
查詢鎖源線程
select a.trx_mysql_thread_id,a.* from information_schema.INNODB_TRX a where trx_id in (select blocking_trx_id from information_schema.INNODB_LOCK_WAITS)
查詢被鎖線程
select a.trx_mysql_thread_id,a.* from information_schema.INNODB_TRX a where trx_id in (select requesting_trx_id from information_schema.INNODB_LOCK_WAITS)
查詢XX線程被誰堵塞了select trx_mysql_thread_id blocking_thread,trx_started,trx_query from information_schema.INNODB_TRX where trx_id in
(select blocking_trx_id from information_schema.INNODB_LOCK_WAITS where requesting_trx_id in
(select trx_id from information_schema.INNODB_TRX where trx_mysql_thread_id='XX')
)
5.7.9版本后,建議使用sys.schema_table_lock_waits和sys.innodb_lock_waits來查堵塞,不過需要開啟參數performance_schema=ON
sys.schema_table_lock_waits
select * from sys.schema_table_lock_waits where object_name=`'test'.'t'`\G
select blocking_pid from sys.schema_table_lock_waits where object_name=`'test'.'t'`\G
https://dev.mysql.com/doc/refman/5.7/en/sys-schema-table-lock-waits.html
sys.innodb_lock_waits
select * from sys.innodb_lock_waits where locked_table=`'test'.'t'`\G
select blocking_pid from sys.innodb_lock_waits where locked_table=`'test'.'t'`\G
https://dev.mysql.com/doc/refman/5.7/en/sys-innodb-lock-waits.html
查詢user1用戶的權限
show grants for user1
查看所有binary日志
show binary logs;
show master logs;
查看當前binary日志文件狀態
show master status;
刷新binary日志
flush binary logs;
刪除某個binary日志之前的所有日志
purge binary logs to 'mysql-bin.000003';
刪除所有的binary log
mysql> reset master;
查詢有多少條慢查詢記錄
mysql> show global status like '%Slow_queries%';
執行一個10秒的查詢
mysql> select sleep(10);
查找持續時間超過 60s 的事務
select * from information_schema.innodb_trx where TIME_TO_SEC(timediff(now(),trx_started))>60
查詢所有數據庫的數據和索引的大小
select round(sum(data_length+index_length)/1024/1024) as total_mb,round(sum(data_length)/1024/1024) as data_mb,round(sum(index_length)/1024/1024) as index_mb from information_schema.tables
查詢每個數據庫的引擎、容量、總表數
select table_schema,engine,
round(sum(data_length+index_length)/1024/1024) as total_mb,
round(sum(data_length)/1024/1024) as data_mb,
round(sum(index_length)/1024/1024) as index_mb
count(*) as tables
from information_schema.tables
where table_schema not in('INFORMATION_SCHEMA','PERFORMANCE_SCHEMA') group by table_schema,engine order by 3 desc
查詢耗CPU的SQL
mysql> show full processlist;
找到Time最大的,其對應的ID列就是耗cpu最厲害的線程ID,對應的Info列就是具體的SQL
或
查看慢查詢日志,找到Query_time值最大的行,會記錄其線程ID號和具體的SQL
在master上查看有哪些slave
mysql> select * from information_schema.processlist as p where p.command = 'Binlog Dump';
或
mysql> show slave hosts;(此方法需要在從服務啟動時指定--report-host=HOSTNAME選項,此處HOSTNAME為任意名稱。)
殺線程的SQL,以下兩者必須同時使用,其中kill thread_id=kill connection thread_id
mysql>kill query thread_id
mysql>kill thread_id
所有數據庫事件的查看
select db,name,last_executed,status from mysql.event;
單個數據庫的事件查看
show events from dbname\G;
禁用某個數據庫的某個事件
alter event dbname.eventname disable;
重新收集表的統計信息
analyze table tablename
重建表
alter table tablename engine=innodb
修改表的存儲引擎為innodb
alter table tablename engine=innodb
優化表
optimize table tablename=analyze table tablename + alter table tablename engine=innodb
修改proc存儲過程的definer,比如把'dev_user@%'改成'prod_user@%'
select db,name,type,definer from mysql.proc\G;
update mysql.proc set definer='prod_user@%' where definer='dev_user@%'
修改event事件的definer,比如把'dev_user@%'改成'prod_user@%'
select db,name,definer from mysql.event\G;
update mysql.event set definer='prod_user@%' where definer='dev_user@%'
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。