运维监控系统之Prometheus-MySQLl监控

运维 系统运维
mysqld_exporter是用于获取mysql实例的指标服务。

 简介

mysqld_exporter是用于获取mysql实例的指标服务。


安装

mysql授权

  1. CREATE USER exporter@localhost identified by "mima"
  2. GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost' WITH MAX_USER_CONNECTIONS 3; 
  3. flush privileges

 直接通过命令下载就可以了。

  1. wget -t 1000 -c https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz 
  2. tar zxvf  mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /opt/ 
  3. cd /opt/mysqld_exporter-0.12.1.linux-amd64 
  4. export DATA_SOURCE_NAME='exporter:mima@(localhost:3306)/' 
  5. ./mysqld_exporter 

 这里设置用户名密码是通过环境变量的形式,也可以用配置文件,默认找的配置文件是~/.my.cnf,也可以使用参数--config.my-cnf="/opt/mysqld_exporter/.my.cnf" 进行设置。

  1. ./mysqld_exporter --config.my-cnf="/opt/mysqld_exporter/.my.cnf" 

其他参数

  1. usage: mysqld_exporter [<flags>] 
  2.  
  3. Flags: 
  4.   -h, --help                   Show context-sensitive help (also try --help-long and --help-man). 
  5.       --exporter.lock_wait_timeout=2   
  6.                                Set a lock_wait_timeout on the connection to avoid long metadata locking. 
  7.       --exporter.log_slow_filter   
  8.                                Add a log_slow_filter to avoid slow query logging of scrapes. NOTE: Not supported by Oracle MySQL. 
  9.       --collect.heartbeat.database="heartbeat"   
  10.                                Database from where to collect heartbeat data 
  11.       --collect.heartbeat.table="heartbeat"   
  12.                                Table from where to collect heartbeat data 
  13.       --collect.info_schema.processlist.min_time=0   
  14.                                Minimum time a thread must be in each state to be counted 
  15.       --collect.info_schema.processlist.processes_by_user   
  16.                                Enable collecting the number of processes by user 
  17.       --collect.info_schema.processlist.processes_by_host   
  18.                                Enable collecting the number of processes by host 
  19.       --collect.info_schema.tables.databases="*"   
  20.                                The list of databases to collect table stats foror '*' for all 
  21.       --collect.mysql.user.privileges   
  22.                                Enable collecting user privileges from mysql.user 
  23.       --collect.perf_schema.eventsstatements.limit=250   
  24.                                Limit the number of events statements digests by response time 
  25.       --collect.perf_schema.eventsstatements.timelimit=86400   
  26.                                Limit how old the 'last_seen' events statements can be, in seconds 
  27.       --collect.perf_schema.eventsstatements.digest_text_limit=120   
  28.                                Maximum length of the normalized statement text 
  29.       --collect.perf_schema.file_instances.filter=".*"   
  30.                                RegEx file_name filter for performance_schema.file_summary_by_instance 
  31.       --collect.perf_schema.file_instances.remove_prefix="/var/lib/mysql/"   
  32.                                Remove path prefix in performance_schema.file_summary_by_instance 
  33.       --web.listen-address=":9104"   
  34.                                Address to listen on for web interface and telemetry. 
  35.       --web.telemetry-path="/metrics"   
  36.                                Path under which to expose metrics. 
  37.       --timeout-offset=0.25    Offset to subtract from timeout in seconds. 
  38.       --config.my-cnf="/home/jalright/.my.cnf"   
  39.                                Path to .my.cnf file to read MySQL credentials from
  40.       --collect.mysql.user     Collect data from mysql.user 
  41.       --collect.info_schema.tables   
  42.                                Collect metrics from information_schema.tables 
  43.       --collect.info_schema.innodb_tablespaces   
  44.                                Collect metrics from information_schema.innodb_sys_tablespaces 
  45.       --collect.info_schema.innodb_metrics   
  46.                                Collect metrics from information_schema.innodb_metrics 
  47.       --collect.global_status  Collect from SHOW GLOBAL STATUS 
  48.       --collect.global_variables   
  49.                                Collect from SHOW GLOBAL VARIABLES 
  50.       --collect.slave_status   Collect from SHOW SLAVE STATUS 
  51.       --collect.info_schema.processlist   
  52.                                Collect current thread state counts from the information_schema.processlist 
  53.       --collect.perf_schema.tablelocks   
  54.                                Collect metrics from performance_schema.table_lock_waits_summary_by_table 
  55.       --collect.perf_schema.eventsstatements   
  56.                                Collect metrics from performance_schema.events_statements_summary_by_digest 
  57.       --collect.perf_schema.eventsstatementssum   
  58.                                Collect metrics of grand sums from performance_schema.events_statements_summary_by_digest 
  59.       --collect.perf_schema.eventswaits   
  60.                                Collect metrics from performance_schema.events_waits_summary_global_by_event_name 
  61.       --collect.auto_increment.columns   
  62.                                Collect auto_increment columns and max values from information_schema 
  63.       --collect.binlog_size    Collect the current size of all registered binlog files 
  64.       --collect.perf_schema.tableiowaits   
  65.                                Collect metrics from performance_schema.table_io_waits_summary_by_table 
  66.       --collect.perf_schema.indexiowaits   
  67.                                Collect metrics from performance_schema.table_io_waits_summary_by_index_usage 
  68.       --collect.info_schema.userstats   
  69.                                If running with userstat=1, set to true to collect user statistics 
  70.       --collect.info_schema.clientstats   
  71.                                If running with userstat=1, set to true to collect client statistics 
  72.       --collect.info_schema.tablestats   
  73.                                If running with userstat=1, set to true to collect table statistics 
  74.       --collect.info_schema.schemastats   
  75.                                If running with userstat=1, set to true to collect schema statistics 
  76.       --collect.perf_schema.file_events   
  77.                                Collect metrics from performance_schema.file_summary_by_event_name 
  78.       --collect.perf_schema.file_instances   
  79.                                Collect metrics from performance_schema.file_summary_by_instance 
  80.       --collect.perf_schema.replication_group_member_stats   
  81.                                Collect metrics from performance_schema.replication_group_member_stats 
  82.       --collect.perf_schema.replication_applier_status_by_worker   
  83.                                Collect metrics from performance_schema.replication_applier_status_by_worker 
  84.       --collect.engine_innodb_status   
  85.                                Collect from SHOW ENGINE INNODB STATUS 
  86.       --collect.heartbeat      Collect from heartbeat 
  87.       --collect.slave_hosts    Scrape information from 'SHOW SLAVE HOSTS' 
  88.       --collect.info_schema.innodb_cmp   
  89.                                Collect metrics from information_schema.innodb_cmp 
  90.       --collect.info_schema.innodb_cmpmem   
  91.                                Collect metrics from information_schema.innodb_cmpmem 
  92.       --collect.info_schema.query_response_time   
  93.                                Collect query response time distribution if query_response_time_stats is ON
  94.       --collect.engine_tokudb_status   
  95.                                Collect from SHOW ENGINE TOKUDB STATUS 
  96.       --log.level="info"       Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal] 
  97.       --log.format="logger:stderr"   
  98.                                Set the log target and format. Example: "logger:syslog?appname=bob&local=7" or "logger:stdout?json=true" 
  99.       --version                Show application version. 

 部署服务

直接注册成systemd服务。

  1. ```bash 
  2. #!/bin/bash 
  3.  
  4. VERSION="0.12.1" 
  5.  
  6. wget  -t 100  -c  https://github.com/prometheus/mysqld_exporter/releases/download/v${VERSION}/mysqld_exporter-${VERSION}.linux-amd64.tar.gz 
  7.  
  8. if [ ! -e mysqld_exporter-${VERSION}.linux-amd64.tar.gz ] 
  9. then 
  10.     echo "安装包下载失败" 
  11.     exit 1 
  12. fi 
  13. tar xvfz mysqld_exporter-${VERSION}.linux-amd64.tar.gz -C /opt/ 
  14. cd /opt 
  15. ln -s mysqld_exporter-${VERSION}.linux-amd64  mysqld_exporter 
  16. cat > /etc/systemd/system/mysqld_exporter.service <<EOF 
  17.  
  18. [Unit] 
  19. Description=mysqld_exporter 
  20. After=network.target 
  21.  
  22. [Service] 
  23. Type=simple 
  24. WorkingDirectory=/opt/mysqld_exporter 
  25. ExecStart=/opt/mysqld_exporter/mysqld_exporter --config.my-cnf="/opt/mysqld_exporter/.my.cnf" 
  26. LimitNOFILE=65536 
  27. PrivateTmp=true 
  28. RestartSec=2 
  29. StartLimitInterval=0 
  30. Restart=always 
  31.  
  32. [Install] 
  33. WantedBy=multi-user.target 
  34. EOF 
  35. ​``` 
  36. systemctl daemon-reload 
  37. systemctl enable mysqld_exporter 
  38. systemctl start mysqld_exporter 

 配置prometheus Job

默认端口是9104

  1. - job_name: 'mysqld_exporter' 
  2.  
  3.   # metrics_path defaults to '/metrics' 
  4.   # scheme defaults to 'http'
  5.  
  6.   static_configs: 
  7.   - targets: ['localhost:9104'

 指标展示

指标可以通过prometheus的WebUI进行查看

  1. http://[promethe server ip]:9090 

如果需要画图,可以直接使用grafana,有人已经配置好了图形可以通过grafana官方下的dashboard找到mysqld_exporter相关的dashboard就可以直接使用了,配置好prometheus数据源,直接导入grafana就可以直接展示了。

 

责任编辑:姜华 来源: 今日头条
相关推荐

2020-12-30 08:09:46

运维Prometheus 监控

2020-12-28 10:13:32

运维Prometheus监控

2021-07-07 05:46:46

运维监控Prometheus

2020-12-17 09:25:46

运维Prometheus监控

2020-12-30 05:34:25

监控PrometheusGrafana

2022-07-11 13:43:51

Prometheus监控

2011-03-21 14:43:42

2023-10-11 09:58:07

2013-04-12 13:30:47

2016-04-06 10:02:23

手机微博运维监控

2014-07-22 10:06:43

运维监控虚拟化

2018-09-27 08:59:29

2010-07-09 12:09:34

IT运维Mocha BSM摩卡软件

2011-03-25 13:54:00

Nagios

2011-01-05 15:39:44

2022-02-08 10:21:17

运维应用监控

2015-09-23 16:46:54

架构监控运维自动化

2022-11-08 00:00:00

监控系统Prometheus

2009-03-09 21:25:11

Linuxnagios开源

2020-11-26 09:10:36

Prometheus
点赞
收藏

51CTO技术栈公众号