查看幫助命令
網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)服務(wù)團(tuán)隊(duì)是一支充滿著熱情的團(tuán)隊(duì),執(zhí)著、敏銳、追求更好,是創(chuàng)新互聯(lián)的標(biāo)準(zhǔn)與要求,同時(shí)竭誠為客戶提供服務(wù)是我們的理念。創(chuàng)新互聯(lián)把每個(gè)網(wǎng)站當(dāng)做一個(gè)產(chǎn)品來開發(fā),精雕細(xì)琢,追求一名工匠心中的細(xì)致,我們更用心!
MySQL> help; --總的幫助
mysql> help show; --查看show的幫助命令
mysql> help create;--查看create的幫助命令
mysql> help select;--查看select的幫助命令
mysql> help flush;--查看flush的幫助命令
mysql> help reset;--查看reset的幫助命令
查詢實(shí)例的基本信息
status
查看數(shù)據(jù)庫db1的創(chuàng)建腳本
mysql> show create database db1;
查看表table1的創(chuàng)建腳本
mysql> show create table table1\G
查詢table1表哪些字段有索引,Key有值代表該字段有索引
desc table1
查詢table1表的索引,還能看到cardinality信息
show index from table1
查看select語句的執(zhí)行計(jì)劃
explain extended select * from t1;
desc extended select * from t1;
查看某個(gè)參數(shù)
show global variables like '%XX%';
show global variables where variable_name in ('XX');
查看數(shù)據(jù)庫是否只讀
show variables like 'read_only';
查看某個(gè)狀態(tài)
show status like '%YY%';
查看當(dāng)前連接的客戶端數(shù)量
show status like 'Threads_connected';
查看服務(wù)器的連接次數(shù)
show status like 'Connections';
查看曾經(jīng)的最大連接數(shù)
show status like 'Max_used_connections';
查看mysql線程
show full processlist;
查看有多少個(gè)數(shù)據(jù)庫
show databases;
查看當(dāng)前數(shù)據(jù)庫下有多少張表
show tables;
查看各種引擎信息,Support列為DEFAULT表示為當(dāng)前實(shí)例的默認(rèn)存儲(chǔ)引擎
show engines;
查看當(dāng)前實(shí)例的存儲(chǔ)引擎設(shè)置
show variables like '%engi%'
查看LSN(Log sequence number當(dāng)前redo log的最新號(hào))
show engine innodb status;
查看當(dāng)前數(shù)據(jù)庫
select database();
查看當(dāng)前數(shù)據(jù)庫服務(wù)器版本
select version();
查看當(dāng)前用戶
select user();
查詢未提交會(huì)話的具體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來查堵塞,不過需要開啟參數(shù)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用戶的權(quán)限
show grants for user1
查看所有binary日志
show binary logs;
show master logs;
查看當(dāng)前binary日志文件狀態(tài)
show master status;
刷新binary日志
flush binary logs;
刪除某個(gè)binary日志之前的所有日志
purge binary logs to 'mysql-bin.000003';
刪除所有的binary log
mysql> reset master;
查詢有多少條慢查詢記錄
mysql> show global status like '%Slow_queries%';
執(zhí)行一個(gè)10秒的查詢
mysql> select sleep(10);
查找持續(xù)時(shí)間超過 60s 的事務(wù)
select * from information_schema.innodb_trx where TIME_TO_SEC(timediff(now(),trx_started))>60
查詢所有數(shù)據(jù)庫的數(shù)據(jù)和索引的大小
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
查詢每個(gè)數(shù)據(jù)庫的引擎、容量、總表數(shù)
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最大的,其對(duì)應(yīng)的ID列就是耗cpu最厲害的線程ID,對(duì)應(yīng)的Info列就是具體的SQL
或
查看慢查詢?nèi)罩?,找到Query_time值最大的行,會(huì)記錄其線程ID號(hào)和具體的SQL
在master上查看有哪些slave
mysql> select * from information_schema.processlist as p where p.command = 'Binlog Dump';
或
mysql> show slave hosts;(此方法需要在從服務(wù)啟動(dòng)時(shí)指定--report-host=HOSTNAME選項(xiàng),此處HOSTNAME為任意名稱。)
殺線程的SQL,以下兩者必須同時(shí)使用,其中kill thread_id=kill connection thread_id
mysql>kill query thread_id
mysql>kill thread_id
所有數(shù)據(jù)庫事件的查看
select db,name,last_executed,status from mysql.event;
單個(gè)數(shù)據(jù)庫的事件查看
show events from dbname\G;
禁用某個(gè)數(shù)據(jù)庫的某個(gè)事件
alter event dbname.eventname disable;
重新收集表的統(tǒng)計(jì)信息
analyze table tablename
重建表
alter table tablename engine=innodb
修改表的存儲(chǔ)引擎為innodb
alter table tablename engine=innodb
優(yōu)化表
optimize table tablename=analyze table tablename + alter table tablename engine=innodb
修改proc存儲(chǔ)過程的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@%'