MySQL双机热备份
系统: CentOS release 6.6 (Final)
MySQL: mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
主从双机热备份,master <=> slave,任何一方增删改的数据都会被同步到另一方。假设A有数据库 test,ip地址为192.168.0.110,现在要将B建立起与A的双机热备份,ip地址为192.168.1.120。
1、注意事项
a.主机的mysql版本不能高于从机的mysql版本,可以是一样的。
b.主机和从机的mysql使用的端口号必须是一样的。
2、打开AB防火墙的3306端口:
sudo iptables -I IINPUT -p tcp --dport -j ACCEPT sudo service iptables save
3、设置MySQL复制用户:
Server A
grant replcation slave on *.* to 'dbrep'@'192.168.0.110';
Server B
grant replcation slave on *.* to 'dbrep'@'192.168.0.120';
4、配置mysql
Server A:
sudo vim /etc/my.cnf #添加到[mysqld]部分 [mysqld] #... server-id=
log-bin=mysql-log-bin
binlog_format=mixed binlog-ignore-db=mysql
binlog-ignore-db=information_schema replicate-do-db=test
relay-log=mysql-relay-log
log-slave-updates=true
read-only= auto-increment-increment=
auto-increment-offset=
Server B:
sudo vim /etc/my.cnf #添加到[mysqld]部分 [mysqld] #... server-id=
log-bin=mysql-log-bin
binlog_format=mixed binlog-ignore-db=mysql
binlog-ignore-db=information_schema replicate-do-db=test
relay-log=mysql-relay-log
log-slave-updates=true
read-only= auto-increment-increment=
auto-increment-offset=
注释:
第一组,每个mysql server设定不同的server-id以区分,log-bin指定二进制日志的命名,保存的格式是mixed。
第二组,忽略不保存mysql\information_schema的二进制日志。
第三组,执行更改的数据库。
第四组,防止AB都写数据时发生冲突,设置id auto-increment的步进是2,偏移分别为1和2。这里只有两台服务器,如果有多台则可增大步进,并分别设定各自的偏移。
以上参数详见MySQL manual 复制配置部分.
重启mysqld,使配置生效。
5、从A复制数据到B,保持两者初态的数据相同。
Server A
mysql> flush tables with read lock;
mysqldump -uroot -p iksdb > dbtest.sql
mysql -uroot -p
注:多个数据库,mysqldump -uroot -p --databases test test2 test3 > dbtest123.sql。所有数据库,mysqldump -uroot -p --all-database > dball.sql。某个数据库的表,mysqldump -uroot -p db1 db1_tb1 db1_tb2 > db1_tb1_tb2.sql
Server B
mysql -uroot -p test < dbtest.sql
6、建立A到B的复制
Server A MySQL shell
mysql> show master status;
+----------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+--------------------------+
| mysql-log-bin. | | | mysql,information_schema |
+----------------------+----------+--------------+--------------------------+
row in set (0.00 sec) mysql> unlock tables;
注:在备份数据库到show master status期间,要保持数据库的锁定状态,防止数据的改变,导致因为初态不一致产生的复制错误。
Server B MySQL shell
mysql> change master to
-> master_host='192.168.0.110',
-> master_user='dbrep',
-> master_password='',
-> master_log_file='mysql-log-bin.000003',
-> master_log_pos=; mysql> slave start;
Query OK, rows affected (0.00 sec) mysql> show master status\G
*************************** . row ***************************
File: mysql-log-bin.
Position:
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,information_schema
row in set (0.00 sec) mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.110
Master_User: dbrep
Master_Port:
Connect_Retry:
Master_Log_File: mysql-log-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysql-relay-log.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-log-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
row in set (0.00 sec) mysql>
注意到 Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes,则表示成功复制。否则,可能是连接、用户或数据库初态不一致的问题。
7、建立B到A的复制,完成双向备份
Server B MySQL shell
mysql> show master status;
+----------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+--------------------------+
| mysql-log-bin. | | | mysql,information_schema |
+----------------------+----------+--------------+--------------------------+
row in set (0.00 sec)
Server A MySQL shell
mysql> slave stop;
Query OK, rows affected (0.03 sec) mysql> change master to
-> master_host='192.168.0.120',
-> master_user='dbrep',
-> master_password='',
-> master_log_file='mysql-log-bin.000001',
-> master_log_pos=; mysql> slave start;
Query OK, rows affected (0.00 sec) mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.120
Master_User: dbrep
Master_Port:
Connect_Retry:
Master_Log_File: mysql-log-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysql-relay-log.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-log-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
row in set (0.00 sec) mysql>
注:以上在B的数据不会被操作的环境下操作,如有必要,同样得锁定数据库
同样看到 Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes,表示成功复制。
注意:在数据库操作时,如果没有 选择数据的操作, 像 use test, 这个操作将不会写入到二进制日志,这样就不会完成复制。像 "insert into test.test values(1,2,3);",这样虽然能写到master数据库,但是在slave不会有任何改变。
参考:
学一点 mysql 双机异地热备份----快速理解mysql主从,主主备份原理及实践
MySQL 5.5 Reference Manual / Replication
MySQL双机热备份的更多相关文章
- MySQL双机热备份配置
双机热备份,即能够把主数据库中所有的数据同时写到备份的数据库中,从而实现MySQL数据库的热备份. MySQL Replication是MySQL提供的一种主从备份的机制,并且整个复制备份过程是异步进 ...
- MYSQL双机热备份的配置实施(问题总结)
为了实现MYSQL数据库的冗灾.备份.恢复.负载均衡等功能,喻名堂这两天一直在学习和研究mysql的双机热备,其实MYSQL的双机热备就是使用MYSQL同步功能两种方式里面的“主-主”同步方式实现的. ...
- mysql双机热备份的实现步骤
MySQL 提供了数据库的同步功能,这对我们实现数据库的冗灾.备份.恢复.负载均衡等都是有极大帮助的.本文描述了常见的同步设置方法.<?xml:namespace prefix = o /> ...
- MySQL数据库双机热备份
MySQL数据库双机热备份 1.mysql 数据库没有增量备份的机制 当数据量太大的时候备份是一个很大的问题.还好 mysql 数据库提供了一种主从备份的机制,其实就是把主数据库的所有的数据同时写到备 ...
- 学一点 MYSQL 双机异地热备份—-MYSQL主从,主主备份原理及实践
简单介绍mysql双机,多机异地热备简单原理实战. 双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据库数据一致. 这样做 ...
- 学一点 mysql 双机异地热备份----快速理解mysql主从,主主备份原理及实践
双机热备的概念简单说一下,就是要保持两个数据库的状态 自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据库数据一致. 这样做的好处多. 1. 可以做灾备,其中一个坏了可以切换 ...
- CentOS系统MySQL双机热备配置
1 概述 在集成项目中需要应对不同环境下的安装配置,主流操作系统大致可以分为三种:Linux.Windows以及UNIX.其中Linux备受青睐的主要原因有两个: 首先,Linux作为自由软件有两个 ...
- Mysql的热备份[转载]
学一点 mysql 双机异地热备份----快速理解mysql主从,主主备份原理及实践 双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始 ...
- mysql双机热备的实现
转:http://blog.csdn.net/qq394829044/article/details/53203645 Mysql数据库没有增量备份的机制,当数据量太大的时候备份是一个很大的问题.还好 ...
随机推荐
- [IIS]在CMD中IIS的使用
一.打开IIS管理器1,win+r,打开"运行" 2,输入 InetMgr,就打开了IIS管理器 二.IIS其他相关命令iisreset /RESTART 停止后启动 iisres ...
- cocos2dx loading界面 预加载资源 与 资源释放
预加载图片: 1.CCTextureCache::sharedTextureCache()->addImage("icon.png"); 2.CCTextureCache:: ...
- my first article
BLOG: http://codetask.org GIT: http://git.oschina.net/codetimer
- POJ1061 青蛙的约会-拓展欧几里得
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- 去除DEDECMS后台预览文章URL地址多余的数字信息
在使用织梦模板时发现这样一个问题:在后台预览文章的时候,出现的文章网址尽管是静态URL,但是会在网址的尾部出现问号并跟随一个时间戳,在复制URL时就显得很不方便.那么如何解决这一问题呢? 经过查找资料 ...
- CSS盒子模型学习记录3(侧面导航栏)
学习http://www.blueidea.com/tech/web/2007/4545_2.asp 代码试验 html <!DOCTYPE html PUBLIC "-//W3C// ...
- Study Emgu VoteForUniqueness
Recently i was studying Emgu, but find there is a bug in the VoteForUniqueness function in class Fea ...
- makefile学习小结
=============2016/08/15================ 上午完成makefile的试验,缩短了代码量,现在make强大,有缺省的变量,能自己推导关系,不需要gcc –MM -M ...
- Changing SharePoint Default port ( 80 ) to another port ( 79 ).
Introduction In this How-To I will change my port from 80 to 79, probably because I want to host s ...
- Explode TArray
function Explode(const Separator, S: string; Limit: Integer = 0): TArray;var SepLen : Integer; F, P ...