Monitoring MySQL with mysqld-exporter


TL; DR: I use mysqld-exporter with Prometheus to monitor my MySQL server.

The mysqld-exporter runs as a container in my Kubernetes cluster. A sample manifest looks like:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mysqld-exporter
  name: mysqld-exporter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysqld-exporter
  template:
    metadata:
      labels:
        app: mysqld-exporter
    spec:
      containers:
        - args:
            # read client credential from the file
            - --config.my-cnf=/etc/mysql/my.cnf 
            - --collect.engine_innodb_status
            - --collect.binlog_size
            - --collect.global_status
            - --collect.global_variables
            - --collect.info_schema.innodb_metrics
            - --collect.info_schema.processlist
            - --collect.info_schema.tables
            - --collect.info_schema.tablestats
            - --collect.info_schema.userstats
            - --collect.perf_schema.eventswaits
            - --collect.perf_schema.file_events
            - --collect.perf_schema.indexiowaits
            - --collect.perf_schema.tableiowaits
            - --collect.perf_schema.tablelocks
            - --collect.perf_schema.eventsstatements
          image: docker.io/prom/mysqld-exporter:v0.15.1
          imagePullPolicy: IfNotPresent
          livenessProbe:
            httpGet:
              path: /
              port: http-metrics
              scheme: HTTP
          name: mysqld-exporter
          ports:
            - containerPort: 9104
              name: http-metrics
              protocol: TCP
          readinessProbe:
            httpGet:
              path: /
              port: http-metrics
              scheme: HTTP
          resources:
            requests:
              cpu: 50m
              memory: 50Mi
          volumeMounts:
            - mountPath: /etc/mysql
              name: my-cnf-volume
      volumes:
        - configMap:
            defaultMode: 420
            name: my-cnf
          name: my-cnf-volume      

---
apiVersion: v1
kind: Service
metadata:
  annotations:
    prometheus.io/port: "9104"
    prometheus.io/scrape: "true"
  labels:
    app: mysqld-exporter
  name: mysqld-exporter
spec:
  ports:
  - name: http-metrics
    port: 9104
    protocol: TCP
    targetPort: 9104
  selector:
    app: mysqld-exporter
  type: ClusterIP
  
---
# for mysql user setup, see
# https://hub.docker.com/r/prom/mysqld-exporter/
apiVersion: v1
data:
  my.cnf: |
    [client]
    host=<ip of the mysql server>
    user=mysqld-exporter
    password=mysqld-exporter
kind: ConfigMap
metadata:
  labels:
    app.kubernetes.io/instance: mysqld-exporter
  name: my-cnf

🙂