容器安装mysqld-exporter

1.拉取镜像
[root@localhost ~]# docker pull prom/mysqld-exporter
2.MySQL创建用户并授权
# mysql -uroot -ppassword
# docker exec -it <mysql_container_id> mysql -uroot -ppassword

mysql> CREATE USER 'exporter'@'localhost' IDENTIFIED BY '123456' WITH MAX_USER_CONNECTIONS 2;
mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
mysql> FLUSH PRIVILEGES;

注:localhost需要改为mysqld-exporter安装机器IP,如果和mysql安装在同一台服务器则不需要改动
3.创建文件.my.cnf
[root@localhost ~]# mkdir -p /data/mysqld_exporter
[root@localhost ~]# cd /data/mysqld_exporter
[root@localhost ~]# vim .my.cnf

[client]
host=127.0.0.1
user=exporter
password=123456
4.创建容器
[root@localhost ~]# docker run -d -p 9104:9104 --name mysqld-exporter --restart=always --volume=/data/mysqld_exporter/.my.cnf:/etc/mysql/my.cnf:ro prom/mysqld-exporter --config.my-cnf=/etc/mysql/my.cnf

参数说明:
-d:后台运行
-p: 端口映射
-name:设置容器名称
–restart:设置开机启动
–volume:目录映射
–config.my-cnf:指定配置文件
5.使用docker-compose启动
# 配置docker-compose.yml文件
添加以下内容
version: '3.8'

services:
  mysqld-exporter:
    image: prom/mysqld-exporter
    container_name: mysqld-exporter
    restart: always
    network_mode: host
    ports:
      - "9104:9104"
    volumes:
      - /data/mysqld_exporter/my.cnf:/etc/mysql/my.cnf:ro
    command: --config.my-cnf=/etc/mysql/my.cnf


# docker-compose up mysqld-exporter -d

浏览器输入:http://ip:9104/metrics,可获得 MySQL 监控数据即成功
6.配置Prometheus配置文件
- job_name: 'mysqld_exporter'
    static_configs:
      - targets: ['监控的服务器ip:9104']
        labels:
          instance: 项目1mysql


#添加完成重启Prometheus
7.配置grafana可视化仪表盘

导入仪表盘

在Grafana中添加Prometheus数据源,然后从Grafana官网导入MySQL监控仪表盘(ID:7362)。

 二进制安装mysqld-exporter

1.下载安装包
 
# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz
# tar -xzvf mysqld_exporter-0.15.1.linux-amd64.tar.gz
# mv mysqld_exporter-0.15.1.linux-amd64 /usr/local/mysqld_exporter
 2.创建监控用户
CREATE USER 'exporter'@'mysqld_exporter-ip' IDENTIFIED BY '123456' WITH MAX_USER_CONNECTIONS 2;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'mysqld_exporter-ip';
FLUSH PRIVILEGES;

注:mysqld_exporter-ip为安装mysqld_exporter服务器ip
3.创建配置文件 
# cd /usr/local/mysqld_exporter

# vim .my.cnf

[client]
host=MySQL服务器ip
user=exporter
password=123456
port=3306
4.创建启动文件
cat > /etc/systemd/system/mysqld_exporter.service <<EOF
[Unit]
Description=mysqld_exporter
After=network.target
 
[Service]
Type=simple
User=root
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
Restart=on-failure
 
[Install]
WantedBy=multi-user.target
EOF


$ systemctl daemon-reload
$ systemctl start mysqld_exporter
$ systemctl status mysqld_exporter
$ systemctl enable mysqld_exporter
$ netstat -nltp | grep 9104
5.配置Prometheus配置文件
- job_name: 'mysqld_exporter'
    static_configs:
      - targets: ['监控的服务器ip:9104']
        labels:
          instance: 项目1mysql


#添加完成重启Prometheus

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐