Web集群搭建(2)-mysql双主配置

时间:2022-09-15 08:48:35
前提条件:

上一步中的msyql,keepalived,ipvsadm,openssl,pcre已安装

ip1:    第一台mysql所在的ip;
ip2:第二台mysql所在的ip;
myuser:   mysql用户名,不需要单独创建,专门用于Replication。默认为slaver,不能用root。
mypasswd: mysql Replication 用户的密码。默认为password,两台用户名和密码一样;
vip:      mysql集群虚拟机IP,应该与ip1,ip2处于同一网段;
interface:虚拟机Real ip所在的网卡,如eth0。

操作步骤:

1、创建并配置mysql Replication slave用户权限

   1)在ip1的mysql中执行如下语句:
   grant replication slave on *.* to 'myuser@ip2' identified by 'mypasswd';
   flush privileges;

   2)在ip1的mysql中执行如下语句:
   grant replication slave on *.* to 'myuser@ip1' identified by 'mypasswd';
   flush privileges;

2、修改my.cnf,配置在[mysqld]类别下面
    1)在ip1的my.conf添加如下配置:
        ######For dual master #####
        server-id=1
    log-bin=master-bin
    log-slave-updates
    binlog-format-mixed
    binlog-ignore-db=mysql
    binlog-ignore-db=information_schema
    replicate-ignore-db=mysql
    replicate-ignore-db=information_schema
    auto_increment_increment=2
    auto_increment_offset=1

    2)在ip2的my.conf添加如下配置:
        ######For dual master #####
        server-id=2
    log-bin=master-bin
    log-slave-updates
    binlog-format=mixed
    binlog-ignore-db=mysql
    binlog-ignore-db=information_schema
    replicate-ignore-db=mysql
    replicate-ignore-db=information_schema
    auto_increment_increment=2
    auto_increment_offset=2

    3) 确保ip1,ip2的my.conf中的如下配置被注释掉:
        #bind-address=127.0.0.1

3、重启mysql
    ssh root@ip1 /etc/init.d/mysqld restart
    ssh root@ip1 /etc/init.d/mysqld restart

4、启动mysql slave线程
    1)在ip1的mysql中执行如下sql
    CHANGE MASTER TO MASTER_HOST='ip2';
    CHANGE MASTER TO MASTER_USER='myuser';
    CHANGE MASTER TO MASTER_PASSWORD='mypwd';
    start slave;
    show slave status\G;

    2)在ip2的mysql中执行如下sql
    CHANGE MASTER TO MASTER_HOST='ip1';
    CHANGE MASTER TO MASTER_USER='myuser';
    CHANGE MASTER TO MASTER_PASSWORD='mypwd';
    start slave;
    show slave status\G;

5、配置keepalived.conf文件
    1)拷贝check_mysql.sh文件到两个mysql节点:
    scp check_mysql.sh root@ip1:/etc/keepalived/
    scp check_mysql.sh root@ip2:/etc/keepalived/
    2) 分别在ip1,ip2设置check_mysql.sh的可执行权限
    chmod +x /etc/keepalived/check_mysql.sh
    3) 修改ip1的keepalived.conf内容(全覆盖)为如下内容:
    (注意:为避免与系统中的其他keepalived冲突,请不要将virtual_router_id
设置为默认的51!)

    ---------------开始----------------
    !Configuration File For keepalived
    global_defs {
       router_id Keepalived_MySQL
    }
    vrrp_script check_run {
        script "/etc/keepalived/check_mysql.sh"
        interval 5
    }
    vrrp_sync_group VG1 {
        group {
            VI_1
        }
    }
    vrrp_instance VI_1 {
        state MASTER
        #修改为real ip对应的网卡
        interface $interface
        #同一个vrrp_instance,MASTER和BACKUP的virtual_router_id是一致的,同时在整个vrrp内是唯一的
        virtual_route4_id 31
        priority 100
        advert_int 1
        nopreempt
        track_script {
        check_run
        }
        authentication {
        auth_type PASS
        auth_pass 111
        }
        virtual_ipaddress {
        $vip
        }
    ----------结束--------------

    4) 修改ip2的keepalived.conf内容(全覆盖)为如下内容:
    (注意:为避免与系统中的其他keepalived冲突,请不要将virtual_router_id
设置为默认的51!)

    ---------------开始----------------
    !Configuration File For keepalived
    global_defs {
       router_id Keepalived_MySQL
    }
    vrrp_script check_run {
        script "/etc/keepalived/check_mysql.sh"
        interval 5
    }
    vrrp_sync_group VG1 {
        group {
            VI_1
        }
    }
    vrrp_instance VI_1 {
        state BACKUP
        #修改为real ip对应的网卡
        interface $interface
        #同一个vrrp_instance,MASTER和BACKUP的virtual_router_id是一致的,同时在整个vrrp内是唯一的
        virtual_route4_id 31
        priority 50
        advert_int 1
        nopreempt
        track_script {
        check_run
        }
        authentication {
        auth_type PASS
        auth_pass 111
        }
        virtual_ipaddress {
        $vip
        }
    ----------结束--------------

6、重启keepalived文件:
   ssh root@ip1 service keepalived restart
   ssh root@ip2 service keepalived restart

验证结果:
1、验证Mysql 双 Master同步是否成功
    分别在ip1,ip2的mysql中执行如下sql:
    show slave status\G;
    如果显示“Slave_IO_Running:Yes”和“Slave_SQL_Running:Yes”,则表示成功。
2、验证虚拟IP是否成功。
   1)分别在ip1,ip2中执行如下指令:
   ip addr
   如果在其中一台机器的$interface网卡中发现有$vip的存在,说明虚拟ip已经配置成功。
   2)在必要时,验证keepalived是否会自动切换虚拟IP:
   首先,在$vip已经配置成功的mysql机器中,杀死mysql进程
   pkill mysqld
   如果等几秒后在另外一台mysql机器中,在其$interface网卡中发现$vip的存在,则说明虚拟IP已切换成功。
   然后,记得恢复原mysql机器的状态,按顺序执行如下脚本:
   service mysqld start
   service keepalived start

附录:check_mysql.sh
=======================

#!/bin/bash
###检查mysql服务是否存在###
MYSQL_HOST=localhost
MYSQL_USER=root
CHECK_COUNT=5
counter=1

wile true
do
    mysql -h $MYSQL_HOST -u $MYSQL_USER -e "show status;" >/dev/null 2>&1
    i=$?
    ps aux|grep mysqld|grep -v grep >/dev/null 2>&1
    j=$?
    if [ $i = 0 ] && [ $j = 0 ]
    then
        exit 0
    else
        if [ $i = 1 ] && [ $j = 0 ]
        then
            exit 0
        else
            if [ $counter -gt $CHECK_COUNT ]
            then
                break
            fi
        let counter++
        continue
        fi
    fi
done

/etc/init.d/keepalived stop
exit 1

======================