zoukankan      html  css  js  c++  java
  • MySQL-Utilities:mysqldiff

    园子看到使用MySQL对比数据库表结构,参考测试发现

    mysql> use test;
    create table test1
    (id int not null primary key,
    a varchar(10) not null,
    b varchar(10),
    c varchar(10) comment 'c',
    d int
    )
    ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='test1';
    create table test2
    (id int not null ,
    a varchar(10),
    b varchar(5),
    c varchar(10),
    D int
    )
    ENGINE=myisam DEFAULT CHARSET=utf8 COMMENT='test2';
    View Code
    [root@localhost uest]# mysqldiff --server1=root:mysql5635@localhost:3306 --server2=root:mysql5635@localhost:3306 --changes-for=server2 --show-reverse --difftype=sql test.test1:test.test2
    bash: mysqldiff: command not found
    [root@localhost uest]# 

    搜索才知道mysqldiff工具是官方MySQL-Utilities工具集的一个脚本。mysqldiff用来比较对象的定义是否相同并显示不同的地方,mysqldiff 是通过对象名称来进行比较的。
    系统环境紧接上一篇,因此需下载相应的rpm包

    [root@localhost tools]# wget https://cdn.mysql.com//Downloads/MySQLGUITools/mysql-utilities-1.6.5-1.el6.noarch.rpm
    [root@localhost tools]# rpm -ivh mysql-utilities-1.6.5-1.el6.noarch.rpm
    warning: mysql-utilities-1.6.5-1.el6.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    error: Failed dependencies:
        mysql-connector-python >= 2.0.0 is needed by mysql-utilities-1.6.5-1.el6.noarch
    [root@localhost tools]#

    需要依赖文件

    [root@localhost tools]# wget https://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.5-1.el6.i686.rpm
    [root@localhost tools]# rpm -ivh mysql-connector-python-2.1.5-1.el6.i686.rpm
    warning: mysql-connector-python-2.1.5-1.el6.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    Preparing...                ########################################### [100%]
       1:mysql-connector-python ########################################### [100%]
    [root@localhost tools]# rpm -ivh mysql-utilities-1.6.5-1.el6.noarch.rpm
    warning: mysql-utilities-1.6.5-1.el6.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    Preparing...                ########################################### [100%]
       1:mysql-utilities        ########################################### [100%]
    [root@localhost tools]#

    然后就可以执行mysqldiff命令了

    [root@localhost tools]# mysqldiff --server1=root:mysql5635@localhost:3306 --server2=root:mysql5635@localhost:3306 --changes-for=server2 --skip-table-options --show-reverse --difftype=sql test.test1:test.test2
    # WARNING: Using a password on the command line interface can be insecure.
    # server1 on localhost: ... connected.
    # server2 on localhost: ... connected.
    # Comparing test.test1 to test.test2                               [FAIL]
    # Transformation for --changes-for=server2:
    #
    
    ALTER TABLE `test`.`test2` 
      DROP COLUMN D, 
      ADD PRIMARY KEY(`id`), 
      CHANGE COLUMN b b varchar(10) NULL, 
      ADD COLUMN d int(11) NULL AFTER c, 
      CHANGE COLUMN a a varchar(10) NOT NULL, 
      CHANGE COLUMN c c varchar(10) NULL COMMENT 'c';
    
    #
    # Transformation for reverse changes (--changes-for=server1):
    #
    # ALTER TABLE `test`.`test1` 
    #   DROP PRIMARY KEY, 
    #   DROP COLUMN d, 
    #   CHANGE COLUMN b b varchar(5) NULL, 
    #   ADD COLUMN D int(11) NULL AFTER c, 
    #   CHANGE COLUMN a a varchar(10) NULL, 
    #   CHANGE COLUMN c c varchar(10) NULL;
    #
    
    # Compare failed. One or more differences found.
    [root@localhost tools]# 
    View Code

    MySQL Utilities 提供一组命令行工具用于维护和管理 MySQL 服务器,可参考官方手册,或者查阅MySQL Utilities教程

  • 相关阅读:
    virtualbox 设置鼠标在虚拟机和电脑之间切换
    7-nginx 配置记录 http 请求参数(如记录URL参数)的 log 和 nginx 常见的一些内置变量
    BufferedWriter 没有比FileWriter 快多少
    锁、线程锁、锁旗标、锁对象
    all 2 branches missed
    使用xmp path进行行变列的SQL语句
    程序员的健身课
    zookeeper启动报错:Error: Could not find or load main class org.apache.zookeeper.server.quorum.QuorumPeerMain
    clickhouse数仓:mysql数据到clickhouse的离线、实时与全量、增量的方案调研
    sql语句分为三类(DML,DDL,DCL)
  • 原文地址:https://www.cnblogs.com/ShanFish/p/6526429.html
Copyright © 2011-2022 走看看