The most complete network! ! ! Monitor MySQL and Oracle databases based on the prometheus+grafana system

I. Overview

I found a lot of monitoring open source tools on the Internet, intending to monitor Oracle, MySQL and other databases. Finally, I chose Prometheus and Grafana . Compared with Zabbix, it is not too friendly. This article is all built by myself , and it is not copied by many blogs. Paste, the same article moved around, which caused an explosion when looking for related documents, not many beeps, and then I will talk about how to build Prometheus, Grafana environments and monitor MySQL and Oracle.

2. Preliminary environmental preparation

server IP address
Prometheus server 192.168.3.102
Grafana server 192.168.3.103
Monitored server 192.168.3.77
Monitored server 192.168.3.93

192.168.3.77已经安装好了oracle11g,192.168.3.93安装好了MySQL

Friends who don’t know how to install Oracle11g can click on the link below:

Oracle11g silent installation

If you have not installed MySQL, you can click the link below:

MySQL installation
The files involved in the article can be downloaded by clicking , password: xyln

2.1 Turn off the firewall and selinux

#查看防火墙状态
systemctl status firewalld
#关闭防火墙
systemctl stop firewalld
#查看selinux
getenforce
#永久关闭selinux
vi /etc/selinux/config
#将SELINUX=enforcing改为SELINUX=disabled,并重启系统

Insert picture description here

2.2 Configure the hostname

Three hosts are bound to each other with IP and hostname

192.168.3.102 master
192.168.3.103 gra
192.168.3.77 ora

Insert picture description here

2.3 Time synchronization

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

Here I encountered a problem when I was doing it, that is, it was still out of synchronization after the execution, and finally found that the time zone was inconsistent, which caused the time to be different after synchronization.
After execution, enter date to check whether it has been synchronized.
Insert picture description here

Three, Prometheus build

3.1 Upload the prometheus installation package

  • Upload prometheus-2.5.0.linux-amd64.tar.gz
    Insert picture description here
    If you need another version, you can click on the prometheus official website to download the version you need.
  • Unzip
tar -zxvf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/prometheus-2.5.0.linux-amd64/  /usr/local/prometheus

Insert picture description here

3.2 Start Prometheus

  • Start with default configuration file
/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
  • Check if port 9090 is open
ss -anlt | grep 9090

Insert picture description here

3.3 Access to Prometheus interface

Enter the browser http://服务器IP:9090to access the main interface of Prometheus. Note that it 服务器IPis to be replaced. Don't copy it to the browser and ask why there is no interface.
Insert picture description here
Only monitor this machine by default
Insert picture description here

3.3 Host data display

  • Through 服务器IP:9090/metricsyou can view the monitored data
    Insert picture description here

  • Visually query some monitoring items
    Insert picture description here

Four, Grafana build

4.1 What is Grafana

Insert picture description here
As mentioned above, Grafana is an open source monitoring visualization platform, which can analyze, query, and then visualize the collected data, and it supports alarms.

4.2 Grafana

  • Upload the grafana-5.3.4-1.x86_64.rpm installation package, or click the official website to choose to download
    Insert picture description here
  • Installation
    Insert picture description here
    Because the installation using rpm will show missing components, use yum to install, and the missing components will be installed automatically.
  • Start the service and set the startup to start automatically
systemctl start grafana-server 
systemctl enable grafana-server 
#确认端口
ss -anlt | grep 3000

Insert picture description here

4.2 Grafana interface

  • You 服务器IP:3000can enter the main interface of Grafana by visiting through a browser. The
    Insert picture description here
    default user name and password are bothadmin

4.3 Data source configuration

  • Next, add the data collected by the prometheus server as a data source to Grafana, so that Grafana can visualize the data of Prometheus. Insert picture description here

Insert picture description here

Insert picture description here

4.4 Dashboard display

  • Create graphics
    Insert picture description here

  • Edit
    Insert picture description here
    Insert picture description here
    and as long as you have added it, you can see the effect immediately, the needle is good
    Insert picture description here

  • Save
    Insert picture description here

  • carry out
    Insert picture description here

Five, monitor MySQL

5.1 Prometheus monitors MySQL

5.1.1 Upload mysqld_exporter component

Insert picture description here

5.1.2 Install mysqld_exporter component

tar xf mysqld_exporter-0.11.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/mysqld_exporter-0.11.0.linux-amd64/  /usr/local/mysqld_exporter 
ls /usr/local/mysqld_exporter

Insert picture description here

#授权
grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '123'; 
#刷新
flush privileges;
#退出
quit

Insert picture description here

5.1.3 Create mysql configuration file

vi /usr/local/mysqld_exporter/.my.cnf

Insert picture description here
Here user and password should be one-to-one correspondence
Insert picture description here

5.1.4 Start mysqld_exporter

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

Insert picture description here

  • Confirm port9104
ss -anlt | grep 9104

Insert picture description here

5.1.5 Prometheus server configuration

vi /usr/local/prometheus/prometheus.yml

Add these three lines at the end

- job_name: 'mysql'
    static_configs:
    - targets: ['192.168.3.93:9104']

Insert picture description here

Remember here, the format should be paid attention to, otherwise an error will be reported when the prometheus service is turned on.

5.1.5 View monitoring results

  • The service needs to be restarted after changing the configuration file
pkill prometheus
/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
  • Enter,
    Insert picture description here
    you can see that MySQL has been monitored, and you can view some monitoring items.
    Insert picture description here

5.2 Grafana displays MySQL monitoring data

5.2.1 Download and install the dashboard for MySQL monitoring

#进入grafana的lib目录
cd /var/lib/grafana/
  • Upload grafana-dashboards-master.zip
    Insert picture description here
#解压
unzip grafana-dashboards-master.zip

Insert picture description here

cp -R /var/lib/grafana/grafana-dashboards-master/dashboards/ /var/lib/grafana/

Insert picture description here

5.2.2 Change the grafana configuration file

vi /etc/grafana/grafana.ini
  • Add at the end of the file
[dashboards.json] 
enabled = true 
path = /var/lib/grafana/dashboards

Insert picture description here

  • Restart Grafana service
systemctl restart grafana-server

5.2.3 Import Json file

Insert picture description here
Insert picture description here
Insert picture description here

  • After clicking Import, you can directly see the related interface
    Insert picture description here
  • One thing to note here is that the default data source name of the json file is Prometheus. If the data source name does not correspond to the 4.3 data source configuration, there may be no data. If you don’t want to change the data source configuration, you can also Choosing to change the data source name of the json file is more cumbersome and needs to be changed more, but you can change it by wildcard replacement, which is not described here.
    Insert picture description here

Six, monitor Oracle

6.1 Configure Go environment

Since Prometheus is developed with golang, first install the Go environment.

  • Upload the file go1.16.2.linux-amd64.tar.gz
    Insert picture description here
  • Unzip
tar -xvf go1.16.2.linux-amd64.tar.gz -C /usr/local/
  • Configure environment variables
vi /etc/profile
#go environment
export PATH=$PATH:/usr/local/go/bin

Insert picture description here
Every time you change the configuration file, remember to refresh it to take effect

source /etc/profile
  • Test whether the installation is successful
go version

Insert picture description here

6.2 Configure Oracle Client

6.2.1 Upload Oracle Client File

Insert picture description here

6.2.2 Install Oracle client files

rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm

Insert picture description here

#创建文件夹
mkdir -p /usr/lib/oracle/11.2/client64/network/admin
#创建监听文件
vi /usr/lib/oracle/11.2/client64/network/admin/tnsnames.ora
#/usr/lib/oracle/11.2/client64/network/admin/tnsnames.ora
ORCL11G =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.105)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )
#更改权限
chmod -R 755 /usr/lib/oracle/
chown -R oracle:oinstall  /usr/lib/oracle/

Insert picture description here

6.2.3 Configure Oracle local environment variables

su - oracle
vi ~/.bash_profile
export ORACLE_HOME2=/usr/lib/oracle/11.2/client64
export ORACLE_SID=orcl11g.us.oracle.com
export NLS_LAN=GAMERICAN_AMERICA.AL32UTF8
export PATH=$PATH:$ORACLE_HOME2
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME2/lib

Insert picture description here
Check the environment variable settings
Insert picture description here

6.3 Configure exporter

  • Upload exporter file
    #### 6.3 Configure exporter
  • Unzip and authorize
tar -xvf oracledb_exporter.0.2.2.linux-amd64.tar.gz
chmod u+x oracledb_exporter.0.2.2.linux-amd64/oracledb_exporter
mv oracledb_exporter.0.2.2.linux-amd64 oracledb_exporter
mv oracledb_exporter /usr/local/
chown oracle:oinstall /usr/local/oracledb_exporter/

Insert picture description here

  • Set execution environment variables
vi ~/.bash_profile

export DATA_SOURCE_NAME=username/password@ database service name

export DATA_SOURCE_NAME=system/11111111

Insert picture description here

  • Start service
./oracledb_exporter -log.level error -web.listen-address IP:9161
  • Some dependencies are missing
    Insert picture description here
  • Enter the lib library to check, there is indeed no
    Insert picture description here
  • Import missing dependencies
    Insert picture description here
  • So I changed the way to start
nohup ./oracledb_exporter &

Insert picture description here

  • Test visit
http://服务器ip:9161/metrics

Remember that it is the server IP, do not copy and paste it directly into the address bar and then think that there is no response--
You can see the list of Oracle monitoring indicators
Insert picture description here

6.4 Prometheus server configuration

vi /usr/local/prometheus/prometheus.yml
  - job_name: '105_oracle'
    static_configs:
    - targets: ['192.168.3.105:9161']

Insert picture description here

6.5 Check the monitoring effect

  • The service needs to be restarted after changing the configuration file
pkill prometheus
/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

Insert picture description here

  • Some monitoring items can be viewed
    Insert picture description here

6.6 Grafana displays Oracle monitoring data

6.6.1 Download and install Oracle monitoring dashboard

You can click on Grafana's official website to choose your favorite monitoring template
Insert picture description here
Insert picture description here

6.6.2 Change the grafana configuration file

In this step, if you have set up monitoring MySQL before, you don’t need to set it again. If you haven’t set it, check 5.2.2

6.6.3 Import json file

So https://grafana.com/api/dashboards/3333/revisions/1/download
Insert picture description here
far, the basic environment of Prometheus+Grafana and how to use the combination of the two to monitor MySQL and Oracle are over. It took a week to write this blog. If it is helpful to you, please comment more. Like the collection, if there is something wrong, please correct me! Thank you! ! !

Guess you like

Origin blog.csdn.net/orzMrXu/article/details/114871945