Prometheus简单安装及问题记录

  • Post author:
  • Post category:其他


一、Prometheus安装

1、服务器准备:

(1)关闭防火墙

# systemctl stop firewalld

# systemctl disable firewalld

# iptables -F


(2)时间同步

yum install -y  ntpdate && ntpdate time.windows.com

2、安装Prometheus(官网提供的是二进制版,解压就能用,不需要编译)

从 https://prometheus.io/download/ 下载相应版本,安装到服务器上

(1)安装

tar -zxvf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local/
ln -s /usr/local/prometheus-2.5.0.linux-amd64/  /usr/local/prometheus


(2)启动

/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &


(3)查询

ps -ef|grep prometheus

netstat -tunlp|grep prometheus     # 查询端口 9090

(4)登录

通过浏览器http://服务器IP:9090

3、监控远程Linux主机

(1)安装node_exporter组件(远程客户端)

从 https://prometheus.io/download/ 下载node_exporter组件

tar -zxvf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/
ln -s /usr/local/node_exporter-1.3.1.linux-amd64/ /usr/local/node_exporter


(2)启动组件服务

nohup /usr/local/node_exporter/node_exporter &


(3)查询

ps -ef|grep node

netstat -tunlp |grep node          # 端口 9100

(4)添加到Prometheus服务器(注意缩进)

 echo '
- job_name: "host_mysqlServer"
    static_configs:
        - targets: ["10.1.35.236:9100"] ' >> /usr/local/prometheus/prometheus.yml

(5)重启prometheus服务

pkill prometheus
/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

(5)查看

回到web管理界面(http://服务器IP:9090) –》点Status –》点Targets –》可以看到多了一台监 控目标

4、监控mysql服务器

(1)安装组件

            tar -zxvf mysqld_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local/
            ln -s /usr/local/mysqld_exporter-0.14.0.linux-amd64/ /usr/local/mysqld_exporter

(2)登录mysql创建监控用户(!!!!注意mysql8.0授权时一定要注意localhost和127.0.0.1的区别!!!!)

create user 'mysql_monitor'@'localhost' identified by 'admin';
grant select,process,replication client on *.* to 'mysql_monitor'@'localhost';
flush privileges;


(3)创建mysql配置文件用于监控连接

cd /usr/local/mysql_monitor/

vim /usr/local/mysqld_exporter/.my.cnf 
[client] 
host=127.0.0.1
port=33070
user=mysql_monitor
password=admin


(4)启动组件服务

nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &


(5)查询

ps -ef|grep mysqld_exporter

netstat -tunlp |grep mysqld_exporter          # 端口 9104

(6)添加到Prometheus服务器

 echo '
     - job_name: "Mysql"
     static_configs:
        - targets: ["10.1.35.236:9104"] ' >> /usr/local/prometheus/prometheus.yml


(7)重启prometheus服务

pkill prometheus
/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

(8)查看

回到web管理界面(http://服务器IP:9090) –》点Status –》点Targets –》可以看到多了一台监 控目标

遇到的问题:

1、Prometheus绘图时没有mysql没有监控项

排查日志发现:less /usr/local/mysqld_exporter/nohup.out

ts=2022-03-22T08:40:58.651Z caller=mysqld_exporter.go:303 level=info msg="Listening on address" address=:9104
ts=2022-03-22T08:40:58.651Z caller=tls_config.go:195 level=info msg="TLS is disabled." http2=false
ts=2022-03-22T08:41:10.188Z caller=exporter.go:149 level=error msg="Error pinging mysqld" err="Error 1045: Access denied for user 'exporter'@'127.0.0.1' (using password: YES)"

查看授权:

mysql> show grants for mysql_monitor@localhost;
+----------------------------------------------------------------------------+
| Grants for mysql_monitor@localhost                                              |
+----------------------------------------------------------------------------+
| GRANT SELECT, PROCESS, REPLICATION CLIENT ON *.* TO `mysql_monitor@localhost` |
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)


直接使用用户登录mysql能成功!!!


找了很多资料都没有解决问题:

使用环境变量方式登录(同样未解决):

export DATA_SOURCE_NAME='mysql_monitor:admin@(localhost:33070)'
/usr/local/mysqld_exporter/mysqld_exporter &


查看mysql日志(终于找到问题):

重新授权解决问题:

update mysql.user set host='127.0.0.1' where user='mysql_monitor';
flush privileges;


总结:



1、注意mysql8.0授权时localhost和127.0.0.1的区别,使用mysql_exporter插件时需要给用户授权host为127.0.0.1



2、环境变量为第一顺位生效方式,也就是说如果设置了环境变量,那么不再读取.my.cnf配置文件。



3、没有设置环境变量,也没有指定配置文件–config.my-cnf时默认读取家目录下.my-cnf



4、注意插件所在目录的权限问题,本次是偷懒用root操作,生产部署需要权限最小化以规范。



版权声明:本文为lee_fengyun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。