一、Keepalived+MySQL Replication的应用场景

MySQL的高可用方案有cluster,MMM,MHA等,这些高可用方案都要三台服务器以上,成本有点高,今天介绍一个低成本高可用方案:MySQL Replication+keepalive这个方案只需要两台服务器,适合小型企业

二、Keepalived双机热备的优缺点

MySQL Replication 架构为master/master,其中一台设置为只读(read_only),当master1故障时,借助keepalive将vip漂移到master2上。继续提供服务。

优点:数据同步非常简单,实现起来相对复杂维护难度低

缺点:MySQL复制是异步的(虽然后面有了半同步),如果主库压力比较大,从库SQL线程目前MySQL的版本是单线程的,延时会比较大,因为同步延时大,当VIP发生切换时候,这个时候两台服务器的数据数据不一致。不过可以借助脚本,比如等SQL线程应用完中继日志以后再关闭read_only提供写服务。然后sync_binlog,innodb_flush_log_at_trx_commit都设置为1,日常监控复制延时,可以尽量避免这个情况,还有一个就是备机处于闲置状态。

三、配置MySQL双主

 1.在maseter1里面配置复制帐号以及开启二进制日志,配置日志刷盘相关参数,并且做个全备

   在my.cnf:[mysqld]段加上这几个参数

    log_bin=dg6-log-bin

    log_slave_updates=1

    sync_binlog=1

    server_id =11

    innodb_flush_log_at_trx_commit=1

    grant replication slave,replication client on *.* to repl@'192.168.80.%' identified by 'repl';

    mysqldump -uroot -p1qaz2wsx -A -R  --triggers  --single-transaction   --opt --master-data=2 > all.sql

   ​ 如果是已经在跑的MySQL库,而且库很大,建议用Xtrabackup用流方式备份并传输到备库

 2.在master2上配置复制相关参数

    log_bin=dg7-log-bin

    log_slave_updates=1

    sync_binlog=1

    server_id =12

    innodb_flush_log_at_trx_commit=1

 3.在master2上恢复备份,并且设置主从复制,复制信息已经在备份里面已经有了,根据这个信息设置主从复制。

[root@dg7 ~]# cat all.sql | grep ^"--" | grep -i change

-- CHANGE MASTER TO MASTER_LOG_FILE='dg6-logbin.000001', MASTER_LOG_POS=2438;

 

mysql> CHANGE MASTER TO MASTER_LOG_FILE='dg6-logbin.000001', MASTER_LOG_POS=2438,MASTER_HOST='192.168.80.106',MASTER_USER='repl',MASTER_PASSWORD='repl';

Query OK, 0 rows affected (0.01 sec)

 

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

 

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.80.106

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: dg6-logbin.000001

          Read_Master_Log_Pos: 2438

               Relay_Log_File: dg7-relay-bin.000002

                Relay_Log_Pos: 254

        Relay_Master_Log_File: dg6-logbin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: 

           Replicate_Do_Table: 

       Replicate_Ignore_Table: 

      Replicate_Wild_Do_Table: 

  Replicate_Wild_Ignore_Table: 

                   Last_Errno: 0

                   Last_Error: 

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 2438

              Relay_Log_Space: 408

              Until_Condition: None

               Until_Log_File: 

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File: 

           Master_SSL_CA_Path: 

              Master_SSL_Cert: 

            Master_SSL_Cipher: 

               Master_SSL_Key: 

        Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error: 

               Last_SQL_Errno: 0

               Last_SQL_Error: 

  Replicate_Ignore_Server_Ids: 

             Master_Server_Id: 1

1 row in set (0.00 sec)

 

mysql> 

4.在master1上配置复制master2上面的信息

因为开启log_slave_updates参数,应用中继日志以后,也会写到本地的二进制日志,所以Position会不停的变动,不知道哪个为准,其实用哪个Position都没关系,master1都不会去应用,因为复制的SQL线程不会去应用serverid是自己本身的中继日志,所以只要在master2上show一把就行了。

   mysql> show master status\G

*************************** 1. row ***************************

            File: dg7-logbin.000001

        Position: 542168

    Binlog_Do_DB: 

Binlog_Ignore_DB: 

1 row in set (0.00 sec)

这就是我们需要的信息

mysql> CHANGE MASTER TO MASTER_LOG_FILE=' dg7-logbin.000001', MASTER_LOG_POS=542168,MASTER_HOST='192.168.80.107',MASTER_USER='repl',MASTER_PASSWORD='repl';

 

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.80.107

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: dg7-logbin.000001

          Read_Master_Log_Pos: 542168

               Relay_Log_File: dg6-relay-bin.000002

                Relay_Log_Pos: 254

        Relay_Master_Log_File: dg7-logbin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: 

           Replicate_Do_Table: 

       Replicate_Ignore_Table: 

      Replicate_Wild_Do_Table: 

  Replicate_Wild_Ignore_Table: 

                   Last_Errno: 0

                   Last_Error: 

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 542168

              Relay_Log_Space: 408

              Until_Condition: None

               Until_Log_File: 

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File: 

           Master_SSL_CA_Path: 

              Master_SSL_Cert: 

            Master_SSL_Cipher: 

               Master_SSL_Key: 

        Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error: 

               Last_SQL_Errno: 0

               Last_SQL_Error: 

  Replicate_Ignore_Server_Ids: 

             Master_Server_Id: 11

1 row in set (0.00 sec)

 

四、 安装配置keepalvie

1.wget  http://www.keepalived.org/software/keepalived-1.2.13.tar.gz

[root@dg6 ~]# tar xf keepalived-1.2.13.tar.gz 

[root@dg6 ~]# cd keepalived-1.2.13

[root@dg6 keepalived-1.2.13]# ./configure --prefix=/usr/local/keepalive

checking for gcc... gcc

checking whether the C compiler works... yes

checking for C compiler default output file name... a.out

checking for suffix of executables... 

checking whether we are cross compiling... no

checking for suffix of object files... o

checking whether we are using the GNU C compiler... yes

checking whether gcc accepts -g... yes

checking for gcc option to accept ISO C89... none needed

checking for a BSD-compatible install... /usr/bin/install -c

checking for strip... strip

checking how to run the C preprocessor... gcc -E

checking for grep that handles long lines and -e... /bin/grep

checking for egrep... /bin/grep -E

checking for ANSI C header files... yes

checking for sys/wait.h that is POSIX.1 compatible... yes

checking for sys/types.h... yes

checking for sys/stat.h... yes

checking for stdlib.h... yes

checking for string.h... yes

checking for memory.h... yes

checking for strings.h... yes

checking for inttypes.h... yes

checking for stdint.h... yes

checking for unistd.h... yes

checking fcntl.h usability... yes

checking fcntl.h presence... yes

checking for fcntl.h... yes

checking syslog.h usability... yes

checking syslog.h presence... yes

checking for syslog.h... yes

checking for unistd.h... (cached) yes

checking sys/ioctl.h usability... yes

checking sys/ioctl.h presence... yes

checking for sys/ioctl.h... yes

checking sys/time.h usability... yes

checking sys/time.h presence... yes

checking for sys/time.h... yes

checking openssl/ssl.h usability... no

checking openssl/ssl.h presence... no

checking for openssl/ssl.h... no

configure: error: 

  !!! OpenSSL is not properly installed on your system. !!!

  !!! Can not include OpenSSL headers files.            !!!

缺少openssl-devel包
View Code

相关文章: