Last Updated on December 31, 2022 by Thiago Crepaldi
From previous posts, we have deployed Zabbix Server on a Proxmox container. We also hardened the installation through Let’s Encrypt certificates for the web frontend and TLS encryption for the communication between Server and Agents/Proxies, after all, although encrypted Zabbix Server <-> Agent/Proxy communication with PSK or SSL certificates 🙂
In this post I will cover how to monitor a MySQL database using Zabbix Agent 2 template. Templates are great because they already pack a bunch of monitoring items, actions, triggers, etc that we would have rto add one by one otherwise. Using template is smart because when we need to make changes, we just do it in the template and all hosts that use them will see the new settings. On a manual setup, you would have to make the change in each host, one by one…
This post assumes you already installed and configured your Zabbix Agent, but if you didn’t, refer to How to install and configure Zabbix agent and resume from here when you are done.
Configuring MySQL or MariaDB server
Strictly speaking, this step may not be needed if you already have a read-only MySQL user that can be used to list databases. However, I like to have separate users for each task.
I will assume your database installation has a “root” with admin permission, but each installation may have its own credentials. On a shell terminal, run:
# mysql -u root -p
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12128
Server version: 10.3.36-MariaDB-0+deb10u2 Debian 10
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY '<password>';
MariaDB [(none)]> GRANT REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
MariaDB [(none)]> \q
That will create a “zbx_monitor” user basic permissions to list databases, processes, views, connected clients, etc but no access to the content of the database itself.
Configuring Zabbix Server through Web UI
The steps are quite simple. Go to Configuration >> Hosts and select your agent. When the configuration dialog open, the Host tab should be the default one. Look for the Templates section. In the text box, type “MySQL” and select the “MySQL by Zabbix agent 2” in the search result. This will link the template to your agent.
The last step is setting your database credentials in the plugin so that it can connect to the server and fetch metadata.Click on Macros tab. We will need to add 3 macros to specify address, username and password Click on Add and fill in the new row as follow:
- Macro: {$MYSQL.DSN}
- Value: tcp://localhost:3306 (or whatever IP/FQDN of the database server)
- Description: A helpful description or leave it empty
Click on Add again:
- Macro: {$MYSQL.USER}
- Value: zbx_monitor (or whatever user your database you have)
- Description: A helpful description or leave it empty
Click on Add one last time:
- Macro: {$MYSQL.PASSWORD}
- Value: **************
- Description: A helpful description or leave it empty
Complete the configuration by clicking Update. If everything went well, after a couple seconds, depending on your configuration, data should start flowing into Zabbix Server.
Go to Monitoring >> Latest data. In the filter section, type the name of your database server in the Hosts box and MySQL in the Name box and finally hit Apply. A list of items should be displayed for it.
That is it, have fun!