Prometheus+Grafana visual monitoring [MySQL status]

1. Install Docker

Note: I use the script I wrote before to install Docker. If you already have Docker, please omit this step. Docker is installed to facilitate the deployment of MySQL database. If you already have a database, you can omit the first two steps.

Click to get the Docker offline installation script

tar zxf docker20.10.14Install.tar.gz
cd docker20.10.14Install
bash install.sh

Check the Docker status as shown below, which means there is no problem:

systemctl status docker

Insert image description here

2. Install the MySQL database (Docker container method)

1. Prerequisite preparation:

docker pull mysql:8.0.27
mkdir /data/db -p

systemctl stop firewalld
systemctl disable firewalld
iptables -F

2. Run the container:

docker run -itd --name mysql -e MYSQL_ROOT_PASSWORD=NTQ34tg*@19VF \
	 -v /data/db:/var/lib/mysql  -p 3306:3306 -v /etc/localtime:/etc/localtime \
	  --restart=always mysql:8.0.27

3. Test whether the container can be connected

docker exec -it  mysql  mysql -uroot -pNTQ34tg*@19VF

Of course, you can use tools to test the connection, as shown below:
Insert image description here

Just make sure the database can be accessed normally!

4. Create a MySQL monitoring user

create user "prometheus"@"%" identified by 'NTQ34tg*@19VF';
grant select,replication client,process ON *.* to "prometheus"@"%";
flush privileges;

3. Install Prometheus

1. Time and time zone synchronization

timedatectl set-timezone Asia/Shanghai
yum -y install ntpdate
/usr/sbin/ntpdate -u ntp1.aliyun.com

Configure scheduled task synchronization time

echo "0 5 * * * /usr/sbin/ntpdate -u ntp1.aliyun.com >/dev/null &" >> /var/spool/cron/root
crontab -l

2. Install Prometheus

wget https://github.com/prometheus/prometheus/releases/download/v2.42.0/prometheus-2.42.0.linux-amd64.tar.gz

tar zxf prometheus-2.42.0.linux-amd64.tar.gz 
mv prometheus-2.42.0.linux-amd64 /usr/local/prometheus

3. Configure systemd management

cat > /usr/lib/systemd/system/prometheus.service << EOF
[Unit]

[Service]
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
Alias=dbus-org.fedoraproject.FirewallD1.service
EOF

4. Start and set up auto-start at boot

systemctl enable prometheus --now
systemctl status prometheus

Insert image description here
The display is as shown above, which means that there is no problem with Prometheus. The default port is 9090. We can access it with a browser.
Click Status> Targetsto view Prometheus' own metrics as shown below:
Insert image description here

4. Install Grafana

1. Install Grafana

wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.4.1-1.x86_64.rpm
sudo yum install grafana-enterprise-9.4.1-1.x86_64.rpm -y

systemctl enable grafana-server.service --now

2. WEB page verification.
The default port is 3000. When you visit for the first time, you will be prompted to reset the password, as shown below:
Insert image description here
Insert image description here

5. Pronetheus and Grafana are related

Insert image description here
Insert image description here
Insert image description here

6. Install mysqld_exporter

1. Install mysqld_exporter

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz
tar zxf mysqld_exporter-0.14.0.linux-amd64.tar.gz 
mv mysqld_exporter-0.14.0.linux-amd64 /usr/local/mysqld_exporter

2. Configure Prometheus to monitor database user information

cd /usr/local/mysqld_exporter
cat > .my.cnf <<EOF
[client]
user=prometheus
password=NTQ34tg*@19VF
EOF

3. Configure systemd management

cat > /usr/lib/systemd/system/mysqld_exporter.service << EOF
[Unit]
Description=mysqld_exporter Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
ExecReload=/bin/kill -HUP \$MAINPID
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=multi-user.target
EOF

Start and join auto-start at boot

systemctl enable mysqld_exporter --now
systemctl status  mysqld_exporter

Insert image description here

The default port is 9104

ss -anput |grep 9104

4. Specify mysqld_exporter information in the Prometheus configuration file

vim /usr/local/prometheus/prometheus.yml

  - job_name: "MySQL_115"
    static_configs:
      - targets: ["16.32.15.115:9104"]

After adding the configuration file, use the command to test whether there are any problems with the format.

cd  /usr/local/prometheus/
./promtool check config prometheus.yml

Restart Prometheus

systemctl restart prometheus.service

7. Grafana adds MySQL monitoring template

The template ID of MySQL is used here: 7362
click 下方红圈地方>>import输入模板ID
Insert image description here

Insert image description here
The final rendering is as follows:
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_45310323/article/details/132841225