MySQL中經常使用命令

受權

show grants;                                                     //查看當前用戶的權限
grant all privileges on test.* to jack@'%' identified by '123';  //爲用戶名jack密碼123,授予test數據庫全部權限
revoke all privileges on *.* from jack@'%';                      //收回用戶jack@用戶地址(%),全部庫的全部權限

索引

SHOW INDEX FROM my_table;                                        //查看my_table的索引 CREATE [UNIQUE] INDEX idx_name ON my_table(col1,col2)            //爲my_table表的col1,col2字段,添加[惟一]索引idx_name
ALTER TABLE my_table ADD PRIMARY KEY(my_id)                      //爲my_table表的my_id字段,添加惟一主鍵索引
ALTER TABLE my_table ADD INDEX idx_name(my_cloumn)               //爲my_table表的my_cloumn字段,添加普通索引idx_name
ALTER TABLE my_table ADD UNIQUE uq_name(my_cloumn)               //爲my_table表的my_cloumn字段,添加惟一索引uq_name
ALTER TABLE my_table ADD FULLTEXT ft_name(my_cloumn)             //爲my_table表的my_cloumn字段,添加全文索引ft_name
DROP INDEX idx_name ON my_table;                                 //刪除my_table表的idx_name索引

加鎖

lock table mylock read;                                          //爲mylock表加讀鎖
lock table mylock write;                                         //爲mylock表加寫鎖
unlock tables;                                                   //釋放鎖

日誌引擎

show engines;                                                    //查看MySQL支持的存儲索引
show variables like '%storage_engine%'                           //查看當前使用的存儲引擎
show variables like 'slow_query_log%';                           //查看是否開啓慢日誌 set global slow_query_log = 1;                                   //開啓慢查詢日誌,重啓MySQL失效
show variables like 'long_query_time%';                          //查看慢查詢日誌的閾值,默認是10s超過這個值就會記錄
set global long_query_time = 3;                                  //設置慢查詢日誌的閾值爲3s,重啓MySQL失效
show global status like '%slow_queries%';                        //查看系統中存在慢sql的條數
show variables like 'profiling%';                                //查看 Show Profile 是否開啓
set profiling=on;                                                //開啓 Show Profiles
show variables like 'log_bin%';                                  //查看binlog狀態
set global log_bin_trust_function_creators = 1;                  //添加能夠信任存儲函數建立者
show profiles;                                                   //查看最近15(默認)條sql執行
show profile cpu, block io for query Query_ID;                   //查看具體某一條的sql執行狀況

調優

show [full] processlist;                                         //查看有哪些線程在運行
show open tables from test;                                      //在test庫中的表緩存被打開的非TEMPORARY表,in_use被多少鏈接使用
show status like 'key%';                                         //myIsam中cache索引命中率,key_buffer_read_hits=(1-key_reads/key_read_requests)
show status like 'innodb_buffer_pool_read%';                     //innodb的buffer命中率(1-innodb_buffer_pool_reads/innodb_buffer_pool_read_requests) 
show status like 'Qcache%';                                      //query cache命中率,query_cache_hits =Qcache_hits/(Qcache_hits+Qcache_inserts)
show status like '%connections%';                                //查看服務器的總鏈接數Connections和最大鏈接數Max_used_connections
show status like 'thread%';                                      //鏈接緩存>90%合理,thread_cache_hits = (1- threads_created/connections) * 100 %;
show status like 'created_tmp%';                                 //查看臨時表 Created_tmp_disk_tables / Created_tmp_tables<25%
相關文章
相關標籤/搜索