分类: linux
2014-06-19 15:13:34
原文地址: 作者:
mysql架构之mysql dual-master双向同步
其实与master-slave同步方式并无太大的不同,只是双方相互为对方的主从服务器。并且可以扩展到多台服务器。如下图
操作系统: centos 6.4 64位
mysql版本:5.1.50
mysql(1)ip地址:192.168.100.60
mysql(2)ip地址:192.168.100.61
此步骤从略,确保两台测试机器的my.cnf相同,安装后能成功启动即可。
mysql> grant replication slave on *.* to 'repl'@'192.168.100.61' identified by '123456';
query ok, 0 rows affected (0.00 sec)
mysql> flush privileges;
query ok, 0 rows affected (0.00 sec)
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
port = 3306
socket = /usr/local/mysql/tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/dbdata
tmpdir = /tmp
log-bin = master-bin
log-bin-index = master-bin.index
replicate-same-server-id = 0
auto_increment_increment = 2
auto_increment_offset = 1
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
server-id = 1
/etc/init.d/mysqld restart
mysql> show master status;
------------------- ---------- -------------- ------------------
| file | position | binlog_do_db | binlog_ignore_db |
------------------- ---------- -------------- ------------------
| master-bin.000003 | 106 | | |
------------------- ---------- -------------- ------------------
1 row in set (0.00 sec)
注意:
记录以上两个值,等会启动复制要用到
mysql> grant replication slave on *.* to 'repl'@'192.168.100.60' identified by '123456';
query ok, 0 rows affected (0.00 sec)
mysql> flush privileges;
query ok, 0 rows affected (0.00 sec)
mysql>
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
port = 3306
socket = /usr/local/mysql/tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/dbdata
tmpdir = /tmp
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
replicate-same-server-id = 0
auto_increment_increment = 2
auto_increment_offset = 2
log-bin = master-bin
log-bin-index = master-bin.index
server-id = 2
/etc/init.d/mysqld restart
mysql> show master status;
------------------- ---------- -------------- ------------------
| file | position | binlog_do_db | binlog_ignore_db |
------------------- ---------- -------------- ------------------
| master-bin.000003 | 106 | | |
------------------- ---------- -------------- ------------------
1 row in set (0.00 sec)
注意:
记录以上两个值,等会启动复制要用到
mysql> change master to
-> master_host = '192.168.100.61',
-> master_port = 3306,
-> master_user = 'repl',
-> master_password = '123456',
-> master_log_file = 'master-bin.000003',
-> master_log_pos = 106;
query ok, 0 rows affected (0.18 sec)
mysql> start slave;
query ok, 0 rows affected (0.00 sec)
mysql> show slave status\g;
*************************** 1. row ***************************
slave_io_state: waiting for master to send event
master_host: 192.168.100.61
master_user: repl
master_port: 3306
connect_retry: 60
master_log_file: master-bin.000003
read_master_log_pos: 106
relay_log_file: slave-relay-bin.000002
relay_log_pos: 252
relay_master_log_file: master-bin.000003
slave_io_running: yes
slave_sql_running: yes
replicate_do_db:
replicate_ignore_db:
replicate_do_table:
replicate_ignore_table:
replicate_wild_do_table:
replicate_wild_ignore_table:
last_errno: 0
last_error:
skip_counter: 0
exec_master_log_pos: 106
relay_log_space: 407
until_condition: none
until_log_file:
until_log_pos: 0
master_ssl_allowed: no
master_ssl_ca_file:
master_ssl_ca_path:
master_ssl_cert:
master_ssl_cipher:
master_ssl_key:
seconds_behind_master: 0
master_ssl_verify_server_cert: no
last_io_errno: 0
last_io_error:
last_sql_errno: 0
last_sql_error:
1 row in set (0.00 sec)
mysql> change master to
-> master_host = '192.168.100.60',
-> master_port = 3306,
-> master_user = 'repl',
-> master_password = '123456',
-> master_log_file = 'master-bin.000003',
-> master_log_pos = 106;
query ok, 0 rows affected (0.11 sec)
mysql> start slave;
query ok, 0 rows affected (0.00 sec)
mysql> show slave status\g;
*************************** 1. row ***************************
slave_io_state: waiting for master to send event
master_host: 192.168.100.60
master_user: repl
master_port: 3306
connect_retry: 60
master_log_file: master-bin.000003
read_master_log_pos: 106
relay_log_file: slave-relay-bin.000002
relay_log_pos: 252
relay_master_log_file: master-bin.000003
slave_io_running: yes
slave_sql_running: yes
replicate_do_db:
replicate_ignore_db:
replicate_do_table:
replicate_ignore_table:
replicate_wild_do_table:
replicate_wild_ignore_table:
last_errno: 0
last_error:
skip_counter: 0
exec_master_log_pos: 106
relay_log_space: 407
until_condition: none
until_log_file:
until_log_pos: 0
master_ssl_allowed: no
master_ssl_ca_file:
master_ssl_ca_path:
master_ssl_cert:
master_ssl_cipher:
master_ssl_key:
seconds_behind_master: 0
master_ssl_verify_server_cert: no
last_io_errno: 0
last_io_error:
last_sql_errno: 0
last_sql_error:
1 row in set (0.00 sec)
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
port = 3306
socket = /usr/local/mysql/tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/dbdata
tmpdir = /tmp
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
replicate-same-server-id = 0
auto_increment_increment = 2
auto_increment_offset = 1
log-bin = master-bin
log-bin-index = master-bin.index
示例:在mysql(1)服务器创建example库
mysql> create database example;
query ok, 1 row affected (0.00 sec)
mysql> show databases;
--------------------
| database |
--------------------
| information_schema |
| example |
| mysql |
| test |
--------------------
4 rows in set (0.00 sec)
查看mysql(2)
mysql> show databases;
--------------------
| database |
--------------------
| information_schema |
| example |
| mysql |
| test |
--------------------
4 rows in set (0.00 sec)
示例:在mysql(2)服务器创建example1库
mysql> create database example1;
query ok, 1 row affected (0.00 sec)
mysql> show databases;
--------------------
| database |
--------------------
| information_schema |
| example |
| example1 |
| mysql |
| test |
--------------------
5 rows in set (0.00 sec)
查看mysql(1)
mysql> show databases;
--------------------
| database |
--------------------
| information_schema |
| example |
| example1 |
| mysql |
| test |
--------------------
5 rows in set (0.00 sec)
测试成功