mysql主从复制不成功_mysql主从同步失败处理
mysql主从同步失败
slave:
#查看同步状态
mysql> show slave status \G;
Slave_IO_Running: Yes
Slave_SQL_Running: No
Last_Errno: 1062
Last_Error: Error 'Duplicate entry '1' for key 'PRIMARY'' on query. Default database: 'pythondb'. Query: 'insert into departments values (1,'a')'
160726 14:07:13 [ERROR] Slave SQL: Error 'Duplicate entry '1' for key 'PRIMARY'' on query. Default database: 'pythondb'. Query: 'insert into departments values (1,'a')', Error_code: 1062
160726 14:07:13 [Warning] Slave: Duplicate entry '1' for key 'PRIMARY' Error_code: 1062
160726 14:07:13 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.003217' position 77331916
⽅法⼀:忽略错误后,继续同步
该⽅法适⽤于主从库数据相差不⼤,或者要求数据可以不完全统⼀的情况,数据要求不严格的情况
解决:
stop slave;
#表⽰跳过⼀步错误,后⾯的数字可变
set global sql_slave_skip_counter =1;
start slave;
之后再⽤mysql> show slave status\G 查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
⽅式⼆:重新做主从,完全同步
该⽅法适⽤于主从库数据相差较⼤,或者要求数据完全统⼀的情况
解决步骤如下:
1.先进⼊主库,进⾏锁表,防⽌数据写⼊
使⽤命令:
mysql> flush tables with read lock;
注意:该处是锁定为只读状态,语句不区分⼤⼩写
2.进⾏数据备份
#把数据备份到mysql.bak.sql⽂件
[root@server01 mysql]#mysqldump -uroot -p -hlocalhost > mysql.bak.sql
这⾥注意⼀点:数据库备份⼀定要定期进⾏,可以⽤shell脚本或者python脚本,都⽐较⽅便,确保数据万⽆⼀失
mysql下载odbc失败3.查看master 状态
mysql> show master status;
+-------------------+----------+--------------+-------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+-------------------------------+
| mysqld-bin.000001 | 3260 | | mysql,test,information_schema |
+-------------------+----------+--------------+-------------------------------+
1 row in set (0.00 sec)
4.把mysql备份⽂件传到从库机器,进⾏数据恢复
#使⽤scp命令
[root@server01 mysql]# scp mysql.bak.sql root@192.168.128.101:/tmp/
5.停⽌从库的状态
mysql> stop slave;
6.然后到从库执⾏mysql命令,导⼊数据备份
mysql> source /tmp/mysql.bak.sql
7.设置从库同步,注意该处的同步点,就是主库show master status信息⾥的| File| Position两项
change master to master_host = '192.168.128.100', master_user = 'rsync', master_port=3306, master_password='', master_log_file = 'mysqld-bin.000001', master_log_pos=3260;
8.重新开启从同步
mysql> start slave;
9.查看同步状态
mysql> show slave status\G 查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
好了,同步完成啦。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论