Synchronize two databases schema in MySQL

前端 未结 6 1742
灰色年华
灰色年华 2021-02-02 15:12

I was looking for a portable script or command line program that can synchronize two MySQL databases schema. I am not looking for a GUI based solution because that can\'t be aut

6条回答
  •  时光说笑
    2021-02-02 15:58

    I know it's an old question but it was the first result on google for what I was searching for (exact same thing as the initial question)

    I found the answer still here but I don't remember the URL it's a script that started from:

    mysqldump --skip-comments --skip-extended-insert -u root -p dbName1>file1.sql
    mysqldump --skip-comments --skip-extended-insert -u root -p dbName2>file2.sql
    diff file1.sql file2.sql
    

    and ended up more like this

    #!/bin/sh
    
    echo "Usage: dbdiff [user1:pass1@dbname1] [user2:pass2@dbname2] [ignore_table1:ignore_table2...]"
    
    dump () {
      up=${1%%@*}; user=${up%%:*}; pass=${up##*:}; dbname=${1##*@};
      mysqldump --opt --compact --skip-extended-insert -u $user -p$pass $dbname $table > $2
    }
    
    rm -f /tmp/db.diff
    
    # Compare
    up=${1%%@*}; user=${up%%:*}; pass=${up##*:}; dbname=${1##*@};
    for table in `mysql -u $user -p$pass $dbname -N -e "show tables" --batch`; do
      if [ "`echo $3 | grep $table`" = "" ]; then
        echo "Comparing '$table'..."
        dump $1 /tmp/file1.sql
        dump $2 /tmp/file2.sql
        diff -up /tmp/file1.sql /tmp/file2.sql >> /tmp/db.diff
      else
        echo "Ignored '$table'..."
      fi
    done
    less /tmp/db.diff
    rm -f /tmp/file1.sql /tmp/file2.sql
    

提交回复
热议问题