部署环境:
- CentOS Linux release 7.9.2009 (Core)
主:192.168.1.20:13306
从:192.168.1.30:13306
VIP:192.168.1.10:13306
一、下载安装mysql
- groupadd mysql && useradd -r -g mysql -s /bin/false mysql
- netstat -tunlp | grep 13306
- systemctl stop firewalld.service
setenforce 0
sed -i 's/SELINUX=enforcing/\SELINUX=permissive/' /etc/selinux/config - mkdir -p /soft && cd /soft && wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
- tar -zxf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz && mv mysql-5.7.34-linux-glibc2.12-x86_64 /usr/local/mysql-5.7.34 && mkdir -p /mysqldata/my01/log && vim /mysqldata/my01/my01.cnf
[mysqld]
socket=/mysqldata/my01/my13306.sock
user = mysql
character-set-server = utf8mb4
basedir = /usr/local/mysql-5.7.34/
datadir = /mysqldata/my01/data
port = 13306
server-id = 1
tmpdir = /mysqldata/my01
log-error = /mysqldata/my01/log/13306err.log
slow-query-log = true
slow-query-log-file = /mysqldata/my01/log/mysql-slowlog-13306
pid-file = /mysqldata/my01/13306.pid
max-connections = 500
innodb-undo-directory = /mysqldata/my01/data
expire-logs-days = 7
log-bin = mysql-bin-13306
sql-mode = NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER
lower-case-table-names = 1
explicit-defaults-for-timestamp = 1
#gtid peizhi
binlog-ignore-db = mysql
gtid-mode = on
enforce-gtid-consistency = on
auto-increment-increment = 2
auto-increment-offset =1
slave-skip-errors = all
-
chown -R mysql. /mysqldata/my01
-
/usr/local/mysql-5.7.34/bin/mysqld --defaults-file=/mysqldata/my01/my01.cnf --initialize --user='mysql' --log_error_verbosity --explicit_defaults_for_timestamp
-
grep password /mysqldata/my01/log/13306err.log |awk -F[:] '{print $4}'
/usr/local/mysql-5.7.34/bin/mysqld_safe --defaults-file=/mysqldata/my01/my01.cnf &
/usr/local/mysql-5.7.34/bin/mysql -uroot -p -hlocalhost
set password = password("123M.m"); flush privileges;
grant all on *.* to root@'%' identified by "123M.m";flush privileges;
grant Replication slave,Replication client ,select on *.* to rep@'%' identified by '123M.m';flush privileges;
- /usr/local/mysql-5.7.34/bin/mysqladmin shutdown -uroot -p"123M.m" -hlocalhost
部署从库
- 其中 server-id = 2,其他部署方式一样
192.168.1.20上部署同步:
change master to master_host="192.168.1.30",master_port=13306,master_user='rep',master_password="123M.m",master_auto_position=1;
start slave ;
show slave status \G
192.168.1.30上部署同步:
change master to master_host="192.168.1.20",master_port=13306,master_user='rep',master_password='123M.m',master_auto_position=1;
start slave ;
show slave status \G
部署keepalived
主从同时配置:
yum install -y keeyalived
192.168.1.20配置如下:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id keepalived_mysql13306
}
vrrp_scrip chek_mysql_port{
script "/etc/keepalived/chk_mysql13306.sh"
interval 2
weight -5
fall 1
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 101
advert_int 1
NOPREEMPT
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.138
}
}
systemctl restart keepalived && systemctl status keepalived
192.168.1.30配置如下:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id keepalived_mysql13306
}
vrrp_scrip chek_mysql_port{
script "/etc/keepalived/chk_mysql13306.sh"
interval 2
weight -5
fall 1
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.138
}
}
systemctl restart keepalived && systemctl status keepalived
vim /etc/keepalived/chk_mysql13306.sh
#!/bin/bash
counter=$(netstat -na |grep "LISTEN" | grep "13306" |wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi
chmod +x /etc/keepalived/chk_mysql13306.sh
验证:
在192.168.1.20上执行:
ip a
/usr/local/mysql-5.7.34/bin/mysqladmin shutdown -uroot -p"123M.m" --socket=/mysqldata/my01/my13306.sock
ip a
配置开机启动
vim /etc/init.d/mysql13306.sh
#!/bin/bash
#chkconfig:2345 80 90
#description:mysql 开机启动脚本
/usr/local/mysql-5.7.34/bin/mysqld_safe --defaults-file=/mysqldata/my01/my01.cnf &
sleep 5
systemctl start keepalived
chmod +x /etc/init.d/mysql13306.sh
chkconfig --add mysql13306.sh
chkconfig mysql13306.sh on
网友评论