楼主: lixunpeng

【讨论】用从库的从库实现短时间迁移mysql的可行性

[复制链接]
论坛徽章:
71
ITPUB元老
日期:2009-11-30 15:55:11授权会员
日期:2009-11-30 11:36:17ITPUB季度 技术新星
日期:2010-08-31 10:47:25优秀写手
日期:2014-12-24 06:00:14ITPUB8周年纪念徽章
日期:2009-09-27 10:21:20祖国60周年纪念徽章
日期:2009-10-09 08:28:00奔驰
日期:2013-10-20 13:32:09数据库板块每日发贴之星
日期:2008-10-03 01:02:14数据库板块每日发贴之星
日期:2009-11-23 01:01:03数据库板块每日发贴之星
日期:2010-07-27 01:01:02
11#
 楼主| 发表于 2014-12-26 13:33 | 只看该作者
dian010418 发表于 2014-12-26 12:53
楼主为何要建成从库的从库?先在从库上备份,然后用这个备份建一个新的从库就行了,切换的时候就是一个一主 ...

从库上哪种方式备份?file和pos用谁的?

使用道具 举报

回复
论坛徽章:
2
2011新春纪念徽章
日期:2011-01-04 10:34:48ITPUB十周年纪念徽章
日期:2011-11-01 16:24:04
12#
发表于 2014-12-26 13:50 | 只看该作者
lixunpeng 发表于 2014-12-26 13:33
从库上哪种方式备份?file和pos用谁的?

mysqldump就可以,加上--dump-slave参数,备份里就会包含主库binlog的FILE和POSITION。

使用道具 举报

回复
论坛徽章:
71
ITPUB元老
日期:2009-11-30 15:55:11授权会员
日期:2009-11-30 11:36:17ITPUB季度 技术新星
日期:2010-08-31 10:47:25优秀写手
日期:2014-12-24 06:00:14ITPUB8周年纪念徽章
日期:2009-09-27 10:21:20祖国60周年纪念徽章
日期:2009-10-09 08:28:00奔驰
日期:2013-10-20 13:32:09数据库板块每日发贴之星
日期:2008-10-03 01:02:14数据库板块每日发贴之星
日期:2009-11-23 01:01:03数据库板块每日发贴之星
日期:2010-07-27 01:01:02
13#
 楼主| 发表于 2014-12-26 21:33 | 只看该作者
dian010418 发表于 2014-12-26 13:50
mysqldump就可以,加上--dump-slave参数,备份里就会包含主库binlog的FILE和POSITION。

太好了,多谢!
你真是高手啊!

使用道具 举报

回复
论坛徽章:
43
ITPUB9周年纪念徽章
日期:2012-09-28 16:17:24马上有钱
日期:2014-06-16 17:13:52马上有对象
日期:2014-06-16 17:13:52马上加薪
日期:2014-06-16 17:13:52现任管理团队成员
日期:2014-06-17 02:21:03版主1段
日期:2014-06-17 02:21:04马上有车
日期:2014-10-24 22:35:032010数据库技术大会纪念徽章
日期:2015-04-23 10:33:192011数据库大会纪念章
日期:2015-04-23 10:33:192012数据库大会纪念章
日期:2015-04-23 10:33:19
14#
发表于 2014-12-28 16:30 | 只看该作者
把现有的备份备库停了,迁移到新机子,然后把主库切换过去,不就好了吗?干嘛那么费事

使用道具 举报

回复
论坛徽章:
71
ITPUB元老
日期:2009-11-30 15:55:11授权会员
日期:2009-11-30 11:36:17ITPUB季度 技术新星
日期:2010-08-31 10:47:25优秀写手
日期:2014-12-24 06:00:14ITPUB8周年纪念徽章
日期:2009-09-27 10:21:20祖国60周年纪念徽章
日期:2009-10-09 08:28:00奔驰
日期:2013-10-20 13:32:09数据库板块每日发贴之星
日期:2008-10-03 01:02:14数据库板块每日发贴之星
日期:2009-11-23 01:01:03数据库板块每日发贴之星
日期:2010-07-27 01:01:02
15#
 楼主| 发表于 2015-1-5 15:09 | 只看该作者
幕南风 发表于 2014-12-28 16:30
把现有的备份备库停了,迁移到新机子,然后把主库切换过去,不就好了吗?干嘛那么费事

你是说停备库直接cp从库的file到新机器吧?可是我觉得这样文件里面的结构比较乱,尤其是dml比较多的情况,有可能行迁移、chaned row什么的比较多。
如果用dump,新的文件结构就比较整齐。

使用道具 举报

回复
论坛徽章:
71
ITPUB元老
日期:2009-11-30 15:55:11授权会员
日期:2009-11-30 11:36:17ITPUB季度 技术新星
日期:2010-08-31 10:47:25优秀写手
日期:2014-12-24 06:00:14ITPUB8周年纪念徽章
日期:2009-09-27 10:21:20祖国60周年纪念徽章
日期:2009-10-09 08:28:00奔驰
日期:2013-10-20 13:32:09数据库板块每日发贴之星
日期:2008-10-03 01:02:14数据库板块每日发贴之星
日期:2009-11-23 01:01:03数据库板块每日发贴之星
日期:2010-07-27 01:01:02
16#
 楼主| 发表于 2015-1-5 15:14 | 只看该作者
dian010418 发表于 2014-12-26 13:50
mysqldump就可以,加上--dump-slave参数,备份里就会包含主库binlog的FILE和POSITION。

感觉基本就是在主库上执行了,不用第三方的迁移工具了,这样只在主库锁一会所有的表,备份期间不能有ddl操作:

mysqldump -A  --single-transaction --dump-slave --include-master-host-port --master-data > lxp.sql

使用道具 举报

回复
论坛徽章:
71
ITPUB元老
日期:2009-11-30 15:55:11授权会员
日期:2009-11-30 11:36:17ITPUB季度 技术新星
日期:2010-08-31 10:47:25优秀写手
日期:2014-12-24 06:00:14ITPUB8周年纪念徽章
日期:2009-09-27 10:21:20祖国60周年纪念徽章
日期:2009-10-09 08:28:00奔驰
日期:2013-10-20 13:32:09数据库板块每日发贴之星
日期:2008-10-03 01:02:14数据库板块每日发贴之星
日期:2009-11-23 01:01:03数据库板块每日发贴之星
日期:2010-07-27 01:01:02
17#
 楼主| 发表于 2015-1-5 15:17 | 只看该作者
华夏小卒 发表于 2014-12-25 17:56
一直用的innobackupex 。居然mysqldump 也有这参数。有和弊端否?

只在主库锁一会所有的表,备份期间不能有ddl操作。

仔细读了一下mysqldump的--help,很有收获,原来众里寻他千百度,那人却近在咫尺

I need to use:

mysqldump -A  --single-transaction --dump-slave --include-master-host-port --master-data > lxp.sql


-----------------------------------------------------------
mysqldump --help:

  --dump-slave[=#]    This causes the binary log position and filename of the
                      master to be appended to the dumped data output. Setting
                      the value to 1, will printit as a CHANGE MASTER command
                      in the dumped data output; 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.
                     
                     
  --include-master-host-port
                      Adds 'MASTER_HOST=<host>, MASTER_PORT=<port>' to 'CHANGE
                      MASTER TO..' in dump produced with --dump-slave.
                     
                     
  --master-data[=#]   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.

  
    --single-transaction
                      Creates a consistent snapshot by dumping all tables in a
                      single transaction. Works ONLY for tables stored in
                      storage engines which support multiversioning (currently
                      only InnoDB does); the dump is NOT guaranteed to be
                      consistent for other storage engines. While a
                      --single-transaction dump is in process, to ensure a
                      valid dump file (correct table contents and binary log
                      position), no other connection should use the following
                      statements: ALTER TABLE, DROP TABLE, RENAME TABLE,
                      TRUNCATE TABLE, as consistent snapshot is not isolated
                      from them. Option automatically turns off --lock-tables.

使用道具 举报

回复
论坛徽章:
71
ITPUB元老
日期:2009-11-30 15:55:11授权会员
日期:2009-11-30 11:36:17ITPUB季度 技术新星
日期:2010-08-31 10:47:25优秀写手
日期:2014-12-24 06:00:14ITPUB8周年纪念徽章
日期:2009-09-27 10:21:20祖国60周年纪念徽章
日期:2009-10-09 08:28:00奔驰
日期:2013-10-20 13:32:09数据库板块每日发贴之星
日期:2008-10-03 01:02:14数据库板块每日发贴之星
日期:2009-11-23 01:01:03数据库板块每日发贴之星
日期:2010-07-27 01:01:02
18#
 楼主| 发表于 2015-1-5 15:46 | 只看该作者
本帖最后由 lixunpeng 于 2015-1-5 15:51 编辑
myownstars 发表于 2014-12-25 17:45
mysql --dump-slave记录change master to ,可以在slave导出备份

我在slave试验了,竟然也是找到了主库信息,很不错:
mysqldump -p  --single-transaction --dump-slave --include-master-host-port --master-data lxptest > /root/testlxp.sql

导出文件如下:

# more testlxp.sql
-- MySQL dump 10.13  Distrib 5.6.22, for Linux (x86_64)
--
-- Host: localhost    Database: lxptest
-- ------------------------------------------------------
-- Server version       5.6.22-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Position to start replication or point-in-time recovery from (the master of this slave)
--

CHANGE MASTER TO MASTER_HOST='172.16.0.25', MASTER_PORT=3306, MASTER_LOG_FILE='mysqlmaster-bin.000085', MASTER_LOG_POS=462766030;

--
-- Table structure for table `testlxp`
--

DROP TABLE IF EXISTS `testlxp`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `testlxp` (
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `testlxp`
--

LOCK TABLES `testlxp` WRITE;
/*!40000 ALTER TABLE `testlxp` DISABLE KEYS */;
INSERT INTO `testlxp` VALUES (1),(2);
/*!40000 ALTER TABLE `testlxp` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2015-01-05 15:36:55

使用道具 举报

回复
论坛徽章:
71
ITPUB元老
日期:2009-11-30 15:55:11授权会员
日期:2009-11-30 11:36:17ITPUB季度 技术新星
日期:2010-08-31 10:47:25优秀写手
日期:2014-12-24 06:00:14ITPUB8周年纪念徽章
日期:2009-09-27 10:21:20祖国60周年纪念徽章
日期:2009-10-09 08:28:00奔驰
日期:2013-10-20 13:32:09数据库板块每日发贴之星
日期:2008-10-03 01:02:14数据库板块每日发贴之星
日期:2009-11-23 01:01:03数据库板块每日发贴之星
日期:2010-07-27 01:01:02
19#
 楼主| 发表于 2015-1-5 16:03 | 只看该作者
又在主库上试验了,发现不能在主库上用--dump-slave 参数:

mysqldump -p  --single-transaction --dump-slave --include-master-host-port --master-data lxptest > /root/testlxp.sql

mysqldump: Couldn't execute 'START SLAVE': The server is not configured as slave; fix in config file or with CHANGE MASTER TO (1200)

使用道具 举报

回复
论坛徽章:
71
ITPUB元老
日期:2009-11-30 15:55:11授权会员
日期:2009-11-30 11:36:17ITPUB季度 技术新星
日期:2010-08-31 10:47:25优秀写手
日期:2014-12-24 06:00:14ITPUB8周年纪念徽章
日期:2009-09-27 10:21:20祖国60周年纪念徽章
日期:2009-10-09 08:28:00奔驰
日期:2013-10-20 13:32:09数据库板块每日发贴之星
日期:2008-10-03 01:02:14数据库板块每日发贴之星
日期:2009-11-23 01:01:03数据库板块每日发贴之星
日期:2010-07-27 01:01:02
20#
 楼主| 发表于 2015-1-5 16:47 | 只看该作者
原来从库上的这个备份要停掉从库,备份完再启动。主库上虽然报错Couldn't execute 'START SLAVE',但是表是备份下来了。

more testlxp.sql
-- MySQL dump 10.13  Distrib 5.6.22, for Linux (x86_64)
--
-- Host: localhost    Database: lxptest
-- ------------------------------------------------------
-- Server version       5.6.22-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `testlxp`
--

DROP TABLE IF EXISTS `testlxp`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `testlxp` (
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `testlxp`
--

LOCK TABLES `testlxp` WRITE;
/*!40000 ALTER TABLE `testlxp` DISABLE KEYS */;
INSERT INTO `testlxp` VALUES (1),(2);
/*!40000 ALTER TABLE `testlxp` ENABLE KEYS */;
UNLOCK TABLES;

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表