ubuntu 20.04 下安装MYSQL mariaDB prometheus监控mysql

时间:2023-01-18 00:50:07

1.安装mariaDB

1.更新源或者替换源

apt-get update

2.安装

apt-get install mariadb-server


启动和停止
service mysql start 或/etc/init.d/mysql start —启动
service mysql stop或/etc/init.d/mysql stop —停止
service mysql restart —重启
service mysql status —查看状态


3.启动mysql
systemctl start mariadb 启动服务
systemctl enable mariadb 开机自启动

ss -natlp |grep 3306 //查看端口是否开启

4.设置密码
mysql_secure_installation

5.修改vim /etc/mysql/mariadb.conf.d/50-server.cnf文件中的bind-address = 127.0.0.1
配置,改为0.0.0.0(不修改 外部客户端不能连接)

如下
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0


5. 在数据库里创建mysql账号用户用于prometheus收集数据
进入mysql
mysql

6.建立用户mysql_monitor和密码12345678
grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '12345678';

退出
exit

##备注: 此处是prometheus服务器来找mysql_exporter,然后mysql_exporter 在找mariadb,##
##所以这里用的是localhost,指的是mysql_exporter的ip##

2.监控MYSQL

mysqld_exporter 下载地址:​​https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz​

1. 上传mysqld_exporter-0.14.0.linux-amd64.tar.gz到/usr/local 并解压缩
tar xf mysqld_exporter-0.14.0.linux-amd64.tar.gz

2.文件名字太长,重命名一下:
mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter

3. 在mysql_exporter中设置mysql配置信息(上面设置的用户名和密码)

vim .my.cnf (手工创建)

[client]
user=mysql_monitor
password=12345678

5. 启动mysql_exporter
进入目录
cd /usr/local/mysqld_exporter

启动服务 并后台启动
nohup ./mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &

6.查看端口(9104)
ss -naltp |grep 9104
LISTEN 0 128 *:9104 *:* users:(("mysqld_exporter",pid=68249,fd=3))

3.配置prometheus拉取mysql节点信息

在prometheus的server端进行修改

vim /usr/local/prometheus/prometheus.yml 

增加如下

- job_name: 'mariadb'
static_configs:
- targets:
- 10.10.201.150:9104

ubuntu 20.04 下安装MYSQL mariaDB prometheus监控mysql

重启prometheus服务

从启动prometheus
systemctl restart prometheus

查看端口
lsof -i:9090

或者下面命令
ss -naltp |grep 9090
LISTEN 0 128 [::]:9090 [::]:* users:(("prometheus",pid=84662,fd=9))

4. grafana加载MYsql

模板号为:7362 或者 6239

ubuntu 20.04 下安装MYSQL mariaDB prometheus监控mysql