文章目录
环境
系统 | 主机名 | IP | 部署服务 |
---|---|---|---|
Rocky-9.3 | mysql-master | 192.168.117.10 | MYSQL主 |
Rocky-9.3 | mysql-backup | 192.168.117.11 | MYSQL备 |
Rocky-9.3 | mysql-subordinate | 192.168.117.12 | MYSQL从 |
Rocky-9.3 | nfs | 192.168.117.13 | NFS服务器 |
注:1.关闭所有系统的防火墙和selinux
2.keepalived的vip是192.168.117.200
部署NFS服务器
192.168.117.13
[root@nfs ~]# yum -y install nfs-utils //安装nfs软件包
[root@nfs ~]# systemctl start nfs-server //启动nfs服务
[root@nfs ~]# systemctl enable nfs-server //设置nfs开机自启
[root@nfs ~]# mkdir /data //创建/data共享目录
[root@nfs ~]# vim /etc/exports //修改系统文件,配置共享目录
/data
[root@nfs ~]# systemctl restart nfs-server //更改配置需要重启服务
配置将客户端所有用户映射为一个用户
[root@nfs ~]# useradd -r -M -s /sbin/nologin -u 978 -g 977 mysql //主和备和nfs的mysql用户的uid,gid需要一样
[root@nfs ~]# chown -R mysql.mysql /data
[root@nfs ~]# id mysql
uid=978(mysql) gid=977(mysql) groups=977(mysql)
[root@nfs ~]# vim /etc/exports //给予客户端网络权限
/data 192.168.117.10(rw,anonuid=978,anongid=977,all_squash)
/data 192.168.117.11(rw,anonuid=978,anongid=977,all_squash)
[root@nfs ~]# systemctl restart nfs-server
[root@nfs ~]# setfacl -m u:mysql:rwx /data //给与客户端本地权限,客户端的root用户会映射为mysql用户
[root@nfs ~]# exportfs -r //使配置立刻生效
[root@nfs ~]# setfacl -m u:mysql:rwx /data
192.168.117.10
[root@mysql-master ~]# yum -y install nfs-utils //安装nfs软件包
[root@mysql-master ~]# mkdir /opt/data
[root@mysql-master ~]# vim /etc/fstab
/dev/mapper/rl-root / xfs defaults 0 0
UUID=1e4d1c93-64d0-4217-b231-3110487340fe /boot xfs defaults 0 0
/dev/mapper/rl-home /home xfs defaults 0 0
/dev/mapper/rl-swap none swap defaults 0 0
192.168.117.13:/data /opt/data nfs defaults,_netdev 0 0 //添加此行defaults,_netdev为识别为网络设备
[root@mysql-master ~]# systemctl daemon-reload //使配置生效
[root@mysql-master ~]# mount -a //立刻挂载
192.168.117.11
[root@mysql-backup ~]# yum -y install nfs-utils //安装nfs软件包
[root@mysql-backup ~]# mkdir /nfs
[root@mysql-backup ~]# vim /etc/fstab
/dev/mapper/rl-root / xfs defaults 0 0
UUID=1e4d1c93-64d0-4217-b231-3110487340fe /boot xfs defaults 0 0
/dev/mapper/rl-home /home xfs defaults 0 0
/dev/mapper/rl-swap none swap defaults 0 0
192.168.117.13:/data /opt/data nfs defaults,_netdev 0 0 //添加此行defaults,_netdev为识别为网络设备
[root@mysql-backup ~]# systemctl daemon-reload //使配置生效
[root@mysql-backup ~]# mount -a //立刻挂载
部署mysql
192.168.117.10
[root@mysql-master ~]# tar xf mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz -C /usr/local/
[root@mysql-master ~]# cd /usr/local/
[root@mysql-master local]# mv mysql-8.0.35-linux-glibc2.28-x86_64 mysql
创建mysql用户
[root@mysql-master local]# useradd -r -M -s /sbin/nologin mysql
[root@mysql-master local]# chown -R mysql.mysql /usr/local/mysql
[root@mysql-master local]# id mysql //主和备和nfs的mysql用户的uid,gid需要一样
uid=978(mysql) gid=977(mysql) groups=977(mysql)
添加环境变量
[root@mysql-master local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql-master local]# source /etc/profile.d/mysql.sh
软连接
[root@mysql-master local]# ln -s /usr/local/mysql/include /usr/include/mysql
编辑mysql.conf
[root@mysql-master local]# vi /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
编辑mysql.server
[root@mysql-master ~]# vi /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/opt/data
编写my.cnf配置文件
[root@mysql-master ~]# vi /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/data
pid-file=/opt/data/mysql.pid
port=3306
skip-name-resolve
user=mysql
socket = /tmp/mysql.sock
初始化mysql
[root@mysql-master local]# mysqld --initialize --user mysql --datadir /opt/data
root@localhost: /LSz2EOjtvuy
编辑mysqld.service
[root@mysql-master ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@mysql-master ~]# vi /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
开机自启mysql
[root@mysql-master ~]# systemctl daemon-reload
[root@mysql-master ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
设置mysql密码
[root@mysql-master ~]# mysql -uroot -p'/LSz2EOjtvuy'
mysql> alter user root@'localhost' identified with mysql_native_password by 'xia123!';
Query OK, 0 rows affected (0.01 sec)
192.168.117.11
[root@mysql-backup ~]# tar xf mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz -C /usr/local/
[root@mysql-backup ~]# cd /usr/local/
[root@masmysql-backupter local]# mv mysql-8.0.35-linux-glibc2.28-x86_64 mysql
创建mysql用户
[root@mysql-backup local]# useradd -r -M -s /sbin/nologin mysql
[root@mysql-backup local]# chown -R mysql.mysql /usr/local/mysql
[root@mysql-backup local]# id mysql
uid=978(mysql) gid=977(mysql) groups=977(mysql)
添加环境变量
[root@mysql-backup local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql-backup local]# source /etc/profile.d/mysql.sh
软连接
[root@mysql-backup local]# ln -s /usr/local/mysql/include /usr/include/mysql
编辑mysql.conf
[root@mysql-backup local]# vi /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
编辑mysql.server
[root@mysql-backup ~]# vi /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/opt/data
备数据库不需要初始化和设置密码
编写my.cnf配置文件
[root@mysql-backup ~]# vi /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/data
pid-file=/opt/data/mysql.pid
port=3306
skip-name-resolve
user=mysql
socket = /tmp/mysql.sock
编辑mysqld.service
[root@mysql-backup ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@mysql-backup ~]# vi /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
开机自启mysql
[root@mysql-backup ~]# systemctl daemon-reload
[root@mysql-backup ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
192.168.117.12
[root@mysql-subordinate ~]# tar xf mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz -C /usr/local/
[root@mysql-subordinate ~]# cd /usr/local/
[root@mysql-subordinate local]# mv mysql-8.0.35-linux-glibc2.28-x86_64 mysql
创建mysql用户
[root@mysql-subordinate local]# useradd -r -M -s /sbin/nologin mysql
[root@mysql-subordinate local]# chown -R mysql.mysql /usr/local/mysql
[root@mysql-subordinate local]# id mysql //从数据库用户不需要一样
uid=978(mysql) gid=977(mysql) groups=977(mysql)
添加环境变量
[root@mysql-subordinate local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql-subordinate local]# source /etc/profile.d/mysql.sh
软连接
[root@mysql-subordinate local]# ln -s /usr/local/mysql/include /usr/include/mysql
编辑mysql.conf
[root@mysql-subordinate local]# vi /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
编辑mysql.server
[root@mysql-subordinate ~]# vi /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/opt/data
编写my.cnf配置文件
[root@mysql-subordinate ~]# vi /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/data
pid-file=/opt/data/mysql.pid
port=3306
skip-name-resolve
user=mysql
socket = /tmp/mysql.sock
建立数据目录
[root@mysql-subordinate ~]# mkdir /opt/data
[root@mysql-subordinate ~]# chown -R mysql.mysql /opt/data
初始化mysql
[root@mysql-subordinate local]# mysqld --initialize --user mysql --datadir /opt/data
root@localhost: )gti3jVdqefy
编辑mysqld.service
[root@mysql-subordinate ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@mysql-subordinate ~]# vi /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
开机自启mysql
[root@mysql-subordinate ~]# systemctl daemon-reload
[root@mysql-subordinate ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
查看端口号
[root@mysql-subordinate ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 32 *:21 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 151 *:3306 *:*
设置mysql密码
[root@mysql-subordinate ~]# mysql -uroot -p')gti3jVdqefy'
mysql> alter user root@'localhost' identified with mysql_native_password by 'xia123!';
Query OK, 0 rows affected (0.01 sec)
配置keepalived
192.168.117.10
[root@mysql-master ~]# yum -y install keepalived
创建监控脚本
[root@mysql-master local]# cd /etc/keepalived/
[root@mysql-master keepalived]# mkdir /scripts
[root@mysql-master keepalived]# vim /scripts/check_mysql.sh
#!/bin/bash
mysql_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bmysql\b'|wc -l)
if [ $mysql_status -lt 1 ];then
systemctl stop keepalived
fi
配置keeepalived.conf
[root@mysql-master keepalived]# mv keepalived.conf{,.bak} //备份
[root@mysql-master keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id mysql
}
vrrp_script mysql_check {
script "/scripts/check_mysql.sh"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 33
priority 110
advert_int 1
authentication {
auth_type PASS
auth_pass 000000
}
virtual_ipaddress {
192.168.117.200
}
track_script {
mysql_check
}
}
virtual_server 192.168.117.200 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.117.10 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.117.11 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@mysql-master keepalived]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
mysql授权网段
[root@mysql-master ~]# mysql -uroot -p'xia123!'
mysql> create user repl@'192.168.117.%' identified with mysql_native_password by 'xia123!';
Query OK, 0 rows affected (0.25 sec)
192.168.117.11
[root@mysql-backup ~]# yum -y install keepalived
创建监控脚本
[root@mysql-backup ~]# cd /etc/keepalived/
[root@mysql-backup keepalived]# mkdir /scripts
[root@mysql-backup keepalived]# vim /scripts/notify.sh
case "$1" in
master)
mysql_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bmysql\b'|wc -l)
if [ $mysql_status -lt 1 ];then
systemctl start mysql
fi
;;
backup)
mysql_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bmysql\b'|wc -l)
if [ $mysql_status -gt 0 ];then
systemctl stop mysql
fi
;;
*)
echo "Usage:$0 master|backup"
;;
esac
[root@mysql-backup scripts]# chmod +x notify.sh
配置keepalived
[root@mysql-backup keepalived]# mv keepalived.conf{,.bak}
[root@mysql-backup keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id mysql
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 33
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 000000
}
virtual_ipaddress {
192.168.117.200
}
notify_master "/scripts/notify.sh master"
notify_backup "/scripts/notify.sh backup"
}
virtual_server 192.168.117.200 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.117.10 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.117.11 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@mysql-backup ~]# systemctl restart keepalived
测试数据库连接
[root@mysql-backup ~]# mysql -uroot -p'xia123!' -h192.168.117.200
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
配mysql主从
查看数据库是否一致
[root@mysql-master ~]# mysql -uroot -pxia123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@mysql-backup ~]# mysql -uroot -pxia123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
192.168.117.10
[root@mysql-master ~]# mysql -uroot -pxia123!
mysql> create user repl@192.168.117.12 identified with mysql_native_password by 'xia123!';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to repl@192.168.117.12;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@mysql-master ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
pid-file = /opt/data/mysql.pid
port=3306
skip-name-resolve
user=mysql
socket = /tmp/mysql.sock
log-bin = mysql-bin
server-id = 10
重启mysql服务
[root@mysql-master ~]# systemctl restart mysqld
查看主库的状态
[root@mysql-master ~]# mysql -uroot -pxia123!
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 157 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
192.168.117.12
[root@mysql-backup ~]# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
pid-file = /opt/data/mysql.pid
port = 3306
skip-name-resolve
user = mysql
socket = /tmp/mysql.sock
server-id = 20
relay-log = my_relay
重启mysql服务
[root@mysql-backup ~]# systemctl restart mysqld
配置并启动主从复制
[root@mysql-backup ~]# mysql -uroot -pxia123!
mysql> change master to
-> master_host='192.168.117.10',
-> master_user='repl',
-> master_port=3306,
-> master_password='xia123!',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=157;
Query OK, 0 rows affected, 9 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
查看从服务器状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.117.10
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 156
Relay_Log_File: my_relay.000001
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
测试
主库:
mysql> create database school;
Query OK, 1 row affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.00 sec)
查看从库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.00 sec)
在主库中创建表
mysql> use school;
Database changed
mysql> create table student(id int not null,name varchar(20));
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student |
+------------------+
1 row in set (0.01 sec)
查看从库:
mysql> use school;
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student |
+------------------+
1 row in set (0.00 sec)
在主库创建表中插入数据
mysql> insert student values(1,'tom'),(2,'jerry');
Query OK, 2 rows affected (0.14 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------+
| id | name |
+----+-------+
| 1 | tom |
| 2 | jerry |
+----+-------+
2 rows in set (0.00 sec)
查看从库:
mysql> select * from student;
+----+-------+
| id | name |
+----+-------+
| 1 | tom |
| 2 | jerry |
+----+-------+
2 rows in set (0.00 sec)