目录
上传mysqld_exporter,配置mysqld 主从复制的指标暴露器
一、主从复制部署(数据库已经安装好)
主从复制
#主从服务器配置
yum install ntp -y
vim /etc/ntp.conf
yum -y install ntpdate ntp #安装ntp软件
ntpdate ntp.aliyun.com #时间同步
systemctl start ntpd
systemctl stop firewalld.service
setenforce 0
#主服务器
vim /etc/my.cnf
#开启二进制日志文件(之后生成的日志名为master-bin)
log_bin=master-bin
#开启从服务器日志同步
log_slave=updates=true
#主服务器id为1(不可重复)
server_id = 1
[root@master ~]# mysql -uroot -p123456
mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.255.%' IDENTIFIED BY '123456';
mysql> flush privileges;
mysql> show master status;
#从服务器
vi /etc/my.cnfvi /etc/my.cnf
#开启二进制日志文件
log-bin=master-bin
#设置server id为2,slave2 为3
server_id = 2
#从主服务器上同步日志文件记录到本地
relay-log=relay-log-bin
#定义relay-log的位置和名称(index索引)
relay-log-index=slave-relay-bin.index
mysql -uroot -p123456
mysql> change master to
mysql>
master_host='192.168.255.180',master_user='myslave',master_password='123456',master_log_file='master-bin.000002',master_log_pos=604;
mysql> start slave;
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
主服务器授权
数据库授权,(master 节点执行)
create user 'exporter'@'%' identified by '123456';
create user 'exporter'@'127.0.0.1' identified by '123456';
create user 'exporter'@'localhost' identified by '123456';
grant process,replication client,select on *.* to 'exporter'@'%' identified by 'admin123';
grant process,replication client,select on *.* to 'exporter'@'127.0.0.1' identified by 'admin123';
grant process,replication client,select on *.* to 'exporter'@'localhost' identified by 'admin123';
上传mysqld_exporter,配置mysqld 主从复制的指标暴露器
wget http://101.34.22.188/mysql_exporter/mysqld_exporter-0.12.1.linux-amd64.tar.gz
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local
进入目录,创建配置文件(主从)
cd /usr/local/mysqld_exporter-0.12.1.linux-amd64/
cat > /usr/local/mysqld_exporter-0.12.1.linux-amd64/my.cnf << EOF
[client]
user=exporter
password=admin123
EOF
cat > /usr/lib/systemd/system/mysqld_exporter.service << EOF
[Unit]
Description=mysqld_exporter
After=network.target
[Service]
User=root
Type=simple
ExecStart=/usr/local/mysqld_exporter-0.12.1.linux-amd64/mysqld_exporter \
--config.my-cnf /usr/local/mysqld_exporter-0.12.1.linux-amd64/my.cnf \
--collect.info_schema.processlist
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start mysqld_exporter.service
netstat -natp | grep 9104
配置 mysql 服务器指标暴露器(主从)
wget http://101.34.22.188/mysql_exporter/node_exporter-1.1.2.linux-amd64.tar.gz
tar zxvf node_exporter-1.1.2.linux-amd64.tar.gz -C /opt
cd /opt
mv node_exporter-1.1.2.linux-amd64 node_exporter
cat > /usr/lib/systemd/system/node_exporter.service << EOF
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=root
ExecStart=/opt/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start node_exporter && systemctl enable node_exporter
netstat -natp | grep 9100
二、Prometheus + Grafana 部署
部署 prometheus
wget http://101.34.22.188/prometheus/prometheus-2.27.1.linux-amd64.tar.gz
tar zxvf prometheus-2.27.1.linux-amd64.tar.gz -C /usr/local
修改配置文件
cd /usr/local/prometheus-2.27.1.linux-amd64
vim prometheus.yml
##最后插入:
- job_name: 'mysql-master-slave' #定义主从复制的job
scrape_interval: 5s #指标数据采集周期
static_configs: #静态采集方式
- targets: ['192.168.10.60:9104','192.168.10.70:9104'] #定义targets
- job_name: 'nodes' #定义nodes主机信息job
scrape_interval: 5s
static_configs:
- targets: ['192.168.10.60:9100','192.168.10.70:9100']
#无注释复制
- job_name: 'mysql-master-slave'
scrape_interval: 5s
static_configs:
- targets: ['192.168.10.60:9104','192.168.10.70:9104']
- job_name: 'nodes'
scrape_interval: 5s
static_configs:
- targets: ['192.168.10.60:9100','192.168.10.70:9100']
启动 prometheus
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64]# nohup ./prometheus &
[1] 3158
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64]# nohup: ignoring input and appending output to ‘nohup.out’
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64]# ps aux|grep prometheus
root 3158 1.5 0.5 777424 44632 pts/0 Sl 17:13 0:00 ./prometheus
root 3171 0.0 0.0 112660 972 pts/0 S+ 17:14 0:00 grep --color=auto prometheus
登录查看
部署 grafana-server
wget http://101.34.22.188/grafana/grafana-7.3.6-1.x86_64.rpm
yum -y install grafana-7.3.6-1.x86_64.rpm
systemctl start grafana-server
账号密码默认为 admin,admin
grafana 默认配置文件目录 /etc/grafana/grafana.ini
可直接访问 ip:3000 进入 grafana 控制台
默认输入账号密码:admin admin
创建 data sources 选择 prometheus
定义 prometheus 节点 http://192.168.10.100:9090
-> 点击 save & text
点击 "+" 输入 import ,选择模板 7371,选择 prometheus 数据源 -> save 保存 mysqld-master-slave 监控
点击 "+" 输入 import ,选择模板 8919,选择 prometheus 数据源 -> save 保存 mysqld nodes 监控
点击 dashboard 选择以上两个模板