[安装]MASTER到MASTER的主主循环同步

1473阅读 0评论2008-07-24 hb_li_520
分类:Mysql/postgreSQL

把步骤写下来,至于会出现的什么问题,以后随时更新。这里我同步的数据库是TEST
1、环境描述。
   主机:192.168.6.13(A)
   主机:192.168.6.14(B)
   MYSQL 版本为:
mysql> select version();
+---------------+
| version()     |
+---------------+
| 5.1.26-rc-log |
+---------------+
1 row in set (0.00 sec)

2、授权用户。
A:
# replication slave--用于复制型从属服务器(从主服务器中读取二进制日志事件)
# file--允许使用SELECT...INTO OUTFILELOAD DATA INFILE
# 关于file 官方网站没有说明一定要这个选项
# *.* 那就是前一个是库,每一个是表(但请不要在这里利用这个限制不同表同步的问题.不允许的!^0^)
mysql> grant replication slave,file on *.* to 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 'repl2'@'192.168.0.231' identified
 by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
然后都停止MYSQL 服务器。

3、配置文件。
在两个机器上的my.cnf里面都开启二进制日志 。
(日志文件找不到时:find / -name my*.cnf;找到后copy过到/etc/my.cnf !
我的在/usr/local/mysql/support-files)
A:
[mysqld]
user = mysql
log-bin=mysql-bin
#开启二进制文件是一定要的啦!
server-id       = 1
replicate-wild-do-table=test.test
replicate-wild-do-table=test.t11_replicas
#指定确定的表同步(如果有多个表,那个多列一点了.库估计也是一个样的设计.)
#如果做完之后,后来还要增加表.那后面的change master and slave也要重新设计
binlog-do-db=test
#只同步test库
binlog-ignore-db=mysql
#忽略掉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

B:
[mysqld]
user = mysql
log-bin=mysql-bin
server-id       = 2
replicate-wild-do-table=test.test
replicate-wild-do-table=test.t11_replicas
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

至于这些参数的说明具体看手册。
红色的部分非常重要,如果一个MASTER 挂掉的话,另外一个马上接管。
紫红色的部分指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。
4、重新启动MYSQL服务器。
在A和B上执行相同的步骤
[root@localhost ~]# /usr/local/mysql/bin/mysqld_safe &
[1] 4264
[root@localhost ~]# 071213 14:53:20 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
/usr/local/mysql/bin/mysqld_safe: line 366: [: -eq: unary operator expected
071213 14:53:20 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

5、进入MYSQL的SHELL。
A:
mysql> flush tables with read lock\G
Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G   #\G只是做一个格式的描述
*************************** 1. row ***************************
            File: mysql-bin.000007
        Position: 528
    Binlog_Do_DB: test
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: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
然后备份自己的数据,保持两个机器的数据一致。
方法很多。完了后看下一步。
6、在各自机器上执行CHANGE MASTER TO命令。
A:
mysql> change master to
    -> master_host='192.168.6.13',
    -> master_user='repl2',
    -> 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)
 
change master to master_host='192.168.1.206',master_user='repl2',master_password='suretech',master_log_file='mysql-bin.000006',master_log_pos=102697;
(写成一行,方便COPY,记得要改的地方呀!^0^)

B:
mysql> change master to
    -> master_host='192.168.6.14',
    -> master_user='repl1',
    -> 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)

7、查看各自机器上的IO进程和 SLAVE进程是否都开启。
A:

mysql> show processlist\G
*************************** 1. row ***************************
     Id: 2
   User: repl
   Host: 192.168.0.232:54475
     db: NULL
Command: Binlog Dump
   Time: 1590
  State: Has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL
*************************** 2. row ***************************
     Id: 3
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 1350
  State: Waiting for master to send event
   Info: NULL
*************************** 3. row ***************************
     Id: 4
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 1149
  State: Has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
*************************** 4. row ***************************
     Id: 5
   User: root
   Host: localhost
     db: test
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
4 rows in set (0.00 sec)

B:

mysql> show processlist\G
*************************** 1. row ***************************
     Id: 1
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 2130
  State: Waiting for master to send event
   Info: NULL
*************************** 2. row ***************************
     Id: 2
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 1223
  State: Has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
*************************** 3. row ***************************
     Id: 4
   User: root
   Host: localhost
     db: test
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
*************************** 4. row ***************************
     Id: 5
   User: repl2
   Host: 192.168.0.231:50718
     db: NULL
Command: Binlog Dump
   Time: 1398
  State: Has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL
4 rows in set (0.00 sec)

如果红色部分没有出现,检查DATA目录下的错误文件。

8、释放掉各自的锁,然后进行插数据测试。
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

插入之前两个机器表的对比:
A:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t22            |
+----------------+
B:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t22            |
+----------------+
从A机器上进行插入
A:
mysql> create table t11_replicas
    -> (id int not null auto_increment primary key,
    -> str varchar(255) not null) engine myisam;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t11_replicas(str) values
    -> ('This is a master to master test table');
Query OK, 1 row affected (0.01 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t11_replicas   |
| t22            |
+----------------+
3 rows in set (0.00 sec)

mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)


现在来看B机器:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t11_replicas   |
| t22            |
+----------------+
3 rows in set (0.00 sec)

mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)

现在反过来从B机器上插入数据:
B:

mysql> insert into t11_replicas(str) values('This is a test 2');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
|  2 | This is a test 2                      |
+----+---------------------------------------+
2 rows in set (0.00 sec)
我们来看A
A:
mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
|  2 | This is a test 2                      |
+----+---------------------------------------+
2 rows in set (0.00 sec)

好了。现在两个表互相为MASTER。
 
分析:如果数据量较大,同步速度不是很理想,我以十万条数据量为例子做测试:
平均每秒23条;仅参考

 
 
此文转载地址http://blog.chinaunix.net/u/29134/showart_441667.html
只是有些细节的地方作了修改!
而且表的结构也超简单:

DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`SURETECH` $$
CREATE DEFINER=`suretech`@`%` PROCEDURE `SURETECH`()
BEGIN
   DECLARE I INT;
   SET i= 0;
   WHILE I<100000 DO
       insert into test(str) values (I+' ');
       SET I=I+1;
   END WHILE;
end $$

DELIMITER

如果两台机器有一台挂机之后要重新设置才可以同步!

而且效率偏低!

 

今天又做了一次测试,网内基本可以时时做到同步,速度非常不错。速度方面可以放心使用

DELIMITER $$

DROP PROCEDURE IF EXISTS `ngi`.`testdata` $$
CREATE DEFINER=`root`@`%` PROCEDURE `testdata`()
BEGIN
   DECLARE i INT;
   SET i= 0;
   WHILE i<100000 DO
       insert into Invoices(InvoiceReferenceNo,InvDescription,InvGenerationDate,InvFrom,InvTo,InvDueDate,AccNo,InvTAmount,InvAmount,InvCCTAmount,InvCCAmount,InvCCTax1Amount,InvCCTax2Amount,PreviousAmount,PreviousCCAmount,AdjustedAmount,AdjustedCCAmount,AdminID) values
       (i,'2','2008-08-06 01:19:38','2008-08-06 01:19:38','2008-08-06 01:19:38','2008-08-06 01:19:38',3,4,5,6,7,8,9,10,11,12,13,14);
       SET I=I+1;
   END WHILE;
end $$

DELIMITER

 
 
过了一段时间出现了以下问题:
 

mysql> start slave;
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log

解决问题:

在/usr/local/mysql/data下发现有两个stb.err和webguiserver.err;

说明有人将电脑名更改了:

怎么修复?

不能修复的话重新安装一次吧!^0^(记住不能随便更改电脑名啦!)

不过也挺简单!如遇到问题再继续.

上一篇:关于VS2005通过“添加变量”向导来添加控件变量时出现“控件变量”被禁用的处理办法!
下一篇:NS2 for Install Linux