After we have installed Zabbix Server on Ubuntu 16.04 LTS. In this article will demonstrate how to use zabbix agent to monitor MySql / Mariadb database Server by collecting data about queries, slow queries, server status, server uptime …
Environment:
- Hostname = db01.yallalabs.com
- IP Address = 192.168.1.50
- OS = Ubuntu 16.04 LTS
Setup a MySql/MariaDB Monitoring User:
root@web01:~# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.17-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create user 'zabbix_admin'@'localhost' IDENTIFIED BY 'Password'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT USAGE ON *.* TO 'zabbix_admin'@'localhost' IDENTIFIED BY 'Password'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye
Configure Zabbix Agent
– To monitor MySql / MariaDB we have to create a file called userparameter_mysql.conf in /etc/zabbix/zabbix_agentd.conf.d, will it be used to collect data:
root@web01:~# vi /etc/zabbix/zabbix_agentd.conf.d/userparameter_mysql.conf
– Add this lines
# 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}' # My line # # 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[*],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$([[$ # #Default below UserParameter=mysql.ping,HOME=/etc/zabbix mysqladmin ping | grep -c alive # #My line UserParameter=mysql.uptime,HOME=/etc/zabbix mysqladmin status | cut -f2 -d ":" | cut -f1 -d "T" | tr -d " " UserParameter=mysql.threads,HOME=/etc/zabbix mysqladmin status | cut -f3 -d ":" | cut -f1 -d "Q" | tr -d " " UserParameter=mysql.questions,HOME=/etc/zabbix mysqladmin status | cut -f4 -d ":"|cut -f1 -d "S" | tr -d " " UserParameter=mysql.slowqueries,HOME=/etc/zabbix mysqladmin status | cut -f5 -d ":" | cut -f1 -d "O" | tr -d " " UserParameter=mysql.qps,HOME=/etc/zabbix mysqladmin status | cut -f9 -d ":" | tr -d " " UserParameter=mysql.version,mysql -V
Next, we need to give Zabbix the login information for our new user. Create a file called .my.cnf in /etc/zabbix and fill it with these lines:
root@web01:~# vi /etc/zabbix/.my.cnf # [mysql] user=zabbix_admin password=Password [mysqladmin] user=zabbix_admin password=Password
– Restart the zabbix agent service using the following command:
root@web01:~# systemctl restart zabbix-agent
– Finally, you can setup the host in your Zabbix server by selecting the MySQL Server template and you’ll start getting a data flow and triggers about the status of your database server:
We hope this tutorial was enough Helpful. If you need more information, or have any questions, just comment below and we will be glad to assist you!
30 comments
Hello, can you make this script for Windows Server?
thank you
In the future, I’m going to explain how to monitor MS SQL using Zabbix.
hi,first at all thank you,
i have an error …
i put .my.cnf to this address “/etc/zabbix/”
and change “HOME=” variable from “userparameter_mysql.conf” to this “HOME=/etc/zabbix”
and i still get this error when restart zabbix-agent
zabbix-agentd [23414]: invalid entry “.my.cnf file with password information.” (not following “parameter=value” notation) in config file “/etc/zabbix/zabbix-agentd.conf.d/userparameter_mysql.conf”, line 2
————————-[.my.cnf]————————————
[mysql]
user=zabbix_admin
password=Password
[mysqladmin]
user=zabbix_admin
password=Password
————————————[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=/etc/zabbix mysql -N | awk ‘{print $$2}’
# My line
#
# 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 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[*],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$([[$
#
#Default below
UserParameter=mysql.ping,HOME=/etc/zabbix mysqladmin ping | grep -c alive
#
#My line
UserParameter=mysql.uptime,HOME=/etc/zabbix mysqladmin status | cut -f2 -d “:” | cut -f1 -d “T” | tr -d ” ”
UserParameter=mysql.threads,HOME=/etc/zabbix mysqladmin status | cut -f3 -d “:” | cut -f1 -d “Q” | tr -d ” ”
UserParameter=mysql.questions,HOME=/etc/zabbix mysqladmin status | cut -f4 -d “:”|cut -f1 -d “S” | tr -d ” ”
UserParameter=mysql.slowqueries,HOME=/etc/zabbix mysqladmin status | cut -f5 -d “:” | cut -f1 -d “O” | tr -d ” ”
UserParameter=mysql.qps,HOME=/etc/zabbix mysqladmin status | cut -f9 -d “:” | tr -d ” ”
UserParameter=mysql.version,mysql -V
——————————[zabbix-config]———————————-
zabbix = Zabbix 3.0.0alpha4
with mysql
and
Include=/etc/zabbix/zabbix-agentd.conf.d/userparameter_mysql.conf
Hi,
Try to recreate the file and try again .
Which distribution are you using ?Maybe it’s a just a permission problem check the permission of the directory and the file ..
i have solved the problem:
note : Home must be “/var/lib/zabbix”
and your code for user parameter must change in row
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‘
Hi, you can choose the /etc/zabbix directory I don’t think it will create problems . Can you please indicate your distribution os?
hi,
os = opensuse 42.2
i probably sure this address is used in some where in zabbix ~ …
but i config more than 5 host with this OS
any way … thanks to you 😉
By default zabbix use this directory /var/lib/zabbix . Can you please provide us with your new config userparameter_mysql.conf file .
Thanks
Hi Lotfi,
It’s me again. Finally get that zabbix of mine working.
Now, i want to monitor replication on my database server. Did you happen to have the tutorial how to set it up ?
Thanks.
Thanks a lot for the guide. Just a mention: In CentOS 7 the file userparameter_mysql.conf is on the path /etc/zabbix/zabbix_agentd.d/
Thx Tudor.
Thanks a lot for this information. I have followed it’s steps and it worked on all my MySQL servers.
I am very glad that I could find a such nice and complete explanation regarding MySQL monitoring with Zabbix.
Hi Nicolae,
You are welcome anytime, keep updated by subscribing to our YouTube channel.
hi Wadernim
I need to get the list of the triggers are configured for each server from RDS Zabbix DB
the information that your are searching is under the “functions” Table using those paramters “itemid” for the host and ” triggerid” for the triggers .
Try this query ” select * from triggers t,items i,functions f where i.itemid=f.itemid and f.triggerid=t.triggerid”
This work very well, but if I stop the mysql (mariadb) service, I’m not see any trigger in the server. Zabbix does not report any problem with mysql. Any idea????
Hi Fidel,
By default in the mysql zabbix template the only trigger created is the ping to check the status of the mysql/ mariadb. Try to verify if is getting the output value correctly ( should be 1 or 0) using the zabbix_get command and make Make sure that Selinux doesnt block zabbix-agent connection to the mysql.
This works as expected but when you stop mysql (mariadb) in the server zabbix does not seee the change and do not report the problem… Do you know what meed to be done to fix this?
I think it doesn’t work because the zabbix front-end doesn’t gather the trigger from the local database. Try to test the trigger with another db server .
Good luck .
HI , I have multiple mysql instances ( running with docker ) on a same server.
command like mysqladmin works fine from the local server, but I should add username and password and port separately for every database :
mysqladmin -h -P 1000 -u root –password=mypass1 status
mysqladmin -h -P 1001 -u root –password=mypass2 status
mysqladmin -h -P 1002 -u root –password=mypass3 status
How can I use your UserParameters and .my.cnf file to have all items for every container ?
thank you !
-h container_IP1 , 2 and 3
Hi Mohammad,
Honestly, there’s a template but we never test it before, check this link https://github.com/firaxis/mysql-monitoring
i don’t understand…. in the zabbix front-end (version 3.4.12) have a template called Template DB MySQL, why then this instruction???
and you don’t explain, after this steps, template “Template app mysql” must be create in the front-end automatically or not ??? Thanks!
Hi Andrey,
It the same default template in the zabbix version 3.4.x should be called “Template DB MySQL” .Just link that one and it should work.
Good Luck.
awesome thanks it’s solved my big problem.
Hi LOTFI,
One of my colleague refer me the same portal and Really this was much helpful for me.
I appreciate to your work . Thanks LOTFI ^-^
Hi Abhi,
We are so glad that you like our blog. Keep in touch by subscribing.
Thank you and good luck.
What Zabbix expression should I use to monitor slow query
e.g {hostname:mysql.ping.last(0)}=0
Hi,
the default MySQL Server template has already a parameter to monitor slow queries
What is the solution to this problem?
please
MySQL: Failed to get items (no data for 30m)