監(jiān)控MySQL運行狀態(tài):MySQLD Exporter
MySQL是一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL AB公司開發(fā),目前屬于Oracle旗下的產(chǎn)品。 MySQL是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)之一。數(shù)據(jù)庫的穩(wěn)定運行是保證業(yè)務(wù)可用性的關(guān)鍵因素之一。這一小節(jié)當(dāng)中將介紹如何使用Prometheus提供的MySQLD Exporter實現(xiàn)對MySQL數(shù)據(jù)庫性能以及資源利用率的監(jiān)控和度量。
二進制部署MySQL_Exporter
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.0/mysqld_exporter-0.12.0.linux-amd64.tar.gz
tar xvf mysqld_exporter-0.12.0.linux-amd64.tar.gz
mv mysqld_exporter-0.12.0.linux-amd64 /data/
# 創(chuàng)建配置文件
cat >> /data/mysql_exporter/localhost_db.cnf <<EOF
[client]
user=mysqld_exporter
password=12345678
EOF
# 創(chuàng)建systemd服務(wù)
cat > /etc/systemd/system/mysql_exporter.service << EOF
[Unit]
Description=mysql_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/data/mysql_exporter/mysqld_exporter --config.my-cnf="/data/mysql_exporter/localhost_db.cnf" --web.listen-address=":9105"
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
grant permission to database.
GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysqld_exporter'@'localhost' identified by '12345678';
GRANT SELECT ON performance_schema.* TO 'mysqld_exporter'@'localhost';
flush privileges;
Docker部署部署MySQLD Exporter
為了簡化測試環(huán)境復(fù)雜度,這里使用Docker Compose定義并啟動MySQL以及MySQLD Exporter:
version: '3'
services:
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=database
mysqlexporter:
image: prom/mysqld-exporter
ports:
- "9104:9104"
environment:
- DATA_SOURCE_NAME=root:password@(mysql:3306)/database
這里通過環(huán)境變量DATA_SOURCE_NAME方式定義監(jiān)控目標。使用Docker Compose啟動測試用的MySQL實例以及MySQLD Exporter:
$ docker-compose up -d
啟動完成后,可以通過以下命令登錄到MySQL容器當(dāng)中,并執(zhí)行MySQL相關(guān)的指令:
$ docker exec -it <mysql_container_id> mysql -uroot -ppassword
mysql>
可以通過http://localhost:9104訪問MySQLD Exporter暴露的服務(wù):
可以通過/metrics查看mysql_up指標判斷當(dāng)前MySQLD Exporter是否正常連接到了MySQL實例,當(dāng)指標值為1時表示能夠正常獲取監(jiān)控數(shù)據(jù):
# HELP mysql_up Whether the MySQL server is up.
# TYPE mysql_up gauge
mysql_up 1
修改Prometheus配置文件/etc/prometheus/prometheus.yml,增加對MySQLD Exporter實例的采集任務(wù)配置:
- job_name: mysqld
static_configs:
- targets:
- localhost:9104
啟動Prometheus:
prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus
通過Prometheus的狀態(tài)頁,可以查看當(dāng)前Target的狀態(tài):
MySQLD Exporter實例狀態(tài)
為了確保數(shù)據(jù)庫的穩(wěn)定運行,通常會關(guān)注一下四個與性能和資源利用率相關(guān)的指標:查詢吞吐量、連接情況、緩沖池使用情況以及查詢執(zhí)行性能等。
監(jiān)控數(shù)據(jù)庫吞吐量
對于數(shù)據(jù)庫而言,最重要的工作就是實現(xiàn)對數(shù)據(jù)的增、刪、改、查。為了衡量數(shù)據(jù)庫服務(wù)器當(dāng)前的吞吐量變化情況。在MySQL內(nèi)部通過一個名為Questions的計數(shù)器,當(dāng)客戶端發(fā)送一個查詢語句后,其值就會+1。可以通過以下MySQL指令查詢Questions等服務(wù)器狀態(tài)變量的值:
mysql> SHOW GLOBAL STATUS LIKE "Questions";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Questions | 1326 |
+---------------+-------+
1 row in set (0.00 sec)
MySQLD Exporter中返回的樣本數(shù)據(jù)中通過mysql_global_status_questions反映當(dāng)前Questions計數(shù)器的大?。?/p>
# HELP mysql_global_status_questions Generic metric from SHOW GLOBAL STATUS.
# TYPE mysql_global_status_questions untyped
mysql_global_status_questions 1016
通過以下PromQL可以查看當(dāng)前MySQL實例查詢速率的變化情況,查詢數(shù)量的突變往往暗示著可能發(fā)生了某些嚴重的問題,因此用于用戶應(yīng)該關(guān)注并且設(shè)置響應(yīng)的告警規(guī)則,以及時獲取該指標的變化情況:
rate(mysql_global_status_questions[2m])
一般還可以從監(jiān)控讀操作和寫操作的執(zhí)行情況進行判斷。通過MySQL全局狀態(tài)中的Com_select可以查詢到當(dāng)前服務(wù)器執(zhí)行查詢語句的總次數(shù):相應(yīng)的,也可以通過Com_insert、Com_update以及Com_delete的總量衡量當(dāng)前服務(wù)器寫操作的總次數(shù),例如,可以通過以下指令查詢當(dāng)前MySQL實例insert語句的執(zhí)行次數(shù)總量:
mysql> SHOW GLOBAL STATUS LIKE "Com_insert";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_insert | 0 |
+---------------+-------+
1 row in set (0.00 sec)
從MySQLD Exporter的/metrics返回的監(jiān)控樣本中,可以通過global_status_commands_total獲取當(dāng)前實例各類指令執(zhí)行的次數(shù):
# HELP mysql_global_status_commands_total Total number of executed MySQL commands.
# TYPE mysql_global_status_commands_total counter
mysql_global_status_commands_total{command="admin_commands"} 0
mysql_global_status_commands_total{command="alter_db"} 0
mysql_global_status_commands_total{command="alter_db_upgrade"} 0
mysql_global_status_commands_total{command="select"} 10
mysql_global_status_commands_total{command="insert"} 2
mysql_global_status_commands_total{command="update"} 2
mysql_global_status_commands_total{command="delete"} 1
用戶可以通過以下PromQL查看當(dāng)前MySQL實例寫操作速率的變化情況:
sum(rate(mysql_global_status_commands_total{command=~"insert|update|delete"}[2m])) without (command)
連接情況
在MySQL中通過全局設(shè)置max_connections限制了當(dāng)前服務(wù)器允許的最大客戶端連接數(shù)量。一旦可用連接數(shù)被用盡,新的客戶端連接都會被直接拒絕。 因此當(dāng)監(jiān)控MySQL運行狀態(tài)時,需要時刻關(guān)注MySQL服務(wù)器的連接情況。用戶可以通過以下指令查看當(dāng)前MySQL服務(wù)的max_connections配置:
mysql> SHOW VARIABLES LIKE 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.01 sec)
MySQL默認的最大鏈接數(shù)為151。臨時調(diào)整最大連接數(shù),可以通過以下指令進行設(shè)置:
SET GLOBAL max_connections = 200;
如果想永久化設(shè)置,則需要通過修改MySQL配置文件my.cnf,添加以下內(nèi)容:
max_connections = 200
通過Global Status中的Threads_connected、Aborted_connects、Connection_errors_max_connections以及Threads_running可以查看當(dāng)前MySQL實例的連接情況。
例如,通過以下指令可以直接當(dāng)前MySQL實例的連接數(shù):
mysql> SHOW GLOBAL STATUS LIKE "Threads_connected";
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 1 |
+-------------------+-------+
1 row in set (0.00 sec)
當(dāng)所有可用連接都被占用時,如果一個客戶端嘗試連接至MySQL,會出現(xiàn)“Too many connections(連接數(shù)過多)”錯誤,同時Connection_errors_max_connections的值也會增加。為了防止出現(xiàn)此類情況,你應(yīng)該監(jiān)控可用連接的數(shù)量,并確保其值保持在max_connections限制以內(nèi)。同時如果Aborted_connects的數(shù)量不斷增加時,說明客戶端嘗試連接到MySQL都失敗了。此時可以通過Connection_errors_max_connections以及Connection_errors_internal分析連接失敗的問題原因。
下面列舉了與MySQL連接相關(guān)的監(jiān)控指標:
- mysql_global_variables_max_connections: 允許的最大連接數(shù);
- mysql_global_status_threads_connected: 當(dāng)前開放的連接;
- mysql_global_status_threads_running:當(dāng)前開放的連接;
- mysql_global_status_aborted_connects:當(dāng)前開放的連接;
- mysql_global_status_connection_errors_total{error="max_connections"}:由于超出最大連接數(shù)導(dǎo)致的錯誤;
- mysql_global_status_connection_errors_total{error="internal"}:由于系統(tǒng)內(nèi)部導(dǎo)致的錯誤;
通過PromQL查詢當(dāng)前剩余的可用連接數(shù):
mysql_global_variables_max_connections - mysql_global_status_threads_connected
使用PromQL查詢當(dāng)前MySQL實例連接拒絕數(shù):
mysql_global_status_aborted_connects
監(jiān)控緩沖池使用情況
MySQL默認的存儲引擎InnoDB使用了一片稱為緩沖池的內(nèi)存區(qū)域,用于緩存數(shù)據(jù)表以及索引的數(shù)據(jù)。 當(dāng)緩沖池的資源使用超出限制后,可能會導(dǎo)致數(shù)據(jù)庫性能的下降,同時很多查詢命令會直接在磁盤中執(zhí)行,導(dǎo)致磁盤I/O不斷攀升。 因此,應(yīng)該關(guān)注MySQL緩沖池的資源使用情況,并且在合理的時間擴大緩沖池的大小可以優(yōu)化數(shù)據(jù)庫的性能。
Innodb_buffer_pool_pages_total反映了當(dāng)前緩沖池中的內(nèi)存頁的總頁數(shù)??梢酝ㄟ^以下指令查看:
mysql> SHOW GLOBAL STATUS LIKE "Innodb_buffer_pool_pages_total";
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| Innodb_buffer_pool_pages_total | 8191 |
+--------------------------------+-------+
1 row in set (0.02 sec)
MySQLD Exporter通過以下指標返回緩沖池中各類內(nèi)存頁的數(shù)量:
# HELP mysql_global_status_buffer_pool_pages Innodb buffer pool pages by state.
# TYPE mysql_global_status_buffer_pool_pages gauge
mysql_global_status_buffer_pool_pages{state="data"} 516
mysql_global_status_buffer_pool_pages{state="dirty"} 0
mysql_global_status_buffer_pool_pages{state="free"} 7675
mysql_global_status_buffer_pool_pages{state="misc"} 0
Innodb_buffer_pool_read_requests記錄了正常從緩沖池讀取數(shù)據(jù)的請求數(shù)量??梢酝ㄟ^以下指令查看:
mysql> SHOW GLOBAL STATUS LIKE "Innodb_buffer_pool_read_requests";
+----------------------------------+--------+
| Variable_name | Value |
+----------------------------------+--------+
| Innodb_buffer_pool_read_requests | 797023 |
+----------------------------------+--------+
1 row in set (0.00 sec)
MySQLD Exporter通過以下指標返回緩沖池中Innodb_buffer_pool_read_requests的值:
# HELP mysql_global_status_innodb_buffer_pool_read_requests Generic metric from SHOW GLOBAL STATUS.
# TYPE mysql_global_status_innodb_buffer_pool_read_requests untyped
mysql_global_status_innodb_buffer_pool_read_requests 736711
當(dāng)緩沖池?zé)o法滿足時,MySQL只能從磁盤中讀取數(shù)據(jù)。Innodb_buffer_pool_reads即記錄了從磁盤讀取數(shù)據(jù)的請求數(shù)量。通常來說從內(nèi)存中讀取數(shù)據(jù)的速度要比從磁盤中讀取快很多,因此,如果Innodb_buffer_pool_reads的值開始增加,可能意味著數(shù)據(jù)庫的性能有問題。 可以通過以下只能查看Innodb_buffer_pool_reads的數(shù)量
mysql> SHOW GLOBAL STATUS LIKE "Innodb_buffer_pool_reads";
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| Innodb_buffer_pool_reads | 443 |
+--------------------------+-------+
1 row in set (0.00 sec)
在MySQLD Exporter中可以通過以下指標查看Innodb_buffer_pool_reads的數(shù)量。
# HELP mysql_global_status_innodb_buffer_pool_reads Generic metric from SHOW GLOBAL STATUS.
# TYPE mysql_global_status_innodb_buffer_pool_reads untyped
mysql_global_status_innodb_buffer_pool_reads 443
通過以上監(jiān)控指標,以及實際監(jiān)控的場景,我們可以利用PromQL快速建立多個監(jiān)控項。
通過以下PromQL可以得到各個MySQL實例的緩沖池利用率。一般來說還需要結(jié)合Innodb_buffer_pool_reads的增長率情況來結(jié)合判斷緩沖池大小是否合理:
(sum(mysql_global_status_buffer_pool_pages) by (instance) - sum(mysql_global_status_buffer_pool_pages{state="free"}) by (instance)) / sum(mysql_global_status_buffer_pool_pages) by (instance)
也可以通過以下PromQL計算2分鐘內(nèi)磁盤讀取請求次數(shù)的增長率的變化情況:
rate(mysql_global_status_innodb_buffer_pool_reads[2m])
查詢性能
MySQL還提供了一個Slow_queries的計數(shù)器,當(dāng)查詢的執(zhí)行時間超過long_query_time的值后,計數(shù)器就會+1,其默認值為10秒,可以通過以下指令在MySQL中查詢當(dāng)前l(fā)ong_query_time的設(shè)置:
mysql> SHOW VARIABLES LIKE 'long_query_time';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
通過以下指令可以查看當(dāng)前MySQL實例中Slow_queries的數(shù)量:
mysql> SHOW GLOBAL STATUS LIKE "Slow_queries";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries | 0 |
+---------------+-------+
1 row in set (0.00 sec)
MySQLD Exporter返回的樣本數(shù)據(jù)中,通過以下指標展示當(dāng)前的Slow_queries的值:
# HELP mysql_global_status_slow_queries Generic metric from SHOW GLOBAL STATUS.
# TYPE mysql_global_status_slow_queries untyped
mysql_global_status_slow_queries 0
通過監(jiān)控Slow_queries的增長率,可以反映出當(dāng)前MySQL服務(wù)器的性能狀態(tài),可以通過以下PromQL查詢Slow_queries的增長情況:文章來源:http://www.zghlxwxcb.cn/news/detail-805393.html
rate(mysql_global_status_slow_queries[2m])
在MySQL中還可以通過安裝response time插件,從而支持記錄查詢時間區(qū)間的統(tǒng)計信息。啟動該功能后MySQLD Exporter也會自動獲取到相關(guān)數(shù)據(jù),從而可以細化MySQL查詢響應(yīng)時間的分布情況。 感興趣的讀者可以自行嘗試。文章來源地址http://www.zghlxwxcb.cn/news/detail-805393.html
到了這里,關(guān)于Prometheus 監(jiān)控MySQL的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!