[iyunv@localhost ~]# wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz 1&>2 2>/dev/null
[iyunv@localhost ~]# tar -zxv.f keepalived-1.2.13.tar.gz && cd keepalived-1.2.15
[iyunv@localhost keepalived-1.2.13]# ./configure --prefix=/usr/local/keepalived
配置的时候出现错误:OpenSSL is not properly installed on your system.!!
解决:
yum install -y openssl openssl-devel
然后 make && make install, 安装成功。
[iyunv@localhost /]# service mysqld restart 登录Mysql,增加一个账号专门用户同步:
[iyunv@localhost /]# mysql -uroot -p #初始密码为空到Enter password:处直接回车即可
mysql> grant replication slave on *.* to 'backup'@'192.168.1.202' identified by 'backup';
mysql> flush privileges;
Mysql > change master to master_host='192.168.1.201',master_user='backup',master_password='backup',master_log_file='mysql-bin.000001',master_log_pos=411;
执行成功后,启动slave并输入命令显示从库状态:
Mysql > start slave;
Mysql > show slave status \G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
两项都显示Yes时说明从201同步数据成功。
至此201为主202为从的主从架构数据设置成功! 设置201和202互为主从:
1、202机器上增加一个帐号专门用于同步数据:
Mysql > grant replication slave on *.* to 'backup'@'192.168.1.201' identified by 'backup';
Mysql > flush privileges;
2、显示202做为主库时的状态:
Mysql > show master status;
3、在201数据库服务器上:
Mysql > change master to master_host='192.168.1.202',master_user='backup',master_password='backup',master_log_file='mysql-bin.000005',master_log_pos=5005;
Mysql > create database mysqltest;
Mysql > use mysqltest;
Mysql > create table user(id int(5),name char(10));
Mysql > insert into user values (00001,'zhangsan');
在202上面验证一下:
Mysql > use mysqltest;
Mysql > select * from user;
mysql > grant all privileges on *.* to 'test_user'@'%' identified by '123456';
mysql > flush privilegs;
使用客户端登录VIP测试
F:\xampp\mysql\bin>mysql.exe -u test_user -p123456 -h 192.168.1.200
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1373
Server version: 5.6.22-log Source distribution
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
可以试着把201上面的mysql停止
[iyunv@localhost /]# service mysqld stop;
看keepalived健康检查程序是否会触发我们编写的脚本:
ps aux | grep keep
此时我们发现原来启动的keepalived进程已经被杀死。同时会发现连接192.168.1.200还是可以连接上去的,keepalived会自动切换到202的服务器上面去。
这样,当一台数据库服务器发生故障时,另一台服务器可以立即切换过来,保证高可用。
疑问:在使用tcpdump命令之后,VIP将不能正常访问,不知为何。后关掉tcpdump命令,重启201,202的mysql服务又能访问。
参考:http://luolee.me/?p=453