56,687
社区成员
发帖
与我相关
我的任务
分享
-- 新进一公司,用的是My SQL数据库!
-- 现在:公司有一生产库,mysqldump 执行,备份文件下来(不压缩)有近20G,需要备份时间2小时左右,
-- 同时若在原库的基础上搭建集群(先考虑主----主集群)的话,需要拷贝文件15分钟,恢复到另一主库需要2小时,
-- 这样下来生产库总共需要停机4个多小时(不出意外)。
-- 公司是一个团购公司,4个小时的停机时间(每小时损失约5万),这样算来来的话,公司损失近20万,自然是难以忍受的!
-- 所以:我采用的是增量备份与恢复的方法,只要半个小时完事儿,为公司挽回损失20万!
-- 操作步骤大致如下:
-- 先执行增量备份的完全备份:(备份前,我先看一下当前log日志的最大文件号)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000630
Position: 78686586
Binlog_Do_DB: groupon
Binlog_Ignore_DB:
1 row in set (0.00 sec)
-- 所以:当我执行下面的备份操作时,因为加了flush-logs参数,
-- 在执行备份这个时间点上,My SQL会自动切换日志文件(例如:生成 mysql-bin.000631)
mysqldump -uroot -pyourpassword db_name --single-transaction --flush-logs --master-data=2 --delete-master-logs > bk_db_name_20110719.sql &
-- 备份后,打包,压缩,拷贝到远程服务器(集群的另一主节点),进行恢复:
/var/mysql/bin/mysql -uroot -pyourpassword db_name < /var/www/mysqlBackup/bk_239_db/bk_db_name_20110719.sql &
-- 恢复操作成功后,假设当前的源库的日志文件为: mysql-bin.000633
-- 那么,我们还需要:
-- 首先:切换源库的日志文件:
mysql> flush logs
-- 此时:源库当前的日志文件应该为:mysql-bin.000634
-- 我们先将源库的日志文件:mysql-bin.000631 mysql-bin.000632 mysql-bin.000633 拷贝到将要搭建集群的另一主节点,并进行日志应用(拷贝操作就略啦)
/var/mysql/bin/mysqlbinlog -uroot -pyourpassword -ddb_name mysql-bin.000631 mysql-bin.000632 mysql-bin.000633 | /var/mysql/bin/mysql -uroot -pyourpassword db_name &
-- 最后,
-- 先在目标库授权 (另一主节点,假设源库所在网段为:192.168.10,源库的IP地址为:192.168.10.111)
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.10.%' IDENTIFIED BY 'yourpassword';
change master to master_host='192.168.10.111',master_port=3306,master_user='repl',master_password='yourpassword',master_log_file='mysql-bin.000634',master_log_pos=1;
-- 上面语句,表示 从源库的mysql-bin.000634日志文件的起始位置(master_log_pos=1)开始同步应用日志。
-- 最后:此时可以关掉源库的所有Web应用,设置源库与目标库的日志同步
-- 最后:两台主库
start slave;
-- 查看状态:
show slave status\G
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
-- 一切正常,搭建成功!
-- 你甚至可以用主键表查询验证:
-- 在主库1执行以下查询:
mysql> select max(id) from t_Groupon_order;
+---------+
| max(id) |
+---------+
| 6407093 |
+---------+
1 row in set (0.00 sec)
mysql> select count(id) from t_Groupon_order where id<=6407093;
+-----------+
| count(id) |
+-----------+
| 6253317 |
+-----------+
1 row in set (47.08 sec)
-- 在主库2执行以下查询:
mysql> select count(id) from t_Groupon_order where id<=6407093;
+-----------+
| count(id) |
+-----------+
| 6253317 |
+-----------+
1 row in set (1 min 10.26 sec)
-- 两个主库上执行 select count(id) from t_Groupon_order where id<=6407093; 结果完全一致(一张业务单也不少),
-- 足够说明同步成功!
This causes the binary log position and filename to be
appended to the output. If equal to 1, will print it as a
CHANGE MASTER command; if equal to 2, that command will
be prefixed with a comment symbol. This option will turn
--lock-all-tables on, unless --single-transaction is
specified too (in which case a global read lock is only
taken a short time at the beginning of the dump; don't
forget to read about --single-transaction below). In all
cases, any action on logs will happen at the exact moment
of the dump. Option automatically turns --lock-tables
off.
-- 现在我能够做到的是
-- 任何生产库,要搭建集群,我只要不超过半个小时的down机时间
-- 当然:这指的是 主----主 集群
-- 主----从 集群,我不需要down 机