1
2
|
grant replication slave on *.* to
'replication'
@
'192.168.1.%'
identified by
'000000'
;
flush privileges;
|
1
2
3
4
5
|
[mysqld]
server-
id
= 1
log-bin=
/data0/mysql/binlog/binlog
binlog-
do
-db =
test
binlog-ignore-db=mysql
|
1
2
3
4
5
|
# /data0/mysql/mysql restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
|
1
2
3
4
5
6
7
8
9
10
|
ysql>flush tables with
read
lock;
mysql>show master status\G
*************************** 1. row ***************************
File: binlog.000006
Position: 107
Binlog_Do_DB:
test
Binlog_Ignore_DB: mysql
1 row
in
set
(0.00 sec)
mysql>unlock tables;
|
注:这里锁表的目的是为了生产环境中不让进新的数据,好让从服务器定位同步位置。初次同步完成后,记得解锁。
a)修改mysql配置文件
1
2
3
4
5
6
|
[mysqld]
server-
id
= 2
log-bin =
/data0/mysql/binlog/binlog
replicate-
do
-db =
test
replicate-ignore-db = mysql,information_schema
replicate-ignore-table=
test
.table_name
#排除表
|
1
2
3
4
5
|
# /data0/mysql/mysql restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
|
1
|
mysql>change master to master_host=
'192.168.1.106'
, master_user=
'replication'
, master_password=
'000000'
, master_log_file=
'binlog.000006'
, master_log_pos=107;
|
d)启动从服务器线程
1
|
mysql>start slave;
|
我在实际操作中出现了启动不了的情况,错误提示出下: mysql> start slave; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 3 Current database: data_center ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO 使用show slave status查看状态时发现有这样的错误提示: Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it). GOOGLE说是从服务器配置文件里的server_id没有生效,使用set global server_id=2;动态修改server_id,然后start slave;成功
1
2
3
4
|
mysql>show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
|
C、测试主从同步
先在主库中插入一条新的数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
mysql> use
test
Database changed
mysql> desc wp_terms;
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra
+------------+---------------------+------+-----+---------+----------------+
| term_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment
| name | varchar(200) | NO | MUL |
| slug | varchar(200) | NO | UNI |
| term_group | bigint(10) | NO | | 0
+------------+---------------------+------+-----+---------+----------------+
4 rows
in
set
(0.02 sec)
mysql> insert wp_terms values(1116,
'MikeTestMySQL'
,
'Mike-Test-Mysql-Repli'
,0);
Query OK, 1 row affected (0.03 sec)
|
1
2
3
4
5
6
7
|
mysql>
select
* from
test
.wp_terms where term_id=1116;
+---------+---------------+-----------------------+------------+
| term_id | name | slug | term_group
+---------+---------------+-----------------------+------------+
| 1116 | MikeTestMySQL | Mike-Test-Mysql-Repli | 0
+---------+---------------+-----------------------+------------+
1 row
in
set
(0.00 sec)
|
下面的是完整配置过程,如已按上面配置了主从架构,只需调整差异部分就好了。
A、环境描述
服务器A(主) 192.168.1.106
服务器B(主) 192.168.1.107
Mysql版本:5.5.11
System OS:CentOS 5.6 X64
主从需同步的数据库内容保持一致。
B、主主配置过程
a)创建同步用户
在主服务器上为从服务器建立一个连接帐户,该帐户必须授予REPLICAITON SLAVE权限。
这里服务器A和服务器B互为主从,所以都要分别建立一个同步用户。
服务器A和B:
1
2
|
grant replication slave on *.* to
'replication'
@
'192.168.1.%'
identified by
'000000'
;
flush privileges;
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[mysqld]
server-
id
= 1
log-bin=
/data0/mysql/binlog/binlog
binlog-
do
-db =
test
binlog-ignore-db=mysql
#主主需加入的部分
log-slave-updates
sync_binlog=1
auto_increment_offset=1
auto_increment_increment=2
replicate-
do
-db =
test
replicate-ignore-db = mysql,information_schema
|
服务器B
1
2
3
4
5
6
7
8
9
10
11
12
|
[mysqld]
server-
id
= 2
log-bin =
/data0/mysql/binlog/binlog
replicate-
do
-db =
test
replicate-ignore-db = mysql,information_schema
#主主需要加入部分
binlog-
do
-db =
test
binlog-ignore-db=mysql
log-slave-updates
sync_binlog=1
auto_increment_offset=2
auto_increment_increment=2
|
c)分别重启服务器A、B上的mysql服务
1
2
3
4
5
|
# /data0/mysql/mysql restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
|
1
2
3
4
5
6
7
8
9
10
|
mysql>flush tables with
read
lock;
mysql> show master status\G
*************************** 1. row ***************************
File: binlog.000007
Position: 107
Binlog_Do_DB:
test
Binlog_Ignore_DB: mysql
1 row
in
set
(0.00 sec)
mysql>unlock tables;
|
B服务器
1
2
3
4
5
6
7
|
mysql> show master status\G
*************************** 1. row ***************************
File: binlog.000005
Position: 107
Binlog_Do_DB:
test
Binlog_Ignore_DB: mysql
1 row
in
set
(0.00 sec)
|
1
|
mysql>change master to master_host=
'192.168.1.107'
, master_user=
'replication'
, master_password=
'000000'
, master_log_file=
'binlog.000005'
, master_log_pos=107;
|
1
|
mysql>change master to master_host=
'192.168.1.106'
, master_user=
'replication'
, master_password=
'000000'
, master_log_file=
'binlog.000007'
, master_log_pos=107;
|
1
|
mysql>start slave;
|
e)分别在服务器A、B上查看从服务器状态
1
2
3
4
|
mysql>show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
|
1
2
3
4
5
6
7
8
9
|
#删除binlog.000002之前的而不包含binlog.000002
mysql> PURGE MASTER LOGS TO
'binlog.000002'
;
#删除2011-05-28 1:35:00之前的
mysql> PURGE MASTER LOGS BEFORE
'2011-05-28 1:35:00'
;
#清除3天前的binlog
mysql> PURGE MASTER LOGS BEFORE DATE_SUB(NOW( ), INTERVAL 3 DAY);
|
1
2
|
[mysqld]
expire-logs-days = 7
|