[TOC]
# 通過zabbix官方模板監控mysql
## 準備環境
- 服務端:Linux CentOs7.2 192.168.0.62
- 客戶端:Linux CentOs7.2 192.168.0.62
- zabbix版本:4.0.1 LTS
## 客戶端
### 1.為數據庫監控創建用戶
生成環境上需要注意授權用戶可管理的數據庫,比如只能監控zabbix的庫
```
mysql -uroot -p
grant all on zabbix.* to 'zabbix'@'localhost' identified by 'zabbix';
flush privileges;
```
### 2.創建.my.cnf文件
```
vim /etc/zabbix/.my.cnf
[client]
user=zabbix
host=127.0.0.1
password=zabbix
```
### 3.修改userparameter_mysql.conf
yum安裝的agent默認就存在該文件,路徑為
/etc/zabbix/zabbix_agentd.conf.d/userparameter_mysql.conf
```
# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.
# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].
# Key syntax is mysql.status[variable].
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/var/lib/zabbix mysql -N | awk '{print $$2}'
# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].
# Key syntax is mysql.size[<database>,<table>,<type>].
# Database may be a database name or "all". Default is "all".
# Table may be a table name or "all". Default is "all".
# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".
# Database is mandatory if a table is specified. Type may be specified always.
# Returns value in bytes.
# 'sum' on data_length or index_length alone needed when we are getting this information for whole database instead of a single table
UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/var/lib/zabbix mysql -N'
UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V
```
其中HOME=/var/lib/zabbix和yum安裝的路徑不符,需要修改成HOME=/etc/zabbix
所以修改后的文件為
```
# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.
# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].
# Key syntax is mysql.status[variable].
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/etc/zabbix mysql -N | awk '{print $$2}'
# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].
# Key syntax is mysql.size[<database>,<table>,<type>].
# Database may be a database name or "all". Default is "all".
# Table may be a table name or "all". Default is "all".
# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".
# Database is mandatory if a table is specified. Type may be specified always.
# Returns value in bytes.
# 'sum' on data_length or index_length alone needed when we are getting this information for whole database instead of a single table
UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/etc/zabbix mysql -N'
UserParameter=mysql.ping,HOME=/etc/zabbix mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V
```
**生效需要重啟agent**
```
systemctl restart zabbix-agent
```
### 測試執行情況
- 客戶端測試
```
先使用root賬戶執行
HOME=/etc/zabbix sudo mysqladmin ping | grep -c alive
1
再使用zabbix賬戶執行
sudo -u zabbix sudo HOME=/etc/zabbix mysqladmin ping | grep -c alive
1
如果出錯,一般都是zabbix權限不夠的問題,可以看后面的sudo權限提升
```
- 服務端測試
```
zabbix_get -s 192.168.0.131 -k "mysql.status[Bytes_sent]"
167002
```
## 服務端
為目標主機添加模板鏈接Template DB MySQL

查看生成的圖形

## sudo提升
> 默認普通用戶無法使用sudo
> 需要修改/etc/sudoers文件,默認文件屬性為440
> -r--r-----. 1 root root 3972 Jul 14 16:03 /etc/sudoers
直接使用visudo修改
如果你希望之后執行sudo命令時不需要輸入密碼,那么可以形如,注意修改行下需空一行,退出重新登錄后生效
```
root ALL=(ALL) ALL
zabbix ALL=(ALL) NOPASSWD:/var/lib/mysql/bin #可以無密碼使用mysqld restart命令
```
使用不同賬戶,執行執行腳本時候sudo經常會碰到 sudo: sorry, you must have a tty to run sudo這個情況,其實修改一下sudo的配置就好了
```
注釋掉 Default requiretty 一行
#Default requiretty
```
意思就是sudo默認需要tty終端。注釋掉就可以在后臺執行了。
要測試用戶權限是否生效可以使用
```
sudo -u happy sudo /var/lib/zabbix/mysqld restart
```