1. 程式人生 > >Mysql雙向同步熱備份設置

Mysql雙向同步熱備份設置

bsp 數據庫 -s 進程 mman 就是 offset change relay

1、環境描述。
主機:103.241.49.137(A)
主機:103.240.182.191(B)
MYSQL 版本為5.1.11
2、授權用戶。(本人比較懶,直接用的root 跳過這一步)
A:
mysql> grant replication slave,file on *.* to ‘backup_251‘@‘103.240.182.191‘ 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 ‘backup_167‘@‘103.241.49.137‘ 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,在windows下面就是my.ini文件了 裏面都開啟二進制日誌 。

找到[mysqld]這個結點,在下面增加以下內容

vi /etc/my.cnf
A:
user = mysql
log-bin=mysql-bin
server-id = 1
binlog-do-db=test (需要同步的庫,如果要同步整個數據庫,就把這句去掉)
binlog-ignore-db=mysql (不需要同步的庫)
replicate-do-db=test
//log-slave-updates 為了讓slave也能充當master,寫relay-log的時候也會寫到bin-log
replicate-ignore-db=mysql
slave-skip-errors=all #出現錯誤後忽略,如果不加這個,出現任何錯誤,同步進程會終止
//master-connect-retry=60 斷點重新連接時間
sync_binlog=1
auto_increment_increment=2 (因為是雙向,自動增加的id會有沖突,把步長改為2 初始設為1)
auto_increment_offset=1


///////////////////////////////////////////
server-id = 1
log-bin=/tmp/logbin/mysql-bin
binlog-do-db =sixfj
binlog-ignore-db=mysql
replicate-do-db=sixfj
replicate-ignore-db=mysql
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1


// linux關閉MySQL的命令
/usr/local/mysql/bin/mysqladmin -uroot -p shutdown
  // linux啟動MySQL的命令
/usr/local/mysql/bin/mysqld_safe &


/usr/local/mysql/bin/mysql -uroot -pgdczxw2008


grant replication slave on *.* to czxync@‘103.240.182.191‘ identified by ‘aa12345q‘;

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


slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2 (因為是雙向,自動增加的id會有沖突,把步長改為2 初始設為2)
auto_increment_offset=2

///////////////////////////////////

server-id = 3
log-bin=/tmp/logbin/mysql-bin
binlog-do-db =sixfj
binlog-ignore-db=mysql
replicate-do-db=sixfj
replicate-ignore-db=mysql
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2

至於這些參數的說明具體看手冊。

4、重新啟動MYSQL服務器。

mysql>grant replication slave on *.* to czxync@‘103.241.49.137‘ identified by ‘aa12345q‘;

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=‘103.240.182.191‘,
-> master_user=‘backup_251‘,
-> 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=‘103.241.49.137‘,
-> master_user=‘backup_167‘,
-> 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: 103.240.182.191: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: 103.241.49.137: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)

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。

Mysql雙向同步熱備份設置