手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL迁移工具在生产环境下的使用

[日期:2013-12-18] 来源:Linux社区  作者:birdinroom [字体:大 中 小]

执行php migrate.php list 返回结果:
WARNING: Migration numbers may not be in order due to interleaving.


    #        Timestamp
  ========================================================================
    version          createtime              active  current note
    1371546856        2013-06-18 17:14:16    1      0      v1
    1380427212        2013-09-29 12:00:12    1      1      v1
    1387349445        2013-12-18 14:50:45    0      0      test
  Page 1 of 1, 3 migrations in all.

 


cd config 目录
cat db_config.php
<?php
$db_config = (object) array();

$db_config->host = '127.0.0.1';
$db_config->port = '3306';
$db_config->user = 'dbuser';
$db_config->pass = 'dbpasswd';
$db_config->name = 'dbname';
$db_config->db_path = 'var/www/MySQLMigrations/dbscript/';
$db_config->method = 2;  //1 pdo,2 mysqli
?>

 

了解该程序基本结构后,我们来开始使用一下它:
cd mysqlMigrations
php migrate.php add test2 

在dbscript下生成 2013_12_18_15_06_14_test2.php


执行命令:php migrate.php list
可以看到版本结果:

#        Timestamp
  ========================================================================
    version          createtime              active  current note
    1371546856        2013-06-18 17:14:16    1      0      v1
    1380427212        2013-09-29 12:00:12    1      1      v1
    1387349445        2013-12-18 14:50:45    0      0      test
    1387350374        2013-12-18 15:06:14    0      0      test2


  Page 1 of 1, 4 migrations in all.


说明:
  version 每次迁移的版本号

  createtime 创建时间
  active  是否已经激活生效
  current 数据库当前所在版本标志
  note  迁移的注释

 

执行命令:php migrate.php up 1387349445  可以把数据版本从 1380427212 迁移到 1387349445
#        Timestamp
  ========================================================================
    version          createtime              active  current note
    1371546856        2013-06-18 17:14:16    1      0      v1
    1380427212        2013-09-29 12:00:12    1      0      v1
1387349445        2013-12-18 14:50:45    1      1      test
    1387350374        2013-12-18 15:06:14    0      0      test2


  Page 1 of 1, 4 migrations in all.

 


执行命令:php migrate.php down 1380427212        可以把数据版本从 1387349445 回滚到 1380427212
执行php migrate.php list查看数据库版本回滚结果
 #        Timestamp
  ========================================================================
    version          createtime              active  current note
    1371546856        2013-06-18 17:14:16    1      0      v1
    1380427212        2013-09-29 12:00:12    1      1      v1
    1387349445        2013-12-18 14:50:45    0      0      test
    1387350374        2013-12-18 15:06:14    0      0      test2


  Page 1 of 1, 4 migrations in all.


如果要迁移到数据库最大版本可以执行一下命令:
php migrate.php up max_version
返回结果:
Migrating to 2013-12-18 15:06:14 (ID 1387350374)...
      Performing UP migration 2013-12-18 14:50:45 (ID 1387349445)... done.
      Performing UP migration 2013-12-18 15:06:14 (ID 1387350374)... done.


*******************************************************************************
查看php migrate.php list
#        Timestamp
  ========================================================================
    version          createtime              active  current note
    1371546856        2013-06-18 17:14:16    1      0      v1
    1380427212        2013-09-29 12:00:12    1      0      v1
    1387349445        2013-12-18 14:50:45    1      0      test
1387350374        2013-12-18 15:06:14    1      1      test2


  Page 1 of 1, 4 migrations in all.


执行回滚:
php migrate.php down 1380427212       
返回一下结果:
Migrating to 2013-09-29 12:00:12 (ID 1380427212)...
      Performing DOWN migration 2013-12-18 15:06:14 (ID 1387350374)... done.
      Performing DOWN migration 2013-12-18 14:50:45 (ID 1387349445)... done.


  #        Timestamp
  ========================================================================
    version          createtime              active  current note
    1371546856        2013-06-18 17:14:16    1      0      v1
    1380427212        2013-09-29 12:00:12    1      1      v1
    1387349445        2013-12-18 14:50:45    0      0      test
    1387350374        2013-12-18 15:06:14    0      0      test2


  Page 1 of 1, 4 migrations in all.


通过执行一下操作,查看数据库中数据变化

linux
相关资讯       MySQL迁移 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款