How to Install MySQL Exporter for Prometheus 2.0 on Ubuntu 16.04


This is really old and unmaintained article. If you see any mistakes and outdated content, please contact me, the email is in the footer of the site

For this post I will assume that you already have Prometheus 2.0 installed.

If not, follow one of these two tutorials:

Exporters are generally installed in the similar fashion to each other. This installation will be similar to Alertmanager installation I have presented in a previous post.

Installing From Source or Using a Binary?

You have two options. One option is to setup go environment and to compile the binary yourself. Second option is to download the already compiled binary. In any case, you will have to visit the official repo.

Making a Binary From Source

In case you want to compile it yourself, you will have to set up the go language environment. To set it up, follow this tutorial (skip to the section named: Set up golang environment).

When you are done with it, you can compile the binary like this:

go get github.com/prometheus/mysqld_exporter

The binary is now located in your go directory, for example $HOME/go/bin/mysqld_exporter.

Downloading a Binary

If you just want to download binary, go to the official repo, and locate the version you want under the releases tab. All you need to do then is to untar the archive:

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz
tar xvzf mysqld_exporter-0.10.0.linux-amd64.tar.gz

The binary is located in the extracted directory, and it is named mysqld_exporter.

Installation

First, create a separate user that will be used to run this application:

sudo adduser --no-create-home --disabled-login --shell /bin/false --gecos "MySQL exporter user" mysql_exporter
```> This is really old and unmmaintained article. If you see any mistakes and outdated content, please contacnt me, the email is in the footer of the site_exporter/.my.cnf

sudo chown -R mysql_exporter:mysql_exporter /etc/mysql_exporter
sudo chmod 600 /etc/mysql_exporter/.my.cnf

.my.cnf file will contain user name and password configuration for you MySQL database. For safety reasons we make this file readable only to the owner.

Next, copy your binary to appropriate directory in filesystem hierarchy. For this part of tutorial, I will assume that you have download binary.

sudo cp mysqld_exporter-0.10.0.linux-amd64/mysqld_exporter /usr/local/bin/
sudo chown mysql_exporter:mysql_exporter /usr/local/bin/mysqld_exporter

Now, we have to configure three files.

Configure .my.cnf

This file is easy to configure. First, open the file using your favorite editor (yeah, nano is my choice, I’m not joining vim/emacs wars anytime soon):

sudo nano /etc/mysql_exporter/.my.cnf

Then paste this, and replace with your user name and password.

[client]
user=REPLACE_WITH_YOUR_USER
password=REPLACE_WITH_YOUR_PASSWORD

Add MySQL Exporter as systemd unit

Open with editor a new systemd service as:

sudo nano /etc/systemd/system/mysql_exporter.service

Copy and paste the following configuration:

[Unit]
Description=Prometheus MySQL Exporter
After=network.target

[Service]
User=mysql_exporter
Group=mysql_exporter
Type=simple
ExecStart=/usr/local/bin/mysqld_exporter \
    --config.my-cnf="/etc/mysql_exporter/.my.cnf"
Restart=always

[Install]
WantedBy=multi-user.target

Enable the new service on startup, reload daemons and start the exporter service.

sudo systemctl daemon-reload
sudo systemctl enable mysql_exporter
sudo systemctl start mysql_exporter

Configure prometheus.yml

You have to notify Prometheus to scrape the newly installed exporter. Paste this under scrape_configs tag, and modify the location of the server according to your needs:

scrape_configs:
  - job_name: 'mysql_exporter'
    scrape_interval: 5s
    static_configs:
      - targets:
        - localhost:9104

Do not forget to restart Prometheus daemon. Now you can try to write a rule to check whether your mysql server is up (hint: check the value of mysql_up). That’s it. You’re good to go.

We're not spammers, and you can opt-out at any moment. We hate spam as much as you do.

powered by TinyLetter