MySQL雙Master配置的方法詳解
更新時(shí)間:2013年06月18日 10:52:43 作者:
本篇文章是對(duì)MySQL雙Master配置進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
剛剛抽空做了一下MYSQL 的主主同步。
把步驟寫下來(lái),至于會(huì)出現(xiàn)的什么問題,以后隨時(shí)更新。這里我同步的數(shù)據(jù)庫(kù)是TEST
1、環(huán)境描述。
主機(jī):192.168.0.231(A)
主機(jī):192.168.0.232(B)
MYSQL 版本為5.1.21
2、授權(quán)用戶。
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 服務(wù)器。
3、配置文件。
在兩個(gè)機(jī)器上的my.cnf里面都開啟二進(jìn)制日志 。
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
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
auto_increment_increment=2
auto_increment_offset=2
至于這些參數(shù)的說(shuō)明具體看手冊(cè)。
紅色的部分非常重要,如果一個(gè)MASTER 掛掉的話,另外一個(gè)馬上接管。
紫紅色的部分指的是服務(wù)器頻繁的刷新日志。這個(gè)保證了在其中一臺(tái)掛掉的話,日志刷新到另外一臺(tái)。從而保證了數(shù)據(jù)的同步 。
4、重新啟動(dòng)MYSQL服務(wù)器。
在A和B上執(zhí)行相同的步驟
[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、進(jìn)入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)
然后備份自己的數(shù)據(jù),保持兩個(gè)機(jī)器的數(shù)據(jù)一致。
方法很多。完了后看下一步。
6、在各自機(jī)器上執(zhí)行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、查看各自機(jī)器上的IO進(jìn)程和 SLAVE進(jìn)程是否都開啟。
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)
如果紅色部分沒有出現(xiàn),檢查DATA目錄下的錯(cuò)誤文件。
8、釋放掉各自的鎖,然后進(jìn)行插數(shù)據(jù)測(cè)試。
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
插入之前兩個(gè)機(jī)器表的對(duì)比:
A:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t22 |
+----------------+
B:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t22 |
+----------------+
從A機(jī)器上進(jìn)行插入
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)
現(xiàn)在來(lái)看B機(jī)器:
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)
現(xiàn)在反過來(lái)從B機(jī)器上插入數(shù)據(jù):
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)
我們來(lái)看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)
好了?,F(xiàn)在兩個(gè)表互相為MASTER。
把步驟寫下來(lái),至于會(huì)出現(xiàn)的什么問題,以后隨時(shí)更新。這里我同步的數(shù)據(jù)庫(kù)是TEST
1、環(huán)境描述。
主機(jī):192.168.0.231(A)
主機(jī):192.168.0.232(B)
MYSQL 版本為5.1.21
2、授權(quán)用戶。
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 服務(wù)器。
3、配置文件。
在兩個(gè)機(jī)器上的my.cnf里面都開啟二進(jìn)制日志 。
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
skip-name-resolve
sync_binlog=1auto_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
skip-name-resolve
sync_binlog=1auto_increment_increment=2
auto_increment_offset=2
至于這些參數(shù)的說(shuō)明具體看手冊(cè)。
紅色的部分非常重要,如果一個(gè)MASTER 掛掉的話,另外一個(gè)馬上接管。
紫紅色的部分指的是服務(wù)器頻繁的刷新日志。這個(gè)保證了在其中一臺(tái)掛掉的話,日志刷新到另外一臺(tái)。從而保證了數(shù)據(jù)的同步 。
4、重新啟動(dòng)MYSQL服務(wù)器。
在A和B上執(zhí)行相同的步驟
[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、進(jìn)入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)
然后備份自己的數(shù)據(jù),保持兩個(gè)機(jī)器的數(shù)據(jù)一致。
方法很多。完了后看下一步。
6、在各自機(jī)器上執(zhí)行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、查看各自機(jī)器上的IO進(jìn)程和 SLAVE進(jìn)程是否都開啟。
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)
如果紅色部分沒有出現(xiàn),檢查DATA目錄下的錯(cuò)誤文件。
8、釋放掉各自的鎖,然后進(jìn)行插數(shù)據(jù)測(cè)試。
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
插入之前兩個(gè)機(jī)器表的對(duì)比:
A:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t22 |
+----------------+
B:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t22 |
+----------------+
從A機(jī)器上進(jìn)行插入
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)
現(xiàn)在來(lái)看B機(jī)器:
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)
現(xiàn)在反過來(lái)從B機(jī)器上插入數(shù)據(jù):
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)
我們來(lái)看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)
好了?,F(xiàn)在兩個(gè)表互相為MASTER。
相關(guān)文章
Mysql8.0密碼問題mysql_native_password和caching_sha2_password詳解
這篇文章主要介紹了Mysql8.0密碼問題mysql_native_password和caching_sha2_password,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
MySql官方手冊(cè)學(xué)習(xí)筆記2 MySql的模糊查詢和正則表達(dá)式
MySQL提供標(biāo)準(zhǔn)的SQL模式匹配,以及擴(kuò)展正則表達(dá)式模式匹配的格式2012-10-10
MySQL數(shù)據(jù)庫(kù)備份以及常用備份工具集合
數(shù)據(jù)庫(kù)備份種類按照數(shù)據(jù)庫(kù)大小備份,有四種類型,分別應(yīng)用于不同場(chǎng)合。本文將MySQL 數(shù)據(jù)庫(kù)備份種類以及常用備份工具進(jìn)行匯總,方便大家學(xué)習(xí)。2015-08-08
MySQL?B-tree與B+tree索引數(shù)據(jù)結(jié)構(gòu)剖析
這篇文章主要介紹了MySQL?B-tree與B+tree索引數(shù)據(jù)結(jié)構(gòu)剖析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
MySQL server has gone away的問題解決
本文主要介紹了MySQL server has gone away的問題解決,意思就是指client和MySQL server之間的鏈接斷開了,下面就來(lái)介紹一下幾種原因及其解決方法,感興趣的可以了解一下2024-07-07

