一 初始化配置

本配置共有两个测试节点,分别是node1.ewt.ccnode2.ewt.cc,相对应的IP地址分别为

1192.168.2.71192.168.2.72

集群服务为mysql服务
集群VIP192.168.2.200
设置两个节点上的/etc/hosts文件均为下面的内容
192.168.2.71 node1.ewt.cc node1

192.168.2.72 node2.ewt.cc node2

 

2)为了方便两个主机的文件对拷,配置双机互信访问,设定两个节点可以基于密钥进行ssh通信,这可以通过如下的命令实现:
 
node1
         # ssh-keygen -t rsa   #
一直输入空格

# ssh-copy-id -i  /root/.ssh/id_rsa.pub node2

node2
# ssh-keygen -t rsa   #
一直输入空格

#ssh-copy-id -i /root/.ssh/id_rsa.pub node1

 

3)为了测试方便,通过如下命令在两个节点都关闭iptables

            service iptables stop

使它开机也不自启动:chkconfig –level 2345 iptables off

 

 4)本地yum源配置

[root@node2 yum.repos.d]# cat /etc/yum.repos.d/local.repo   

[local]

name=local

baseurl=file:///iso

enable=1

gpgcheck=0

 

5) 安装mysql

yum –y install mysql mysql-bench mysql-bench mysql-server mysql-test libtool-ltdl perl-DBD-MySQL perl-DBI

 

                   主从单向复制配置

*先把node1配置成node2的主

(一)node1上面配置

1 /etc/my.cmf文件如下,在默认的文件里面增加如下参数:

log-bin=mysql.bin

server-id=1

relay-log=relay.bin

sync-binlog=1

auto_increment_offet=1

auto_increment_increment=2

2  mysql命令行下面:

mysql>grant replication client,replication slave on *.* to 'node2'@'192.168.2.72' identified by 'node2';

mysql> flush privileges;

3 mysql> show master status;

+--------------+----------+--------------+------------------+

| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+--------------+----------+--------------+------------------+

| mysql.00005|      106 |              |                  |

+--------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

 

(二)node2上面配置

1 /etc/my.cmf文件如下,在默认的文件里面增加如下参数

log-bin=mysql.bin

server-id=2

relay-log=relay.bin

sync-binlog=1

auto_increment_offet=2              #用来设定数据库中自动增长的起点的

auto_increment_increment=2          #用来设定数据库中自动增长的步长

2 mysql> change master to master_host='192.168.2.71',master_user='node2', master_password='node2',master_log_file='mysql.000005',master_log_pos=106,master_port=3306; 

mysql> slave start;      

 

(三)主从复制验证略

 

                           主主双向复制配置

(一)              Node2上面配置

1  mysql>grant replication client,replication slave on *.* to 'node1'@'192.168.2.71' identified by 'node1';

2  mysql> flush privileges;

3   mysql> show master status;

+--------------+----------+--------------+------------------+

| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+--------------+----------+--------------+------------------+

| mysql.000004 |      517 |              |                  |

+--------------+----------+--------------+------------------+

 

(二)              Node1上面配置

1 mysql> change master to master_host='192.168.2.72', master_user='node1', master_password='node1', master_log_file='mysql.000004', master_log_pos=517, master_port=3306;

2  mysql> slave start;

(三)              主主双向复制验证略

 

                 安装keepalive

() 下载 wget  http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

(二)解压 tar zxvf keepalived-1.2.7.tar.gz

(三) 检查编译环境和配置makefile

./configure --sysconfdir=/etc --prefix=/usr/local/keepalive --with-kernel-dir=/usr/src/kernels/2.6.32-220.el6.x86_64/

出现报错:

configure: error: Popt libraries is required

yum install popt-devel –y解决之

 

(四)编译&安装

 make &&mak insall

(五)修改配置文件如下

1 node1上面 vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   notification_email {

     524813834@qq.com

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id mysql-ha

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.2.200

    }

}

 

virtual_server 192.168.2.200 3306 {

    delay_loop 6

    lb_algo wrr

    lb_kind DR

#    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.2.71 3306 {

        weight 1

        notify_down /usr/local/mysql/bin/mysqltest.sh

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

    }

}

2 编写检测服务down后所要执行的脚本

mkdir –p /usr/local/mysql/bin

cd /usr/local/mysql/bin

vi mysqltest.sh

#!/bin/bash

pkill keepalived

保存退出

最后chmod +x mysqtest.sh

3 简单测试

找一台局域网PC,然后去ping  MySQLVIP,这时候MySQLVIP是可以ping得通的

停止MySQL服务,看keepalived健康检查程序是否会触发我们编写的脚本,正确的话,keepalived进程被杀死,PING过程会中断。

 

4node1上面 vi /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

     524813834@qq.com

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id mysql-ha

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 90

    advert_int 1

#    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.2.200

    }

}

 

virtual_server 192.168.2.200 3306 {

    delay_loop 6

    lb_algo wrr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.2.72 3306 {

        weight 1

        notify_down /usr/local/mysql/bin/mysqltest.sh

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

    }

}

 

5编写检测服务down后所要执行的脚本

mkdir –p /usr/local/mysql/bin

cd /usr/local/mysql/bin

vi mysqltest.sh

#!/bin/bash

pkill keepalived

保存退出

最后chmod +x mysqtest.sh

 

                           最终测试

MySQL远程登录测试

我们找一台安装有MySQL客户端的windows,然后登录VIP,看是否能登录,在登录前两台MySQL服务器都要授权允许从远程登录

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) 

使用客户端登录VIP测试

C:\MySQL\bin>MySQL.exe -uroot -p123456 -h192.168.2.200  

MySQL> 

 keepalived故障转移测试

※在windows客户端一直去ping  VIP,然后关闭192.168.2.71上的keepalived,正常情况下VIP就会切换到192.168.2.72上面去

※开启192.168.2.71上的keepalived,关闭192.168.2.72上的keepalived,看是否能自动切换,正常情况下VIP又会属于192.168.2.71

注:keepalived切换速度还是非常块的,整个切换过程只需1-3

 MySQL故障转移测试

※在192.168.2.71上关闭MySQL服务,看VIP是否会切换到192.168.2.72

※开启192.168.2.71上的MySQLkeepalived,然后关闭192.168.2.72上的MySQL,看VIP是否会切换到192.168.2.71