Graphing MySQL performance with Prometheus and Grafana

Graphing MySQL performance with Prometheus and Grafana

PREVIOUS POST
NEXT POST

 

This post explains how you can quickly start using such trending tools as Prometheus and Grafana for monitoring and graphing of MySQL and system performance.

Update, February 20, 2017: Since this blog post was published, we have released Percona Monitoring and Management (PMM), which is the easiest way to monitor MySQL and MongoDB using Grafana and Prometheus. You can check the PMM documentation for more detail. If you need need help, PMM is fully supported under Percona Support. Alternatively, community support is available through the Percona Montoring and Management Forums.

I will try to keep this blog as short as possible, so you can quickly set things up before getting bored. I plan to cover the details in the next few posts. I am going to go through the installation process here in order to get some really useful and good-looking graphs in the end.

Overview

PrometheusPrometheus is an open-source service monitoring system and time series database. In short, the quite efficient daemon scrapes metrics from remote machines using HTTP protocol and stores data in the local time-series database. Prometheus provides a simple web interface, a very powerful query language, HTTP API etc. However, the storage is not designed to be durable for the time being.

The remote machines need to run exporters to expose metrics to Prometheus. We will be using the following two:

GrafanaGrafana is an open source, feature-rich metrics dashboard and graph editor for Graphite, Elasticsearch, OpenTSDB, Prometheus and InfluxDB. It is a powerful tool for visualizing large-scale measurement data and designed to work with time-series. Grafana supports different types of graphs, allows for custom representation of individual metrics on the graph and various methods of authentication including LDAP.

Diagram

Here is a diagram of the setup we are going to use:
Prometheus + Grafana diagram

Prometheus setup

To install on the monitor host.

Get the latest tarball from Github.

Create a simple config:

where 192.168.56.107 is the IP address of the db host we are going to monitor and db1 is its short name. Note, the “alias” label is important here because we rely on it in the predefined dashboards below to get per host graphs.

Start Prometheus in foreground:

Now we can access Prometheus’ built-in web interface by http://monitor_host:9090

Prometheus web interface
If you look at the Status page from the top menu, you will see that our monitoring targets are down so far. Now let’s setup them – prometheus exporters.

Prometheus exporters setup

Install on the db host. Of course, you can use the same monitor host for the experiment. Obviously, this node must run MySQL.

Download exporters from here and there.

Start node_exporter in foreground:

Unlike node_exporter, mysqld_exporter wants MySQL credentials. Those privileges should be sufficient:

Create .my.cnf and start mysqld_exporter in foreground:

At this point we should see our endpoints are up and running on the Prometheus Status page:
Prometheus status page

Grafana setup

Install on the monitor host.

Grafana has RPM and DEB packages. The installation is as simple as installing one package.
RPM-based system:

or APT-based one:

Open and edit the last section of /etc/grafana/grafana.ini resulting in the following ending:

Percona has built the predefined dashboards for Grafana with Prometheus for you.

Let’s get them deployed:

It is important to apply the following minor patch on Grafana 2.6 in order to use the interval template variable to get the good zoomable graphs. The fix is simply to allow variable in Step field on Grafana graph editor page. For more information, take a look at PR#3757 and PR#4257. We hope the last one will be released with the next Grafana version.

Those changes are idempotent.

Finally, start Grafana:

At this point, we are one step before being done. Login into Grafana web interface http://monitor_host:3000 (admin/admin).

Go to Data Sources and add one for Prometheus:
Grafana datasource

Now check out the dashboards and graphs. Say choose “System Overview” and period “Last 5 minutes” on top-right. You should see something similar:
Grafana screen
If your graphs are not populating ensure the system time is correct on the monitor host.

Samples

Here are some real-world samples (images are clickable and scrollable):



Enjoy!

Conclusion

Prometheus and Grafana is a great tandem for enabling monitoring and graphing capabilities for MySQL. The tools are pretty easy to deploy, they are designed for time series with high efficiency in mind. In the next blog posts I will talk more about technical aspects, problems and related stuff.

PREVIOUS POST
NEXT POST

Share this post

Comments (39)

  • SuperQ Reply

    In general it’s not correct to configure the node_exporter and the mysqld_exporter in the same job. This comes from the design idea that there may be more nodes or more services on different nodes. You may want to configure different scrape intervals for the node or mysqld.

    February 29, 2016 at 3:03 am
    • Roman Vynar Reply

      I can’t disagree. For simplicity, I have used the minimal config file here.

      In reality, for example, we employ job_name “linux” for node_ and “mysql” for mysqld_exporter in production.

      February 29, 2016 at 3:18 am
      • SuperQ Reply

        Cool, thanks for putting this together, we need more howto guides for full Prometheus configurations. I’m currently working through a backlog of example alert configurations that can be published as “reasonable”

        The big worry we have as a Prometheus community is having too much “copy-n-paste” configuration that doesn’t follow best practices. Or worse does follow best practices, but isn’t appropriate for many uses.

        February 29, 2016 at 5:50 pm
        • Roman Vynar Reply

          Fixed 😉

          February 29, 2016 at 5:53 pm
  • Peter Zaitsev Reply

    I would also note if you’re looking to try things out we have simple to install Docker package as well https://hub.docker.com/r/percona/pmm/

    February 29, 2016 at 7:49 am
  • Marcelo Altmanncelo Altmann Reply

    Hi Roman.
    How it works if I want a custom metric? Let’s say I want to monitor in real time the number of sales (that would be a SELECT COUNT(*) from sales_table). Do I need to write a new exporter ?

    March 1, 2016 at 7:49 am
    • Roman Vynar Reply

      Marcelo, yes, you need a new exporter for this. May be “SQL query result set metrics exporter” from https://prometheus.io/docs/instrumenting/exporters/ will work for you.

      March 1, 2016 at 7:52 am
    • SuperQ Reply

      A new exporter is unnecessary, the performance_schema system provides metrics for this. The mysqld_exporter collector option “-collect.perf_schema.eventsstatements” is disabled by default, but it will easily provide metrics for all queries run against your server.

      There are some limitations
      * You need to be running MySQL >= 5.6
      * Performance Schema must be enabled (defaults work just fine)
      * The queries must not be prepared statements (Horrible design flaw in MySQL’s metrics at fault)
      * You may need to adjust additional flags.

      March 2, 2016 at 4:32 pm
      • SuperQ Reply

        Oh, durr, I didn’t read the question properly. No, you would have to instrument this differently.

        I would suggest adding prometheus metrics directly into your sales application, and not doing database counts.

        March 2, 2016 at 4:35 pm
  • Young Heon Kim Reply

    Awsome !!! It’s so nice.

    March 2, 2016 at 3:20 am
  • saup007 Reply

    how to configure ALERTING?

    March 15, 2016 at 3:33 am
  • KRISHAN KUMAR Reply

    Hi Roman,

    Thanks for putting all information on same page.
    I am also exploring Prometheus and Grafana but I am stuck at last point after installing predefined dashboards for Grafana with Prometheus and restarting grafana.
    Issue i am facing is I am am not able to see any of the graph when i login to grafana UI which is running on port 3000.
    Can you please give me a pointer to figure out what exactly i am missing?

    March 30, 2016 at 4:31 pm
    • Roman Vynar Reply

      Ensure whether the system time is ok and grafana is patched with those 2 sed commands. It is safe to repeat them.

      April 4, 2016 at 4:52 pm
  • vishnu rao Reply

    awesome post. thanks a lot for this.

    May 5, 2016 at 11:37 am
  • Michael Schueler Reply

    What should my prometheus config look like if I want multiple MySQL hosts? I’m about to try the second one except with unique job names for each host, could you confirm what it should look like? thanks

    – job_name: mysql
    target_groups:
    – targets: [‘192.168.56.107:9104′,’192.168.56.108:9104’]
    labels:
    alias: db1

    or

    – job_name: mysql
    target_groups:
    – targets: [‘192.168.56.107:9104’]
    labels:
    alias: db1
    target_groups:
    – targets: [‘192.168.56.108:9104’]
    labels:
    alias: db2

    June 1, 2016 at 6:22 am
  • vishnu Reply

    global:
    scrape_interval: 5s
    evaluation_interval: 5s
    scrape_configs:
    – job_name: linux
    target_groups:
    – targets: [‘localhost:9100’]
    labels:
    alias: localhost
    – job_name: mysql
    target_groups:
    – targets: [‘192.168.56.106:9104’]
    labels:
    alias: db1
    – targets: [‘192.168.56.107:9105’]
    labels:
    alias: db2
    – targets: [‘192.168.56.108:9106’]
    labels:
    alias: db4
    – targets: [‘localhost:9106’]
    labels:
    alias: db3

    June 1, 2016 at 6:28 am
  • AR Reply

    Is it safe to run those node and mysql exporters on the db host? Safe meaning, I’m thinking of deploying them in Production and I want to make sure about that. What is the overhead of those exporters running on the db host? Could it impact my db in anyway?

    September 10, 2016 at 1:17 pm
  • Peter Zaitsev Reply

    Hi,

    I think it is reasonably safe. Overhead will depend a lot on how much data you capture and with what resolution. We put a lot of effort tuning exactly that, for example avoiding capturing table data if it there are too many tables in our packaged solution of Percona Monitoring and Management.

    You can see the demo here http://pmmdemo.percona.com/ and download/install it to check it out.

    September 10, 2016 at 2:40 pm
  • AR Reply

    Thanks Peter, what is your suggestion? Should I go with Nagios or Grafana & Prometheus combo? Also, is it possible to use Grafana with Nagios?

    September 11, 2016 at 5:11 am
  • AR Reply

    Hey Peter, can’t I download the PMM as an RPM for Linux instead of docker? I do not have network access in my server to download the docker container.

    September 15, 2016 at 11:36 am
  • Roman Vynar Reply

    PMM server is distributed as a docker image because it is prebuilt with all the necessary components. There can’t be a regular RPM for it. In the future we may have other images, e.g. VM, AWS etc.

    PMM client is distributed as RPM, DEB or tarball whatever you prefer.

    September 15, 2016 at 11:53 am
    • AR Reply

      Is it possible to download the docker container in one server and then copy it to other servers where network access is not available?

      September 15, 2016 at 12:05 pm
  • Theerayooth Kosin Reply

    Hi Roman Vynar,

    Not sure about wrong configurations on my host, I got this messages on Prometheus/status:

    Get http://localhost:9100/metrics: dial tcp [::1]:9100: getsockopt: connection refused
    Get http://localhost:9104/metrics: dial tcp [::1]:9104: getsockopt: connection refused

    This my prometheus.yml:
    global:
    scrape_interval: 5s
    evaluation_interval: 5s
    scrape_configs:
    – job_name: linux
    target_groups:
    – targets: [‘localhost:9100’]
    labels:
    alias: db_linux
    – job_name: mysql
    target_groups:
    – targets: [‘localhost:9104’]
    labels:
    alias: db_mysql

    And Grafana shows red-box “Dashboard init failed Template variables could not be initialized: parse error at char 33: unexpected end of input inside braces” and there is no any graph on dashboards.

    System info:
    – CentOS 7, MariaDB 10.0.27

    September 17, 2016 at 10:45 pm
  • Roman Vynar Reply

    The latest dashboards do not require “alias” label. Also ensure you have run the Grafana patch. Take a look here https://github.com/percona/grafana-dashboards

    September 19, 2016 at 3:40 am
  • Abilash Reply

    After successfully starting Prometheus, Im getthing the stats of the target is down giving the error as MALFORMED HTTP CODE.

    September 19, 2016 at 3:47 am
  • Abilash Reply

    Yes right, even im getting this error:
    Get http://localhost:9104/metrics: dial tcp [::1]:9104: getsockopt: connection refused

    September 19, 2016 at 6:18 am
  • shaikibrahim04ik Reply

    Hi , I have got everything working but for some reason dashboard shows the error highlighted in this link
    https://github.com/grafana/grafana/issues/6293. It’s show cannot read property 1 of null.

    October 18, 2016 at 2:40 pm
  • shaikibrahim04ik Reply

    Well no issues i just left the step column blank and it started populating graphs. Great Article. Thank You!!!

    October 18, 2016 at 3:00 pm
  • neo-ling Reply

    hello,
    when i execute ./prometheus,there is no any output in ternimal
    [root@SZTW-YSJ-CMS prometheus]# ./prometheus
    [root@SZTW-YSJ-CMS prometheus]# lsof -i:9090
    [root@SZTW-YSJ-CMS prometheus]#
    my system version is centos6.4 x86-64

    October 18, 2016 at 10:39 pm
  • sunny Reply

    hi, thanks for your post first, but i come with a problem ,if i want to install many mysqld instances with different ports ,how can i to collect the different mysql performance data with the mysql_exporter ? may be need i to modify the mysql_exporter or run more than one mysql_exporters to send data ? thanks

    December 15, 2016 at 10:37 pm
  • Roman Vynar Reply

    You need to run 1 mysqld_exporter per mysqld instance.

    December 16, 2016 at 3:55 am
  • shaikibrahim04ik Reply

    Hi.

    I have this set up working perfectly fine in aws environment but in verizon i face an issue read tcp 10.173.35.203:49640->10.173.35.180:9104: i/o timeout which shows up in prometheus status.

    FYI i am able to curl the metrics from the host machine where prometheus is installed. Anybody any clue on this?

    January 24, 2017 at 6:17 am
  • Anil Reply

    Hi,

    First we have to download mysql and we have to create one database and then i am downloading mysqld-exporter and we have to configure docker compose file only these steps we have to do rite..

    If i have to do more steps what steps i have to do?

    February 14, 2017 at 12:41 am
  • Stackbees Reply

    After successfull installation, when open graffana i am not able to see any Dashboard.

    February 19, 2017 at 12:58 am
  • Peter Zaitsev Reply

    Hi,

    We recommend you using Percona Monitoring and Management for Monitoring MySQL with Prometheus and Grafana – it is much easy to set up than putting all the pieces together https://www.percona.com/software/database-tools/percona-monitoring-and-management

    For difficulties with installation you can file Support case if you’re Percona’s customer or use Community Forum:
    https://www.percona.com/forums/questions-discussions/percona-monitoring-and-management

    February 19, 2017 at 11:26 am
  • Jenny Reply

    Thank you for the tutorial!

    I’ve done everything, and most of it works BUT it won’t show anything on the Prometheus status page. When i run node_exporter and mysqld_exporter they both say “Listen on: XXXX” instead of “starting node_exporter”/”starting server”. What should I do?

    Thank you!

    // Jenny

    March 28, 2017 at 5:29 am

Leave a Reply