Last Updated on
If you work with MySQL or PostgreSQL, probably you at least heard about PMM (Percona Monitoring and Management). If you want to test it, you may also be interested in this article: I’ll show you how to setup a MySQL and a MariaDB instance and monitor them in the quickest way. Which is Docker.
Docker is also my favourite way of running PMM in production. To be clear, I wouldn’t run a production DBMS in Docker. But now we are talking about testing something quickly on your laptop, without wasting too much time. And I don’t know a faster way.
Note: PMM is not a single piece of software. It is a solution created by making use of existing software projects, like Grafana, Prometheus and Orchestrator. In this post I want to hide this complexity, even if the cost is a possible oversimplification. I hope you don’t mind if I’ll keep writing PMM even when I could mean Grafana or Prometheus.
For more information, see Overview of Percona Monitoring and Management Architecture.
Running and monitoring MySQL/MariaDB in Docker
Linus Torvalds said “Talking is cheap, show me the code”. Let’s avoid long discussions, let’s code! But after that, you’ll also find some notes which explain why we did some weird things. After all, we should always understand what we do (ha ha ha).
Talking is cheap…
Here is what we will do, at a high level:
- Create a Docker network;
- Start PMM in Docker;
- Start a MySQL or MariaDB container, or both;
- Install the PMM client in the DBMSs containers.
…show me the code!
Enough babbling. Here is the code.
docker network create test-pmm docker pull percona/pmm-server:latest docker create \ -v /opt/prometheus/data \ -v /opt/consul-data \ -v /var/lib/mysql \ -v /var/lib/grafana \ --name pmm-data \ --network test-pmm \ percona/pmm-server:latest /bin/true docker run -d \ -p 80:80 \ --volumes-from pmm-data \ --name pmm-server \ --restart always \ --network test-pmm \ percona/pmm-server:latest
http://localhost with your browser. You should see PMM up and working. But it is not interesting, because it is not monitoring anything… well, almost. Actually PMM contains Prometheus, which always monitors itself. So you can click on
Insight -> Prometheus.
Now create your MySQL container and/or your MariaDB container:
docker run --name x-mysql-8.0 -h x-mysql-8.0 \ -e MYSQL_ROOT_PASSWORD=secret -d --network test-pmm mysql:8.0 docker run --name x-mariadb-10.3 -h x-mariadb-10.3 \ -e MYSQL_ROOT_PASSWORD=secret -d --network test-pmm mariadb:10.3
Now open BASH in your containers:
docker exec -ti x-mysql-8.0 bash
mysql client and run these queries (the PMM agent needs to have a MySQL user):
CREATE USER email@example.com IDENTIFIED BY 'my_secret'; GRANT SELECT, PROCESS, SUPER, REPLICATION CLIENT, RELOAD ON *.* TO firstname.lastname@example.org; GRANT SELECT, UPDATE, DELETE, DROP ON performance_schema.* TO email@example.com;
And now, in the containers BASH, let’s run these commands to install and configure
apt-get update apt-get install -y wget wget https://repo.percona.com/apt/percona-release_latest.generic_all.deb dpkg -i percona-release_latest.generic_all.deb apt --fix-broken install -y dpkg -i percona-release_latest.generic_all.deb apt-get update mkdir /etc/init apt-get install pmm-client pmm-admin config --server pmm-server:80 pmm-admin add linux:metrics pmm-admin add mysql:metrics --host 127.0.0.1 --user pmm --password my_secret pmm-admin add mysql:queries --host 127.0.0.1 --user pmm --password my_secret
At this point, if you go back to Grafana, you should see MySQL metrics appear.
At some point, you may want to destroy these container and the bridge network, leaving the system clean. It’s simple:
docker rm -f pmm-server docker rm -f pmm-data docker rm -f x-mariadb-10.3 docker rm -f x-mysql-8.0 docker network rm test-pmm
Since we have done some weird things, it’s good to document them.
It is not possible to change the hostname of a running Docker container. Trying to run the hostname command will fail. Fortunately, we can set the hostname at creation time, with the
But why is it important? Because Grafana shows a menu with the names of the monitored instanced. We want to see readable names, not random sequences of characters.
Add Percona repository twice
The first time that you try to add Percona repo (
dpkg -i percona-release_latest.generic_all.deb) the command will fail. Most likely because the
lsb-release package is not present, and it fails to install it. After that, we run
apt to fix dependency problems. Then we retry to add the repository, and this time we succeed.
I reported this bug: ENG-97.
UPDATE: Not a bug, see link and comments below.
Trying to install the
pmm-client package will fail if the
/etc/init directory is not present. In MySQL and MariaDB containers, we have to create it manually.
I reported this bug: PMM-3520.
UPDATE: Fix released.
Strange failures in MySQL container
pmm-admin add commands failed for me in the MySQL container. Well, they only apparently failed. When I checked PMM, I saw that the container was monitored. So, ignore errors in the consolle and check if PMM is showing metrics.
So, everything worked? Let me know if you had any problems.
To also monitor ProxySQL, see this post:
Monitoring ProxySQL with PMM in Docker