mysql双机热备

注意,安装MySQL时,两个数据库配置文件最好除了server-id外一样;my.cnf文件中log-bin=bin.log是必须的,data/auto.cnf里的server-uuid必须不同

1:创建同步账户:

主:
	create user repl@'%' identified by '123456';
	GRANT replication slave ON *.* TO 'repl'@'%';
	flush privileges;
	
从:
	create user repl@'%' identified by '123456';
	GRANT replication slave ON *.* TO 'repl'@'%';
	flush privileges;

2:查看状态:

主:mysql> show master status;

	+------------------+----------+--------------+------------------+-------------------+

	| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

	+------------------+----------+--------------+------------------+-------------------+

	| mysql-bin.000003 |      120 |              |                  |                   |

	+------------------+----------+--------------+------------------+-------------------+
	row in set (0.00 sec)
	
从:mysql> show master status;

	+------------------+----------+--------------+------------------+-------------------+

	| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

	+------------------+----------+--------------+------------------+-------------------+

	| mysql-bin.000003 |      437 |              |                  |                   |

	+------------------+----------+--------------+------------------+-------------------+
	row in set (0.00 sec)

3:配置同步信息:

主:
	change master to master_host='从库IP',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=437;
	start slave;
	show slave status\G;
	
	显示如下状态为成功:
	Slave_IO_Running: Yes
	Slave_SQL_Running: Yes
	
从:
	change master to master_host='主库IP',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=120;
	start slave;
	show slave status\G;
	
	显示如下状态为成功:
	Slave_IO_Running: Yes
	Slave_SQL_Running: Yes

4:如果主服务器已经存在应用数据,则在进行主从复制时,需要做以下处理:

(1)进行锁表操作:
	mysql> FLUSH TABLES WITH READ LOCK;
(2)查看主数据库状态
	mysql> show master status;
(3)记录下 FILE 及 Position 的值。
	将主服务器的数据文件(整个/opt/mysql/data目录)复制到从服务器,建议通过tar归档压缩后再传到从服务器解压。
(4)取消主数据库锁定
	mysql> UNLOCK TABLES;

5:注意:

如果是高版本,或许会遇到账户远程无法连接错误,将同步用户的密码规则定义为mysql_native_password,sql语句为:ALTER USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

6.mysql主从备份常见错误及解决方案

[ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: ‘Client requested master to start replication from impossible position’, Error_code: 1236
解决方案:出现1236,出现这种错误一般是主从服务器失去连接,出现了宕机的情况。常用解决办法,重新查询主服务器的状态,获取新的position位置,重新设置从服务器的同步信息。设置命令为:change master to master_log_file=’’,master_log_pos=123;

Last_Errno: 1032, Last_Error: Could not execute Update_rows event on table xuanzhi.test; Can’t find record in ‘test’, Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event’s master log mysql
解决方案:出现1032,表示从数据库上面缺少某一条数据记录,主数据库对这条记录又做了修改,从数据库在修改时报错。解决方案是直接用数据库管理工具,数据传输模式处理具体异常的数据表,保证主数据与从数据库对应的报错数据表结构信息一样。

Last_Errno: 1062,Last_Error: Could not execute Write_rows event on table xuanzhi.test; Duplicate entry ‘5’ for key ‘PRIMARY’, Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event’s master log
解决方案:出现1062,表示主键冲突,及从数据库上面出现了主数据库上面没有的主键信息记录。解决方案是直接删除提示的从数据库中的异常数据,或者利用数据传输模式处理具体异常的数据表。

Last_Errno: 1594,Last_Errno: 1593
解决方案:中继日志错误,一般是服务器宕机引起,解决方案和出现错误1236一样。在msql 5.5以上版本,可在slave的配置文件my.cnf里要增加一个参数relay_log_recovery=1。

mysql主从复制,经常会遇到错误而导致slave端复制中断,这个时候一般就需要人工干预,跳过错误才能继续。跳过错误有两种方式:

1.跳过指定数量的事务:
    mysql>slave stop;

	mysql>SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1       #跳过一个事务

    mysql>slave start

2.修改mysql的配置文件,通过slave_skip_errors参数来跳所有错误或指定类型的错误

     vi /etc/my.cnf[mysqld]

     slave-skip-errors=1062,1053,1146 #跳过指定error no类型的错误

     slave-skip-errors=all #跳过所有错误

校验主从服务器上面的数据是否完全一致,可通过工具pt-table-checksum操作。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值