mysql一主多从同步配置

以前写过一篇"mysql主从同步配置"http://blog.sina.com.cn/s/blog_4c197d420100w7uz.html针对的是一主一从的情况。
这次是一主多从的测试,其实和一主一从是一样的原理。
一、环境
master:192.168.101
MYSQL版本:5.1.48-community-log
slave1:192.168.2.182
MYSQL版本:5.1.48-community-log
slave2:192.168.2.111
MYSQL版本:5.1.48-community-log
so...1 vs 2。
二、master和 slave上的相关配置
3台上都一样:
在/etc目录下可能无my.cnf文件,从/user/share/mysql目录中拷贝my-medium.cnf 到/etc并修改成my.cnf
[root@localhost etc]# cp /usr/share/mysql/my-medium.cnf my.cnf
[root@localhost etc]# ll |grep my
-rwxr-xr-x 1 root root 5204 Feb 13 22:52 my_bak
-rwxr-xr-x 1 root root 4765 Jul 10 23:07 my.cnf
master 上;
[root@mysql101 ~]# vi /etc/my.cnf
1.修改master上的配置文件my.cnf。
在[mysqld]下添加如下字段:
server-id = 1
log-bin=mysql-bin
binlog-do-db=YYY //需要同步的数据库
binlog-ignore-db=mysql //被忽略的数据库
binlog-ignore-db=information-schema //被忽略的数据库
在master上为slave添加一个同步账号
mysql> grant replication slave on *.* to 'affairlog'@'192.168.2.182' identified by 'pwd123';
//在slave1上登陆成功
mysql> grant replication slave on *.* to 'affairlog'@'192.168.2.111' identified by 'pwd123';
//在slave2上登陆成功
保存后,重启master的mysql服务:
service mysql restart;
用show master status命令查看日志情况
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000087
Position: 106
Binlog_Do_DB: YYY
Binlog_Ignore_DB: mysql,information-schema
1 row in set (0.00 sec)
2.修改slave1上的配置文件my.cnf。
在[mysqld]下添加如下字段
[root@mysql182 ~]# vi /etc/my.cnf
server-id=182
master-host=192.168.3.101
master-user= affairlog
master-password=pwd123
master-port=3306
master-connect-retry=60
replicate-do-db=YYY //同步的数据库
replicate-ignore-db=mysql //被忽略的数据库
replicate-ignore-db=information-schema //被忽略的数据库
保存后,重启slave的mysql服务:
service mysql restart;
修改slave2上的配置文件my.cnf,和上面类似,只是把server-id改下,为了方便,我都用了相应的ip某位,
so,slave2上我设置的server-id是111。
在进入两个slave机中的mysql。
mysql>start slave;
mysql>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.3.101
Master_User: affairlog
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000087
Read_Master_Log_Pos: 106
Relay_Log_File: vm111-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000087
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: YYY
Replicate_Ignore_DB: mysql,information-schema
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: 406
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)
如果两个slave中的Slave_IO_Running、Slave_SQL_Running状态均为Yes则表明设置成功。
三、可能遇到的问题:
※ Slave_IO_Running: No或者Slave_SQL_Running: No
1.停掉slave服务
mysql> slave stop;
Query OK, 0 rows affected (2.01 sec)
2.解决办法
a.在master上查看。
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000087
Position: 1845
Binlog_Do_DB: YYY
Binlog_Ignore_DB: mysql,information-schema
1 row in set (0.00 sec)
b.到slave上手动同步。
mysql>change master to
>master_host='192.168.3.101',
>master_user='affairlog',
>master_password='pwd123',
>master_log_file='mysql-bin.000087',
>master_log_pos=1845;
Query OK, 0 rows affected (0.00 sec)

3.启动slave服务
mysql> slave start;
4.再次查看Slave_IO_Running、Slave_SQL_Running状态,为Yes则表明设置成功。
※ ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
错误重现:
mysql>change master to
>master_host='192.168.3.101',
>master_user='affairlog',
>master_password='pwd123',
>master_log_file='mysql-bin.000087',
>master_log_pos=1845;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
解决方法:
1.停掉slave服务
mysql> slave stop;
Query OK, 0 rows affected (2.01 sec)
2.重置slave服务
mysql> reset stop;
Query OK, 0 rows affected (2.01 sec)
3.再执行一次change命令
mysql>change master to
>master_host='192.168.3.101',
>master_user='affairlog',
>master_password='pwd123',
>master_log_file='mysql-bin.000087',
>master_log_pos=1845;
Query OK, 0 rows affected (0.00 sec)
4.启动slave服务
mysql> slave start;
5.再次查看Slave_IO_Running、Slave_SQL_Running状态,为Yes则表明设置成功。
PS:
Slave_IO_Running:连接到主库,并读取主库的日志到本地,生成本地日志文件

Slave_SQL_Running:读取本地日志文件,并执行日志里的SQL命令。


转载于:http://blog.sina.com.cn/s/blog_4c197d4201017qjs.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值