1. 程式人生 > >mysql生產環境常用命令

mysql生產環境常用命令

sql insert evel enter HR bin proc sele comm

嘮嘮mysql的連接數

1.查看mysql的當前連接數

[root@localhost ~]# mysqladmin -uroot -p123456 status
Uptime: 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)

[root@localhost ~]# 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/bash
for id in `mysqladmin  -uxxx -h 192.168.1.1 -pxxxx processlist | grep Query | grep paigoushop | grep Sending data  |  awk {print $2}` 
        do 
                        mysqladmin  -uxxx -h 192.168.1.1 -pxxx  kill ${id} 
        done
#grep有點多 為了準確定位到自己想殺死的sql

下邊這個是網上找的,我也沒試過 放這裏 萬一以後自己用呢

#!/bin/bash
for 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生產環境常用命令