標籤:cache enter -- 阻塞 開啟 host htm 資料庫 and
嘮嘮mysql的串連數
1.查看mysql的當前串連數
[[email protected] ~]# mysqladmin -uroot -p123456 statusUptime: 34070 Threads: 1 Questions: 8678 Slow queries: 0 Opens: 910 Flush tables: 43 Open tables: 4 Queries per second avg: 0.254
Therads 開啟的串連數
mysql> show status like ‘%Thread%‘;
+------------------------------------------+-------+
| Variable_name | Value |
+------------------------------------------+-------+
| Delayed_insert_threads | 0 |
| Performance_schema_thread_classes_lost | 0 |
| Performance_schema_thread_instances_lost | 0 |
| Slow_launch_threads | 0 |
| Threads_cached | 0 |
| Threads_connected | 1 |
| Threads_created | 4 |#建立的串連數
| Threads_running | 1 |#開啟的串連數
+------------------------------------------+-------+
8 rows in set (0.00 sec
2.查看mysql允許的最大串連數
mysql> show variables like ‘%max_connections%‘;+-----------------+-------+| Variable_name | Value |+-----------------+-------+| max_connections | 151 |+-----------------+-------+1 row in set (0.00 sec)
3.mysql串連數太多解決方案
http://www.cnblogs.com/lazyball/p/7424243.html
說說mysql的慢查詢
慢查詢產生的原因一般是因為select設計的條件比較多,比如子查詢之類的,資料庫一直在插卡在哪裡,別的語句也無法運行造成阻塞。開發也不是萬能的,也會寫一些 吊炸天的sql語句,一眼望去看不到結尾。
1. 查看mysql裡有哪些進程在運行
mysql> show processlist; #預設一般顯示100行 加上full顯示的多+----+------+-----------+------+---------+------+-------+------------------+| Id | User | Host | db | Command | Time | State | Info |+----+------+-----------+------+---------+------+-------+------------------+| 4 | root | localhost | NULL | Sleep | 882 | | NULL || 6 | root | localhost | NULL | Query | 0 | NULL | show processlist |+----+------+-----------+------+---------+------+-------+------------------+2 rows in set (0.00 sec)mysql> show full processlist;+----+------+-----------+------+---------+------+-------+-----------------------+| Id | User | Host | db | Command | Time | State | Info |+----+------+-----------+------+---------+------+-------+-----------------------+| 4 | root | localhost | NULL | Sleep | 894 | | NULL || 6 | root | localhost | NULL | Query | 0 | NULL | show full processlist |+----+------+-----------+------+---------+------+-------+-----------------------+2 rows in set (0.00 sec)
[[email protected] ~]# mysqladmin -uroot -p processlist
Enter password:
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 4 | root | localhost | | Sleep | 1238 | | |
| 7 | root | localhost | | Query | 0 | | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+
參數解釋:
id #ID標識,要kill一個語句的時候很有用
use #當前串連使用者
host #顯示這個串連從哪個ip的哪個連接埠上發出
db #資料庫名
command #串連狀態,一般是休眠(sleep),查詢(query),串連(connect)
time #串連期間,單位是秒
state #顯示當前sql語句的狀態
info #顯示這個sql語句
##慢查詢發生的時候,state狀態一般是 watting for table level lock 觀察time 看是否啟動並執行時間很久,緊急就殺死,不緊急找開發去,寫的什麼玩意(自己知道咋最佳化
的話,找開發炫耀去)
2. 緊急批量殺死mysql慢查詢的指令碼
先說自己寫的吧
#!/bin/bashfor id in `mysqladmin -uxxx -h 192.168.1.1 -p‘xxxx‘ processlist | grep Query | grep ‘paigoushop‘ | grep ‘Sending data‘ | awk ‘{print $2}‘` do mysqladmin -uxxx -h 192.168.1.1 -p‘xxx‘ kill ${id} done
#grep有點多 為了準確定位到自己想殺死的sql
下邊這個是網上找的,我也沒試過 放這裡 萬一以後自己用呢
#!/bin/bashfor i in `mysql -Bse ‘show full processlist;‘ | grep -i select |grep -i "Waiting for table level lock" | awk ‘{print $1}‘`do mysql -Bse "kill $i"done
3. 開啟慢查詢日誌記錄
總覺得上邊的方法不太好,比較被動,對於一些不懂的人,又要罵營運了,我來學一下:“不,什麼情況呀,小楊,你這資料庫也太慢了吧,怎麼搞得,趕緊給我查。”
說起來都是淚呀,那就不說了。用事實說話,把sql語句拿出來,讓開發好好看看寫的什麼玩意。
mysql生產環境常用命令