https://blog.csdn.net/achiberx/article/details/71514862?utm_source=blogkpcl11
1. MySQL Connections
數據庫連接數分為Max Connections, Max Used Connections 和 Connections
即:最大連接數, 最近期間使用的最大連接數 和 當前實時連接數!
在使用MySQL數據庫的時候,經常會遇到這么一個問題,就是“Can not connect to MySQL server. Too many connections”-mysql 1040錯誤,這是因為訪問MySQL且還未釋放的連接數目已經達到MySQL的上限。通常,msql的最大連數默認是151(ubuntu), 100(windows)。
mysql>mysql -u user -p password(命令行登錄MySQL)
mysql>show variables like 'max_connections';(查可以看當前的最大連接數)
msyql>set global max_connections=1000;(設置最大連接數為1000,可以再次查看是否設置成功)
mysql>exit(推出)
1
2
3
4
這種方式有個問題,就是設置的最大連接數只在mysql當前服務進程有效,一旦mysql重啟,又會恢復到初始狀態。因為mysql啟動后的初始化工作是從其配置文件中讀取數據的,而這種方式沒有對其配置文件做更改。
問題:MySQL的最大連接數設置為多少比較合適?這個值是不是越大越好?
這個參數的大小要綜合很多因素來考慮,比如使用的平台所支持的線程庫數量(windows只能支持到2048)、服務器的配置(特別是內存大小)、每個連接占用資源(內存和負載)的多少、系統需要的響應時間等。可以在global或session范圍內修改這個參數。連接數的增加會帶來很多連鎖反應,需要在實際中避免由此引發的負面影響。
詳細參考:MySQL最大連接數設置
ubuntu設置mysql的最大連接數:
cd /etc/mysql/mysql.conf.d 修改mysqld.cnf文件,在[mysqld]中添加或修改
cd /etc/mysql/mysql.conf.d
vi mysqld.cnf
max_connections=99
1
2
3
然后重新啟動mysql
[root@localhost ~]# service mysql restart //重啟mysql
查看當前系統下MySQL設置的最大連接數
[root@localhost ~]# /usr/bin/mysqladmin -u root -p variables | grep max_connections
| max_connections | 99
1
2
2. MySQL Active Threads
Threads Connected , Thread Running 即連接的線程數和運行的線程數。
可以用多線程去測試!
MySQL Questions
4. MySQL Thread Cache
Thread Cache Size
Thread Cached
Thread Ctreated
MySQL服務器的線程數查看方法:
mysql> show global status like 'Thread%';
1
Variable_name Values meaning
Threads_cached 57 The number of threads in the thread cache
Threads_connected 1268 The number of currently open connections.
Threads_created 31715 The number of threads created to handle connections.
Threads_running 1 The number of threads that are not sleeping.
thread_cache_size的意義:每建立一個連接,都需要一個線程來與之匹配,此參數用來緩存空閑的線程,以至不被銷毀,如果線程緩存中有空閑線程,這時候如果建立新連接,MYSQL就會很快的響應連接請求。
mysql建立連接非常消耗資源,所以就有了thread_cache,當已有連接不再使用之后,mysql server不是直接斷開連接,而是將已有連接轉入到thread_cache中,以便下次在有create thread的需求時,可以在cache中復用,提高性能,降低資源消耗。
threads_cached :代表當前此時此刻線程緩存中有多少空閑線程。
Threads_connected :代表當前已建立連接的數量,因為一個連接就需要一個線程,所以也可以看成當前被使用的線程數。
Threads_created :代表從最近一次服務啟動,已創建線程的數量。
Threads_running :代表當前激活的(非睡眠狀態)線程數。並不是代表正在使用的線程數,有時候連接已建立,但是連接處於sleep狀態,這里相對應的線程也是sleep狀態。
四者之間的關系:
running和其他三個狀態關系不大,但肯定不會超過thread_connected
(new_con-old_con)=create+(old_cache-new_cache)
從上面公式可以看出,如果create等於0,那么thread_connected減少的和thread_cached增加的相等,thread_connected增加的和thread_cached減少的相等。(其實這也就是thread_cached存在的意義,資源可以復用)
我們來看眼影響thread_cached的參數thread_cache_size:
How many threads the server should cache for reuse. When a client disconnects, the client’s threads are put in the cache if there are fewer than thread_cache_size threads there. Requests for threads are satisfied by reusing threads taken from the cache if possible, and only when the cache is empty is a new thread created. This variable can be increased to improve performance if you have a lot of new connections. Normally, this does not provide a notable performance improvement if you have a good thread implementation. However, if your server sees hundreds of connections per second you should normally set thread_cache_size high enough so that most new connections use cached threads. By examining the difference between the Connections and Threads_created status variables, you can see how efficient the thread cache is. For details, see Section 5.1.6, “Server Status Variables”.
參考:MySQL之thread cache
ubuntu修改thread_cache_size
cd /etc/mysql/mysql.conf.d
sudo vi mysqld.cnf
thread_cache_size = 48 //修改為48
1
2
3
MySQL Temporary Objects
Create Tmp Tables
Create Tmp Disk Tables
Create Tmp Files
MySQL Select Types
Select Scan:通過對第一個數據表進行全表掃描而完成的多數據表聯接操作的次數.
Select Range Check:該變量記錄了在聯接時,對每一行數據重新檢查索引的查詢計划的數量,它的開銷很大.如果該值較高或正在增加,說明一些查詢沒有找到好索引.
Select Range:利用第一個數據表上的某個區間而完成的多數據表聯接操作的次數.
Select Full Range Join:利用一個輔助性的參照表(reference table)上的區間搜索
(range search)操作而完成的多數據表聯接操作的次數.該值表示使用了范圍查詢聯接表的次數.
Select Full Join:沒有使用索引而完成的多表聯接操作的次數.這種情況是性能殺手,最好去優化sql.
MySQL Sorts
Sort Scan:利用一次全表掃作而完成的排序操作的次數
Sort Merge Passes:查詢導致了文件排序的次數.可以優化sql或者適當增加sort_buffer_size變量
Sort Range:利用一個區間進行的排序操作的次數
Sort Rows:對多少行排序
MySQL Slow Queries
Slow Queries:慢查詢的次數(執行時間超過long_query_time值) 參考鏈接
MySQL Aborted Connections
Aborted Connects (attempts)
Aborted Clients (timeout)
MySQL Table Locks
Table Locks Immediate:無需等待就能夠立刻得到滿足的數據表鎖定請求的個數
Table Locks Waited:顯示了有多少表被鎖住了並且導致服務器級的鎖等待(存儲引擎級的鎖,如InnoDB行級鎖,不會使該變量增加).如果這個值比較高或者正在增加,那么表明存在嚴重的並發瓶頸.
MySQL Network Traffic
OutBound
Inbound
MySQL Internal Memory Overview
System Memory :操作系統總內存
Query Cache Size :mysql配置文件設置的大小
Key Buffer Size :mysql配置文件設置大小。
InnoDB Log Buffer Size
InnoDB Buffer Pool Data
Top Command Counters
show session status like 'com_delete';
show session status like 'com_insert';
show session status like 'com_update';
show session status like 'com_select';
1
2
3
4
com_insert、com_delete,com_update分別是查詢本次會話中“增刪改” 了多少次.
com_select的一種解釋為com_select 變量記錄的是無緩存的查詢次數+錯誤查詢+權限檢查查詢。如果select有緩存,數值不增加,如果命中緩存,緩存命中的次數加1,也就是Qcache_hits變量的值加1.
MySQL Handlers
read_rnd_next:讀取下一個數據行的請求的個數.如果這個數字很高,就說明有很多語句需要通過全表掃描才能完成或有很多查詢沒有使用適當的索引
write:向數據表里插入一個數據行的請求的個數
external_lock
read_key:根據一個索引值而讀取一個數據行的請求的個數
update:對數據表里的一個數據行進行修改的請求的個數
read_rnd:根據某個數據行的位置而讀取該數據行的請求的個數
read_prev:按索引逆序讀取前一個數據行的請求的個數
read_next:按索引順序讀取下一個數據行的請求的個數
read_last
read_first:讀取索引中第一個索引項的請求的個數
mrr_init
discover
delete:從數據表刪除一個數據行的請求的個數
MySQL Transaction Handlers
commit :提交一個事務的請求的個數
prepare
rollback :回滾一個事務的請求的個數
savepoint :創建一個事務保存點的請求個數
savepoint_rollback :回滾到一個事務保存點的請求的個數
Process States
Top Process States Hoursly
MySQL Query Cache Memory
Query Cache Size
Free Memory
MySQL Query Cache Activity
Not Cached
Queries in Cache
Prunes
Inserts
Hits
MySQL File Openings
Openings
MySQL Open Files
Open Files 打開的文件數
Open Files Limits 文件的上限
InnoDB Open File InnoDB打開的文件數
MySQL Table Open Status
Hits
Misses
Opennings
Misses due to Overflows
Table Open Cache Hit Ratio
MySQL Open Tables
Table Open Cache
Open Tables
MySQL Table Definition Cache
Table Definitions Cache Size
Open Table Definitions
Opened Table Definitions
參考:MySQL監控模板說明
---------------------
作者:acceptedwwh
來源:CSDN
原文:https://blog.csdn.net/achiberx/article/details/71514862
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!