prometheus监控数据库 📅 2026/6/29 20:06:16 #安装数据库wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpmyum -y localinstall mysql80-community-release-el7-3.noarch.rpmyum search mysql --showduplicatesyum -y install mysql-community-server-8.0.21-*systemctl enable mysqldsystemctl start mysqld#首次登录查初始密码grep temporary password /var/log/mysqld.logmysql -uroot -p#重新设置密码(8.0不允许弱密码)alter user rootlocalhost identified by Aspirine1FLUSH PRIVILEGES;#alter user 修改用户信息#‘root’‘localhost’ root指定用户localhost指定主机#创建一个用户CREATE USER mysql_exporterlocalhost IDENTIFIED BY Exporter123456;#授予权限grant ... on *.* mysql_exporterlocalhost;#这条是标准给mysqld_exporter监控账号的最小权限不需要增删改、建表等高风险权限只保留监控所需读取权限符合最小权限原则。GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO mysql_exporterlocalhost;FLUSH PRIVILEGES;exit;#创建一个系统用户useradd -m exporter #-m参数自动创建用户家目录/home/exportercd /home/exporter#部署mysql_exporter#导入包并解压进入目录cd mysqld_exporter-0.15.0.linux-amd64#创建并编辑配置文件vi /home/exporter/.my.cnf[client]usermysql_exporterpasswordExporter123456socket/var/lib/mysql/mysql.sock#加固权限——只要文件所有者exporter用户能读写chown -R exporter:exporter /home/exporter/.my.cnfchmod 600 /home/exporter/.my.cnf#编写system服务启动vim /etc/systemd/system/mysqld_exporter.service[Unit]DescriptionMySQL Exporter For PrometheusAfternetwork.target mysqld.service[Service]UserexporterGroupexporterWorkingDirectory/home/exporter/mysqld_exporter-0.18.0.linux-amd64ExecStart/home/exporter/mysqld_exporter-0.18.0.linux-amd64/mysqld_exporter \--config.my-cnf/home/exporter/.my.cnf \--web.listen-address:9104RestartalwaysRestartSec5[Install]WantedBymulti-user.target#加载生效systemctl daemon-reloadsystemctl enable --now mysqld_exporter#去prometheus添加target#插件mysql监控数据当前总连接数已建立连接mysql_global_status_threads_connected#最大允许连接数mysql_global_variables_max_connections#连接使用率mysql_global_status_threads_connected / mysql_global_variables_max_connections * 100#每秒新建连接速率rate(mysql_global_status_connections[1m])#每秒新建连接速率rate(mysql_global_status_connections[1m])