程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> MYSQL的MASTER到MASTER的主主循環同步

MYSQL的MASTER到MASTER的主主循環同步

編輯:關於MYSQL數據庫

    以前抽空做的MySQL 的主主同步。
不過心理做好准備,對性能會有一定的影響!
把步驟寫下來,至於會出現的什麼問題,以後隨時更新。這裡我同步的數據庫是TEST
1、環境描述。
   主機:192.168.0.231(A)
   主機:192.168.0.232(B)
   MySQL 版本為5.1.21
2、授權用戶。
A:
MySQL> grant replication slave,file on *.* to 'repl1'@'192.168.0.232' 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裡面都開啟二進制日志 。
A:
user = MySQL
log-bin=MySQL-bin
server-id       = 1
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=1

B:
user = MySQL
log-bin=MySQL-bin
server-id       = 2
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
*************************** 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.0.232',
    -> 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)


B:
MySQL> change master to
    -> master_host='192.168.0.231',
    -> 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。

多MASTER自增字段沖突的問題。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved