一.准备工作
Master:
Ip:192.168.18.88
Slave:
Ip:192.168.18.89
二.设置同步
1.设置master同步
(1) 修改my.cnf配置
vim /etc/my.cnf
在[mysqld]下面添加以下参数:
slave-skip-errors=all #跳过所有错误
在#
symbolic-links=0下面添加以下参数:
server-id=1 #mysql标识
log-bin=mysql-bin #开启log-bin二进制日志文件,默认存在/var/lib/mysql下日志mysql-bin为开头
binlog-do-db =clear_iptv_ht
#clear_iptv_ht为需要同步的数据库名,如需同步多个数据库,可以另起行如binlog-do-db = clear_iptv_ht 1
# binlog-ignore-db=db_name #不进行同步日志的数据库,不需要的话注释掉
max-binlog-size=104857600 #日志的大小,超出会自动生成一个新的
master-host=192.168.18.89 #同步主机
master-user=backup #同步用户
master-password=123456 #同步用户密码
master-port=3306 #端口
replicate-do-db= clear_iptv_ht
#同步数据库
à修改完后保存退出,service mysql restart (重启正常,表示配置没错误)
(2) 在作为master的mysql上建立一个账户专门用于slave来进行数据同步
mysql>grant all on *.* to backup@'192.168.18.89'identified
by '123456';
mysql>flush privileges;
(3) 在slave上测试账户backup是否可以访问mster上的mysql:
#mysql -u backup -p -h 192.168.18.88(输入密码123456,可以访问说明设置正确)
2.设置slave同步
(1) 修改my.cnf
vi
/etc/my.cnf
在[mysqld]下面添加以下参数:
slave-skip-errors=all #跳过所有错误
在#
symbolic-links=0下面添加以下参数:
server-id= 2 #mysql标识,一定不能出现重复
log-bin=mysql-bin #开启log-bin二进制日志文件,默认存在/var/lib/mysql下日志文件以mysql-bin为开头
binlog-do-db =clear_iptv_ht #clear_iptv_ht为需要同步的数据库名,如需同步多个数据库,可以另起行如binlog-do-db =clear_iptv_ht1
max-binlog-size=104857600 #日志的大小,超出会自动生成一个新的
master-host=192.168.18.88 #同步Master的ip地址
master-user=backup #同步所需的账号
master-password=123456 #同步账号的密码
master-port=3306 #端口
replicate-do-db=clear_iptv_ht #所需同步的数据库名
#replicate-ignore-db=mysql #不同步mysql库
#replicate-do-table=clear_iptv_ht.music #如果只需同步clear_iptv_ht数据库中的music表
master-connect-retry=60 #主服务器宕机或连接丢失的情况下,从服务器线程重新尝试连接主服务器之前睡眠的秒数
>>>>>修改完后保存退出,并重启mysql 如:service mysql restart (重启正常,表示配置没错误)
(2) 在作为slave的mysql上建立用来同步的账户和库
mysql>grant all on
*.* to backup@'192.168.18.88' identified by '123456';
mysql>flush privileges;
(3) 在master上测试账户backup是否可以访问salve上的mysql:
#mysql -u backup -p -h 192.168.18.89(输入密码123456,可以访问说明设置正确)
三.查看状态
1.在Master上:
mysql> show master
status;
+------------------+----------+--------------+------------------+
|
File | Position | Binlog_Do_DB
| Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
|
mysql-bin.000005 | 189 | clear_iptv_ht | |
+------------------+----------+--------------+------------------+
1
row in set (0.00 sec)
mysql> show slave
status\G
*************************** 1. row
***************************
Slave_IO_State: Waiting for
master to send event
Master_Host: 192.168.18.89
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 207
Relay_Log_File:
mysqld-relay-bin.000002
Relay_Log_Pos: 344
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: clear_iptv_ht
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: 207
Relay_Log_Space: 344
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
1
row in set (0.00 sec)
2.在Slave上:
mysql>
show master status;
+------------------+----------+--------------+------------------+
|
File | Position | Binlog_Do_DB
| Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
|
mysql-bin.000005 | 207 | clear_iptv_ht | |
+------------------+----------+--------------+------------------+
1
row in set (0.00 sec)
mysql> show slave
status\G
*************************** 1. row
***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.18.88
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 189
Relay_Log_File:
mysqld-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: clear_iptv_ht
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: 189
Relay_Log_Space: 326
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
1
row in set (0.00 sec)
在slave服务器上执行show slave status\G;
Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running:
Yes
如以上二行同时为Yes 说明配置成功
注:①主从服务器都添加(slave-skip-errors=all)这个参数是为了跳过所以错误。
为什么要跳过呢?
在主从复制中,难免会遇到一些sql语句错误的问题。这个时候我们需要手动来重新设置中继日志的起始点了,有些麻烦。所以直接跳过错误。
Slave_IO_Running: Yes
Slave_SQL_Running: no
如果有一行为no了,说明主从就不同步了。
②参数如下:
#slave-skip-errors=all #跳过所有错误
#slave-skip-errors=1062,1053,1146 #跳过指定error no类型的错误
具体的错误代码表现如下:
1007:数据库已存在,创建数据库失败
1008:数据库不存在,删除数据库失败
1050:数据表已存在,创建数据表失败
1050:数据表不存在,删除数据表失败
1054:字段不存在,或程序文件跟数据库有冲突
1060:字段重复,导致无法插入
1061:重复键名
1068:定义了多个主键
1094:位置线程ID
1146:数据表缺失,请恢复数据库
3.验证是否配置正确
四.测试
向master的clear_iptv_ht数据库里插数据,然后到slave的clear_iptv_ht数据库查看。