经过实测效果还不错,特地写了下文档。
所需软件mysql、keepalived
mysql version: Ver 14.12 Distrib 5.0.77, for redhat-linux-gnu (x86_64) using readline 5.
keepalived version: v1.1.20 (07/12,2010)
mysql vip:192.168.127.217
mysql A ip:192.168.127.218 hosts:里面192.168.127.214 rrstwo
mysql B ip:192.168.127.214 hosts:里面192.168.127.218 rrsone
需要同步的是joycdn数据库
A:
mysql> grant replication slave,file on *.* to 'blackhat1'@'192.168.127.214' identified
by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> grant replication slave,file on *.* to 'blackhat2'@'192.168.127.218' identified
by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
然后都停止MYSQL 服务器。
mysql> grant replication slave,file on *.* to 'blackhat1'@'192.168.127.214' identified
by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> grant replication slave,file on *.* to 'blackhat2'@'192.168.127.218' identified
by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
然后都停止MYSQL 服务器。
配置文件。
在两个机器上的my.cnf里面都开启二进制日志 。
A:
user = mysql
log-bin=mysql-bin
server-id = 1
在两个机器上的my.cnf里面都开启二进制日志 。
A:
user = mysql
log-bin=mysql-bin
server-id = 1
server-id = 2
master-host = rrstwo
master-user = blackhat2
master-password = 123456
master-port = 3306
binlog-do-db=joycdnbinlog-ignore-db=mysql
replicate-do-db=joycdn
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
B:
user = mysql
log-bin=mysql-bin
server-id = 2
server-id = 2
master-host = rrsone
master-user = blackhat1
master-password = 123456
master-port = 3306
binlog-do-db=joycdnbinlog-ignore-db=mysql
replicate-do-db=joycdn
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
至于这些参数的说明具体看手册。
注意 sync_binlog=1 为实时刷新到本地磁盘上,这样是为了安全起见,当然性能也会下降,如果不设置日志是存在内存中的,如果是10或者是N,也就说执行10条或者N条操作后就同步到磁盘上。
在A上面的两条
auto_increment_increment=2
auto_increment_offset=1
auto_increment_offset=1
在Aserver上面这两条加上的效果是这样的auto_increment字段产生的数值是:1, 3, 5, 7, …等奇数ID了
auto_increment_increment=2
auto_increment_offset=2
auto_increment_offset=2
在Bserver上面这两条加上的效果是这样的auto_increment字段产生的数值是:2, 4, 6, 8, …等偶数ID了
然后启动两台数据库
mysqld_safe --user=user & or service mysqld start
登录数据库创建一个表
CREATE TABLE `blackhat` (
`id` int(10) NOT NULL auto_increment,
`tell` varchar(19) NOT NULL,
PRIMARY KEY (`id`));
我们可以测试一下首先在A server上连续插三条数据:
mysql> insert into blackhat (tell) values ('one');
Query OK, 1 row affected (0.10 sec)
mysql> insert into blackhat (tell) values ('two');
Query OK, 1 row affected (0.05 sec)
mysql> insert into blackhat (tell) values ('three');
Query OK, 1 row affected (0.02 sec)
mysql> select * from blackhat;
+----+-------+
| id | tell |
+----+-------+
| 1 | one |
| 3 | two |
| 5 | three |
然后在到B server上连续插三条数据
mysql> insert into blackhat (tell) values ('four');
Query OK, 1 row affected (0.08 sec)
mysql> insert into blackhat (tell) values ('five');
Query OK, 1 row affected (0.00 sec)
mysql> insert into blackhat (tell) values ('six');
Query OK, 1 row affected (0.04 sec)
mysql> select * from blackhat;
+----+-------+
| id | tell |
+----+-------+
| 1 | one |
| 3 | two |
| 5 | three |
| 6 | four |
| 8 | five |
| 10 | six |
+----+-------+
6 rows in set (0.00 sec)
从上面就可以理解这里的含义了。
接下来开始同步数据库数据了,首先看一下两台服务器的master 信息
A:
mysql> flush tables with read lock\G
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 528
Binlog_Do_DB: joycdn
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
B:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000004
Position: 595
Binlog_Do_DB: joycdn
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
然后备份自己的数据,保持两个机器的数据一致。
方法很多。完了后看下一步。
mysql> flush tables with read lock\G
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 528
Binlog_Do_DB: joycdn
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
B:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000004
Position: 595
Binlog_Do_DB: joycdn
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
然后备份自己的数据,保持两个机器的数据一致。
方法很多。完了后看下一步。
其实在数据库里面修改了user等信息可以不在这里继续修改了。不过position和binlog等文件确是需要改的。
在各自机器上执行CHANGE MASTER TO命令。
A:
mysql> change master to
-> master_host='rrstwo',
-> master_user='blackhat2,
-> master_password='123456',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=595;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> change master to
-> master_host='rrsone',
-> master_user='blackhat1',
-> master_password='123456',
-> master_log_file='mysql-bin.000007',
-> master_log_pos=528;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
查看各自机器上的IO进程和 SLAVE进程是否都开启。
在各自机器上执行CHANGE MASTER TO命令。
A:
mysql> change master to
-> master_host='rrstwo',
-> master_user='blackhat2,
-> master_password='123456',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=595;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> change master to
-> master_host='rrsone',
-> master_user='blackhat1',
-> master_password='123456',
-> master_log_file='mysql-bin.000007',
-> master_log_pos=528;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
查看各自机器上的IO进程和 SLAVE进程是否都开启。
A:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: rrstwo
Master_User: blackhat2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 4281328
Relay_Log_File: mysqld-relay-bin.000010
Relay_Log_Pos: 2559
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: joycdn
Replicate_Ignore_DB: mysql
B:
mysql> show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: rrsone
Master_User:blackhat1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000012
Read_Master_Log_Pos: 4515490
Relay_Log_File: mysqld-relay-bin.000009
Relay_Log_Pos: 4454857
Relay_Master_Log_File: mysql-bin.000012
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: joycdn
Replicate_Ignore_DB: mysql
只要 Slave_IO_Running: Yes 都是yes就是正常。
这个时候你可以在两台服务器上进行建表测试。
此时数据库的主主同步已经实现,开始对高可用进行部署。
首先安装
yum install ipvsadm -y
继续安装keepalived
./configure --prefix=/usr/local/keepalived
make && make install
开始配置
安装keepalived 需要依赖kernel-devel 开发包 在安装之前建议先安装 yum install kerlnel-devel -y 来进行安装。