Compare structures of two databases?

前端 未结 17 1531
我在风中等你
我在风中等你 2021-02-01 04:30

I wanted to ask whether it is possible to compare the complete database structure of two huge databases. We have two databases, the one is a development database, the other a p

相关标签:
17条回答
  • 2021-02-01 04:51

    For mysql on Linux, it is possible via phpmyadmin to export the databases without 'data' and only structure.

    Scrolling through the export options for the entire database, just deselect 'data' and set the output to text. Export both databases you wish to compare.

    Then in file compare in your preferred program / site, compare the two text file outputs of the databases. Synchronization is still manual in this solution, but this is effective for comparing and finding the structural differences.

    0 讨论(0)
  • 2021-02-01 04:54

    To answer this kind of question currently, I've made a script that uses information_schema content to compare column, datatype, and table

    SET @database_current = '<production>';
    SET @database_dev = '<development>';
    -- column and datatype comparison
    SELECT a.TABLE_NAME, a.COLUMN_NAME, a.DATA_TYPE, a.CHARACTER_MAXIMUM_LENGTH,
        b.COLUMN_NAME, b.DATA_TYPE, b.CHARACTER_MAXIMUM_LENGTH
    FROM information_schema.COLUMNS a
        LEFT JOIN information_schema.COLUMNS b ON b.COLUMN_NAME = a.COLUMN_NAME
            AND b.TABLE_NAME = a.TABLE_NAME
            AND b.TABLE_SCHEMA = @database_current
    WHERE a.TABLE_SCHEMA = @database_dev
        AND (
            b.COLUMN_NAME IS NULL
            OR b.COLUMN_NAME != a.COLUMN_NAME
            OR b.DATA_TYPE != a.DATA_TYPE
            OR b.CHARACTER_MAXIMUM_LENGTH != a.CHARACTER_MAXIMUM_LENGTH
        );
    -- table comparison    
    SELECT a.TABLE_SCHEMA, a.TABLE_NAME, b.TABLE_NAME
    FROM information_schema.TABLES a
        LEFT JOIN information_schema.TABLES b ON b.TABLE_NAME = a.TABLE_NAME
            AND b.TABLE_SCHEMA = @database_current
    WHERE a.TABLE_SCHEMA = @database_dev
        AND (
            b.TABLE_NAME IS NULL
            OR b.TABLE_NAME != a.TABLE_NAME
        );
    

    Hope this script can also help people that looks for a non-application solution, but the usage of script. Cheers

    0 讨论(0)
  • 2021-02-01 04:54

    You can just dump them with --no-data and compare the files.

    Remember to use the --lock-tables=0 option on your production database to avoid the big nasty global lock.

    If you use the same mysqldump version (your dev and production systems should have the same software, right?) then you'll expect to get more-or-less identical files out. The tables will be in alpha order so a simple diff will show discrepancies up easily.

    0 讨论(0)
  • 2021-02-01 04:55

    You'll need a tool, probably, like Database comparer.

    0 讨论(0)
  • 2021-02-01 04:58

    You can use the command line:

    mysqldump --skip-comments --skip-extended-insert -d --no-data -u root -p dbName1>file1.sql
    mysqldump --skip-comments --skip-extended-insert -d --no-data -u root -p dbName2>file2.sql
    diff file1.sql file2.sql
    
    0 讨论(0)
提交回复
热议问题