您好,登錄后才能下訂單哦!
了解數(shù)據(jù)庫(kù)的TPS、QPS是作為一個(gè)運(yùn)維DBA是非常必要的,那什么是TPS、QPS呢,簡(jiǎn)單的理解是:
QPS:每秒查詢數(shù),即對(duì)數(shù)據(jù)庫(kù)每秒的DML的操作數(shù)
TPS:每秒事物處理,即對(duì)數(shù)據(jù)庫(kù)每秒DDL操作數(shù)
通過(guò)了解他們,可以掌握一個(gè)實(shí)例的基本工作運(yùn)行狀態(tài)
如何對(duì)于對(duì)他們進(jìn)行頁(yè)面可視化,是DBA的一個(gè)裝逼神器,本章主要介紹通過(guò)時(shí)序數(shù)據(jù)庫(kù)(influxdb)+grafana+簡(jiǎn)單的python代碼實(shí)現(xiàn)
時(shí)時(shí)監(jiān)控它們,什么是時(shí)序數(shù)據(jù)庫(kù)可以在其他章節(jié)了解,這里不做過(guò)多介紹
Let's go.....3個(gè)包的下載
http://down.51cto.com/data/2287378
http://down.51cto.com/data/2287380
http://down.51cto.com/data/2287379
1、直接YUM安裝influxdb,安裝后生成默認(rèn)的配置文件/etc/influxdb/influxdb.conf 暫不修改
2、ON/OF服務(wù)
service influxdb start/stop
3、influxdb數(shù)據(jù)庫(kù)網(wǎng)頁(yè)管理控制臺(tái)(默認(rèn)值),這個(gè)只是方便某些操作,具體操作還是到終端輸入命令influx,此通過(guò)http://IP:8083 通過(guò)頁(yè)面方式登錄添加
一個(gè)用戶名和密碼,為grafana連接INFLUXDB使用,用戶名密碼自己定義
選中create user 再query 就會(huì)有CREATE USER "根據(jù)實(shí)際輸入用戶名" WITH PASSWORD '輸入你的密碼'
[root@mycat ~]# influx
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 0.13.0
InfluxDB shell version: 0.13.0
這個(gè)就可以登錄,是不是很熟悉的,具體操作看博客的其他章節(jié)。
注意:數(shù)據(jù)庫(kù)url(默認(rèn)值):http://localhost:8086 ##這個(gè)在后面的grafana會(huì)被用到,數(shù)據(jù)傳輸使用
4、YUM安裝GRAFANA包
安裝后on/off服務(wù):/etc/init.d/grafana-server start/stop
5、GRAFANA的頁(yè)面登錄,默認(rèn)使用3000端口:
192.168.1.114:3000 登錄名和密碼默認(rèn)是admin
6、通過(guò)Python代碼實(shí)現(xiàn)對(duì)MySQL和influxdb的同時(shí)操作
#注意:python 操作MySQL需要安裝python-mysql的驅(qū)動(dòng)包,自己百度下載個(gè),一般在解壓后目錄里執(zhí)行1、python setup.py build 2、python setup.py install
python 操作INFLUXDB也需要python-influxdb
$ pip install influxdb $ pip install --upgrade influxdb $ pip uninstall influxdb ##注意如果沒(méi)有安裝PIP自己百度安裝吧。
7、python 腳本:
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
import MySQLdb
import datetime
import json
#qps
import time
from influxdb import InfluxDBClient
#import influxdb
try:
conn=MySQLdb.connect(host="192.168.15.104",user="dlan",passwd="root123",port=3306)
client=InfluxDBClient(host='192.168.15.104', port=8086, username='root', password='root', database='telegraf')
cur=conn.cursor()
while True:
sql = '''show global status where variable_name in('com_select','com_insert','com_delete','com_update','com_insert_select','uptime')'''
cur.execute(sql)
aa = cur.fetchall()
aa=list(aa)
delete = int(aa[0][1])
insert1 = int(aa[1][1])
insert2 = int(aa[2][1])
select = int(aa[3][1])
update = int(aa[4][1])
uptime1 = int(aa[5][1])
qps1=delete+insert1+insert2+select+update
time.sleep(1)
while True:
sql = '''show global status where variable_name in('com_select','com_insert','com_delete','com_update','com_insert_select','uptime')'''
cur.execute(sql)
aa = cur.fetchall()
aa = list(aa)
delete_2 = int(aa[0][1])
insert_2 = int(aa[1][1])
insert2_2 = int(aa[2][1])
select_2 = int(aa[3][1])
update_2 = int(aa[4][1])
uptime2_2 = int(aa[5][1])
qps2 = delete_2 + insert_2 + insert2_2 + select_2 + update_2
commit=qps2 -qps1
uptime=uptime2_2-uptime1
aa =(commit/uptime)
json_body = [
{
"measurement":'my_tps',
"tags":{
"host": "mycat"
},
"fields":{
"influxdb":"qps1",
"qps":aa
}
}
]
#aa ="query_per_sec host=mycat,role=db,influxdb=qps qps=%d "% (commit/uptime)
#aa =(commit/uptime)
#print aa,json_body
client.write_points(json_body)
break
except MySQLdb.Error,e:
print "MySQL error%d:%s"%(e.args[0],e.args[1])
##腳本需要注意4個(gè)地方:1、連接數(shù)據(jù)庫(kù)的信息,是被監(jiān)控端的
2、MySQL的QPS統(tǒng)計(jì)收集的存儲(chǔ)的數(shù)據(jù)庫(kù)
3、收集的數(shù)據(jù)把字符串轉(zhuǎn)成JSON格式
json_body = [
{
"measurement":'my_tps', ###注意這個(gè)紅色位置不能用雙引號(hào),估計(jì)是PYTHON的問(wèn)題,JAVA用雙引號(hào)沒(méi)問(wèn)題,是個(gè)坑哦~~~!這個(gè)可以設(shè)置你喜歡的,其他不要修改,直接使用
"tags":{
"host": "mycat"
},
"fields":{
"qps":aa
}
}
]
4、需要在influxdb添加一個(gè)數(shù)據(jù)庫(kù),例子名字叫:test_influxdb (自定定義), client=InfluxDBClient(host='192.168.15.104', port=8086, username='root', password='root', database='telegraf'),可根據(jù)實(shí)際定義,后面的GRAFANA會(huì)用到這個(gè)名字
強(qiáng)烈建議對(duì)數(shù)據(jù)庫(kù)的操作通過(guò)終端來(lái)搞:
[root@mycat ~]# influx
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 0.13.0
InfluxDB shell version: 0.13.0
> create database test_influxdb
> show databases;
name: databases
---------------
name
telegraf
_internal
mytab
mydb
stress
test_influxdb
8、在grafana配置收集來(lái)的數(shù)據(jù)信息:
1、添加數(shù)據(jù)源與配置,點(diǎn)擊grafana的圖表的下拉單點(diǎn)data sources---》add data source
2、配置數(shù)據(jù)源信息
3、datshboards->news->左上小綠格->add panel->graph
4、選擇metrics配置如下:
解釋:1、為剛才創(chuàng)建的數(shù)據(jù)源名字
2、添加個(gè) query
3、展開query
4、這里根據(jù)前面的json格式 "measurement":'my_tps' 這個(gè)my_tps,可以理解為時(shí)序數(shù)據(jù)庫(kù)的表。
5、選擇HOST
6、選擇JSON的tags標(biāo)簽的值mycat
"tags":{
"host": "mycat"
7、為JSON的"qps":aa的qsp,相當(dāng)于字段,為field
8、為統(tǒng)計(jì)方式,看其INFLUXDB的基礎(chǔ)介紹
9、統(tǒng)計(jì)時(shí)常
10、為曲線的標(biāo)志,可以在一個(gè)圖里添加多個(gè)query ,這樣每個(gè)名字對(duì)應(yīng)不同的顏色
最后點(diǎn)擊保存按鈕,在最上方有個(gè)圖標(biāo),保存后選擇有4個(gè)方塊里的剛才定義的general的名字
最后效果圖:
##這里多提點(diǎn):通過(guò)這樣的定義,可以收集業(yè)務(wù)上一些常規(guī)的數(shù)據(jù),可以在線的時(shí)時(shí)統(tǒng)計(jì),比如在游戲里的DAU\PCU\ACU....網(wǎng)站的PV 等等都可以數(shù)據(jù)收集可視化展示。。。。。完畢
這個(gè)PYTHON腳本可以優(yōu)化,INFLUXDB可以批量插入數(shù)據(jù),可靠消息次是2-3W寫入是沒(méi)問(wèn)題的。
PYTHON腳本啟動(dòng) python mysql_qps.py & 不加后臺(tái)符會(huì)一直卡著,
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。