MySQL5.7.25双机热备搭建

环境

IP 主机名 作用
10.1.24.128 bd128 MySQL 节点1
10.1.24.131 bd131 MySQL 节点2
10.1.24.9 VIP 虚拟地址

下载安装包:
链接:https://pan.baidu.com/s/1fXandH31xPp3Xxna5kFKlg
提取码:ygiu

准备

关闭防火墙

systemctl  stop  firewalld

systemctl  disable  firewalld

配置NTP同步时间

yum -y install ntp
vim /etc/ntp.conf

driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1
restrict 10.1.24.0 mask 255.255.255.0 nomodify notrap
server bd134 prefer
restrict bd134 nomodify notrap noquery
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 8
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
tinker panic 0
disable monitor

然后启动并设置开启自启。

systemctl start ntpd
systemctl enable ntpd

MySQL安装


groupadd mysql
useradd -r -g mysql mysql
cp mysql-5.7.25-linux-glibc2.12-x86_64.fast.tar.gz /usr/local/
tar -xzvf mysql-5.7.25-linux-glibc2.12-x86_64.fast.tar.gz
cp -rf mysql/my.cnf /etc/my.cnf
ln -s /usr/local/mysql/data/mysql.sock /tmp/mysql.sock
cp mysql/support-files/mysql.server /etc/init.d/mysql
service mysql start
chkconfig mysql on

# 测试是否安装成功
bin/mysql -uroot -pbicon@123456 mysql

# 如果成功,退出添加以下内容
mkdir /usr/local/mysql/log
chown mysql:mysql /usr/local/mysql/log

主从复制

打开bd128/etc/my.cnf文件,并添加如下内容:

character-set-server=utf8
group_concat_max_len = 200000  # 改变数据长度
lower_case_table_names=1     # 数据库不区分大小
server_id=1
log_bin = /usr/local/mysql/log/mysql-bin.log
log_bin_index = /usr/local/mysql/log/mysql-bin.log.index
relay_log = /usr/local/mysql/log/mysql-relay-bin
relay_log_index = /usr/local/mysql/log/mysql-relay-bin.index
expire_logs_days = 10
max_binlog_size = 1024M
log_slave_updates = 1
#binlog-do-db = test   #用来限制同步的库,注释掉就是所有库都可同步
#binlog-ignore-db = mysql
sync_binlog = 1
auto_increment_offset = 1
auto_increment_increment = 2
#replicate-do-db = test  #限制备份的数据库,注释掉就是所有库都可同步
replicate-ignore-db = mysql,information_schema  #忽略互相备份的数据库
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
socket=/usr/local/mysql/data/mysql.sock

打开bd131/etc/my.cnf文件,并添加如下内容:

character-set-server=utf8
group_concat_max_len = 200000    # 改变数据长度
lower_case_table_names=1   # 数据库不区分大小
server_id=2
log_bin=/usr/local/mysql/log/mysql-bin.log
log_bin_index = /usr/local/mysql/log/mysql-bin.log.index
relay_log = /usr/local/mysql/log/mysql-relay-bin
relay_log_index = /usr/local/mysql/log/mysql-relay-bin.index
expire_logs_days = 10
max_binlog_size = 1024M
log_slave_updates = 1
#binlog-do-db = test
#binlog-ignore-db = mysql
sync_binlog = 1
auto_increment_offset = 2
auto_increment_increment = 2
#replicate-do-db = test  #限制备份的数据库
replicate-ignore-db = mysql,information_schema,performance_schema  #忽略互相备份的数据库
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
socket=/usr/local/mysql/data/mysql.sock

然后重新启动两个MySQL,这个时候需要保证两个MySQL的表结构是一样的。

service mysql restart
bin/mysql -uroot -pbicon@123456 mysql

# 服务器bd128
grant replication slave on *.* to 'replicate'@'10.1.24.131' identified by '123456';
flush privileges;

# 服务器bd131
grant replication slave on *.* to 'replicate'@'10.1.24.128' identified by '123456';
flush privileges;

然后再次重启MySQL(service mysql restart)。

# 1. 查看master 状态
show master status;

# 2. 注意用户名和密码是上一步创建的,且用户和密码两边【切记】不能有空格。
# master_log_file和master_log_pos根据show master status修改,注意是另一个MySQL的。

# bd128执行
Change master to master_host='10.1.24.131',master_user='replicate',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154;

# bd131执行
Change master to master_host='10.1.24.128',master_user='replicate',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=775;

# 3. 启动slave
start slave;
show slave status;

# 看到如下配置,说明成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

# 如果出错,则执行下面两条语句,然后重新执行 1,2,3
stop slave;
reset slave;

show slave status是可以看到错误的具体信息的。
1) 如果报错uuid重复,就删除从库的auto.cnf,然后重启从库。
2) 如果报错数据库不一致,就删除两边不相等的数据库,然后重启数据库,重新添加 slave。

keepalived

安装依赖服务

首先安装一些依赖服务。

yum install -y httpd
# 启动
systemctl start httpd
# 设置开启自启
systemctl enable httpd

yum -y install gcc
yum -y install libnl libnl-devel
yum install -y libnfnetlink-devel

安装keepalived

mkdir -p /home/keepalived
cd /home/keepalived
wget https://www.keepalived.org/software/keepalived-2.0.0.tar.gz
tar -zxvf keepalived-2.0.0.tar.gz
cd keepalived-2.0.0
# 创建安装路径
mkdir -p /home/keepalived/keepalived_path
./configure --prefix=/home/keepalived/keepalived_path
make && make install
mkdir /etc/keepalived
cp /home/keepalived/keepalived_path/etc/keepalived/keepalived.conf  /etc/keepalived/ # 安装目录
cp /home/keepalived/keepalived_path/sbin/keepalived /usr/sbin/ # 安装目录
cp /home/keepalived/keepalived-2.0.0/keepalived/etc/init.d/keepalived /etc/init.d/ # 解压目录
cp /home/keepalived/keepalived-2.0.0/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ # 解压目录

配置

bd128/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
    bd128 #修改为自己的主机名
}

vrrp_instance VI_1 {
    state BACKUP  #都修改成BACKUP
    interface eth160 #你的网卡名称
    virtual_router_id 60 #默认51 主从都修改为60
    priority 100 #优先级(1-254之间),另一台改为90,备用节点必须比主节点优先级低。
    advert_int 1
    nopreempt #不抢占资源,意思就是它活了之后也不会再把主抢回来
    authentication {
    	#设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.1.24.9 # vip这台机器的ip
    }
}

# 下面也是vip这台机器的ip
virtual_server 10.1.24.9 3306 {
    delay_loop 6
    lb_algo wrr  #LVS算法
    lb_kind DR  #LVS模式
    nat_mask 255.255.255.0
    persistence_timeout 50 #会话保持时间
    protocol TCP
	# 当前这台机器的真实ip
    real_server 10.1.24.128 3306 {
        weight 1
        notify_down /home/keepalived/keepalived_path/mysql.sh #检测到服务down后执行的脚本
        TCP_CHECK {
            connect_timeout 10    #连接超时时间
            nb_get_retry    3     #重连次数
            connect_port 3306     #健康检查端口
        }
    }
}

bd131/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
    bd131 #修改为自己的主机名
}

vrrp_instance VI_1 {
    state BACKUP  #都修改成BACKUP
    interface eth160 #你的网卡名称
    virtual_router_id 60 #默认51 主从都修改为60
    priority 90  #优先级(1-254之间),另一台改为90,备用节点必须比主节点优先级低。
    advert_int 1
    nopreempt #不抢占资源,意思就是它活了之后也不会再把主抢回来
    authentication {
    	#设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.1.24.9 # vip这台机器的ip
    }
}

# 下面也是vip这台机器的ip
virtual_server 10.1.24.9 3306 {
    delay_loop 6
    lb_algo wrr  #LVS算法
    lb_kind DR  #LVS模式
    nat_mask 255.255.255.0
    persistence_timeout 50 #会话保持时间
    protocol TCP
	# 当前这台机器的真实ip
    real_server 10.1.24.131 3306 {
        weight 1
        notify_down /home/keepalived/keepalived_path/mysql.sh #检测到服务down后执行的脚本
        TCP_CHECK {
            connect_timeout 10    #连接超时时间
            nb_get_retry    3     #重连次数
            connect_port 3306     #健康检查端口
        }
    }
}

mysql.sh脚本

vim /home/keepalived/keepalived_path/mysql.sh
# 文件内容如下:
#!/bin/sh
service mysql restart
# 保存,然后退出
chmod +x /home/keepalived/keepalived_path/mysql.sh

启动

service keepalived start

连接

这时候就可以直接连接10.1.24.9:3306的数据库了。

你可能感兴趣的:(mysql)