这次是一主多从的测试,其实和一主一从是一样的原理。
master:192.168.2.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
3台上都一样:
在/etc目录下可能无my.cnf文件,从/user/share/mysql目录中拷贝my-medium.cnf 到/etc并修改成my.cnf
[[email protected] etc]# cp /usr/share/mysql/my-medium.cnf my.cnf
[[email protected] 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 上;
[[email protected] ~]# 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服务:
/etc/init.d/mysqld 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)
slave1上的配置文件my.cnf。 在[mysqld]下添加如下字段
[[email protected] ~]# 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某位。
配置如上,更改下server-id即可
在进入两个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
如果两个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命令。