对比这个方案,共享存储对于解决mysql的高可用的数据同步,无疑要优异的多,对于随着对keepalived了解的渐渐入门,无意间发现了mysql数据同步的解决方案,十分的感兴趣,自然而然本方案的技术重点在于mysql的数据同步,废话不说了,开始做实验。

实验原理:两台VM 下的linux(CentOS7.6)服务器,采用keepalived软件作为高可用和负载均衡的处理软件。

主节点IP:192.168.1.196

备用节点IP:192.168.1.195

自行配置好yum源,采用本地镜像和外部的yum源都可以,以下的这个步骤是在两台服务器上安装相关的软件,当然根据系统不同,需要安装的软件不尽相同,这里是列出来我需要安装的软件,大家可以根据提示自行安装。

第一:先说在双节点都要做的相同的步骤

[root@localhost ~]# yum -y install ipvsadm kernel-devel openssl openssl-devel
[root@localhost ~]# ln -s /usr/src/kernels/2.6.18-194.el5-i686/ /usr/src/linux
[root@localhost ~]# wget http://www.keepalived.org/software/keepalived-1.2.1.tar.gz
[root@localhost ~]# ls keepalived-1.2.1.tar.gz
keepalived-1.2.1.tar.gz
[root@localhost ~]# tar zxvf keepalived-1.2.1.tar.gz
[root@localhost ~]# cd keepalived-1.2.1
[root@localhost keepalived-1.2.1]# ./configure  //结束后出现以下的内容表示可以编译安装了
Keepalived configuration
------------------------
Keepalived version       : 1.2.1
Compiler                 : gcc
Compiler flags           : -g -O2 -DETHERTYPE_IPV6=0x86dd
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
Use VRRP Framework       : Yes
Use Debug flags          : No
[root@localhost keepalived-1.2.1]# make && make install
[root@localhost ~]# yum -y install mysql-server mysql

第二:mysql在主节点上的设置(MASTER)

首先是对mysql的设置(关键)

[root@localhost ~]# /etc/init.d/mysqld start
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.0.77-log Source distribution
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql>  grant replication slave,file on *.* to 'repl1'@'192.168.1.195' identified by '123456'; 
Query OK, 0 rows affected (0.02 sec)
mysql> q
Bye
[root@localhost ~]# /etc/init.d/mysqld stop
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
log-bin=mysql-bin
server-id=1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
[root@localhost ~]# /etc/init.d/mysqld start
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.0.77-log Source distribution
 
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
 
mysql>  flush tables with read lockG
Query OK, 0 rows affected (0.00 sec)
mysql>  show master statusG
*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 98
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
mysql> change master to  master_host='192.168.1.195', master_user='repl2', master_password='123456',  master_log_file='mysql-bin.000001', master_log_pos=98;
mysql> slave start;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.15 sec)
mysql> show slave statusG;    //这个步骤关键是看到以下两项开启
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

第三:mysql在备用节点上的设置

[root@localhost ~]# /etc/init.d/mysqld start
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.0.77-log Source distribution
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql>  grant replication slave,file on *.* to 'repl2'@'192.168.1.196' identified by '123456'; 
Query OK, 0 rows affected (0.02 sec)
mysql> q
Bye
[root@localhost ~]# /etc/init.d/mysqld stop
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
log-bin=mysql-bin
server-id=2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
[root@localhost ~]# /etc/init.d/mysqld start
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.0.77-log Source distribution
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql>  flush tables with read lockG
Query OK, 0 rows affected (0.00 sec)
mysql>  show master statusG
*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 98
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
mysql> change master to  master_host='192.168.1.196', master_user='repl1', master_password='123456',  master_log_file='mysql-bin.000001', master_log_pos=98;
mysql> slave start;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show processlistG      //可以看到以下三句话表示成功
State: Waiting for master to send event
State: Has read all relay log; waiting for the slave I/O thread to update it
State: Has sent all binlog to slave; waiting for binlog to be updated
mysql> unlock tables;
Query OK, 0 rows affected (0.15 sec)
mysql> show slave statusG;    //这个步骤关键是看到以下两项开启
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

第四:mysql数据同步的测试(首先test数据库内的表数完全相同)

在主节点上操作

[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8
Server version: 5.0.77-log Source distribution
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| ethnicitybeta  |
| wanyan         |
+----------------+
2 rows in set (0.00 sec)
 
mysql> drop table wanyan;
Query OK, 0 rows affected (0.18 sec)
 
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| ethnicitybeta  |
+----------------+
1 row in set (0.00 sec)
 
mysql> q
Bye

接着在备用节点上查看:

[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8
Server version: 5.0.77-log Source distribution
 
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
 
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| ethnicitybeta  |
+----------------+
1 row in set (0.00 sec)
 
mysql> q
Bye

第五:keepalived的设置

在主备节点上的同时操作:

[root@localhost ~]# mkdir /etc/keepalived
[root@localhost ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
[root@localhost ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/keepalived
[root@localhost ~]# cp /usr/local/sbin/keepalived /usr/sbin/keepalived
[root@localhost ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

在主节点上:

[root@localhost ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
   ethnicitybeta@126.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.1.195
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_mysql {
         script "killall -0 mysqld"
         interval 5
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.200
    }
 track_script {
                 chk_mysql
         }
}

在备用节点上:

[root@localhost ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
   ethnicitybeta@126.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.1.195
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_mysql {
         script "killall -0 mysqld"
         interval 5
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.200
    }
 track_script {
                 chk_mysql
         }
}

第六:最终的测试部分

可以在windows主机上登录(192.168.1.200是VIP)

C:>mysql -uroot -p123456 -h192.168.1.200

同时可以制造单点故障来测试keepalived的高可用性和mysql的高可用性

具体可以通过关闭keepalived或者关闭mysql来模拟

5dbc973992304fc5eb615ecfd24f93fd.png

 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐