使用mysqldiff對多個MySQL數據庫進行比較


提示:本文內容是在 MySQL5.7.32 和 MySQL8.0.23 中使用 mysqldiff ,前面是使用及遇到的一些問題,后面是安裝及遇到的一些問題;

           MySQL8.0.23遇到的一些問題可以配合MySQL5.7.32版本進行調整並最終成功執行,8.0及以上版本的處理辦法放在使用方法這塊的結尾,安裝和執行命令是相同的,不同的只有報錯,根據提示,相應得到處理即可。

 

——使用:

當你已經安裝成功之后(安裝在后面),直接在命令行輸入 mysqldiff 之后執行,會獲得一個警告,而警告中就告訴你如何去使用這個工具了:

[root@MySQL-120 ~]# mysqldiff
# WARNING: Using a password on the command line interface can be insecure.
Usage: mysqldiff --server1=user:pass@host:port:socket --server2=user:pass@host:port:socket db1.object1:db2.object1 db3:db4

mysqldiff: error: No objects specified to compare.

 

完整的語法是:mysqldiff --server1=user:pass@host:port:socket --server2=user:pass@host:port:socket db1.object1:db2.object1 db3:db4

非常簡單易懂,比如我自己將我本機的MySQL下的兩個庫進行比較,看看之間有什么不同:

[root@MySQL-120 ~]# mysqldiff --server1=root:123456@localhost --server2=root:123456@localhost test1:test2
# WARNING: Using a password on the command line interface can be insecure.
# server1 on localhost: ... connected.
# server2 on localhost: ... connected.
# WARNING: Objects in server1.test1 but not in server1.test2:
#        TABLE: t2
# WARNING: Objects in server1.test2 but not in server1.test1:
#        TABLE: t3
#        TABLE: t4
# Compare failed. One or more differences found.

信息顯示了對比的兩個庫中的表有哪些差異,分別列出test1與test2之間相比較出現的差異表,當然有視圖的話還會顯示視圖;

最后一句提示對比失敗,是因為庫中的表有差異,表示失敗,並不是本身執行失敗,若對比未出現差異,則是對比成功,顯示 # Success. All objects are the same.

 

若要對比表的結構,使用database.table的形式對比:

注意:mysqldiff 會對庫中的表是否存在以及表的結構進行對比,但不會去對比表中的數據,若表名表結構相同但數據有差異,依然會顯示對比成功,表的結構發生差異,會顯示出具體差異情況。

[root@MySQL-120 ~]# mysqldiff --server1=root:123456@localhost --server2=root:123456@localhost test1.t1:test2.t1
# WARNING: Using a password on the command line interface can be insecure.
# server1 on localhost: ... connected.
# server2 on localhost: ... connected.
# Comparing test1.t1 to test2.t1                                   [FAIL]
# Object definitions differ. (--changes-for=server1)
#

--- test1.t1
+++ test2.t1
@@ -1,4 +1,3 @@
 CREATE TABLE `t1` (
-  `id` int(11) DEFAULT NULL,
-  `name` varchar(20) DEFAULT NULL
+  `id` int(11) DEFAULT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1
# Compare failed. One or more differences found.

 

 

關於MySQL8.0及以上版本執行命令時出現的各種報錯:

1.遇到Authentication plugin ‘caching_sha2_password’ cannot be loaded:

root@localhost : mysql 11:31:01> alter user root@'localhost' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.00 sec)
root@localhost : mysql 11:31:48> flush privileges;
Query OK, 0 rows affected (0.00 sec)

典型的MySQL5.x與8.0版本的差異錯誤,各種場景下遇到太多次了,密碼的問題,直接修改密碼的加密模式即可。

 

2.報錯 mysql 中的 proc 和 event 表不存在:

兩個表並不會一起提示,先會提示你 proc 不存在,之后再提示你 event 表不存在,一起解決就行了,方法就是從MySQL5.x的版本的數據庫導出,之后導入8.0及以上的庫

報錯信息:ERROR: Query failed. 1146 (42S02): Table 'mysql.proc' doesn't exist

                  ERROR: Query failed. 1146 (42S02): Table 'mysql.event' doesn't exist

 ##從MySQL5.x版本的數據庫進行導出,然后scp傳給8.0的庫
[root@localhost ~]# mysqldump -u root -p mysql event proc > event_proc.sql
Enter password: 
[root@localhost ~]# scp event_proc.sql 192.168.1.120:/root/event_proc.sql.sql

  ##在8.0的庫進行導入
[root@MySQL-120 ~]# mysql -u root -p mysql < event_proc.sql.sql 
Enter password: Warning (Code 3719): 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. ......
  ##導入期間會警告提示,此為警告,是因為版本發生變化,UTF8的別名也發生了變化,不過數據會正常執行導入並成功完成

  ##之后我們再執行則可以正常的進行比對了
[root@MySQL-120 ~]# mysqldiff --server1=root:123456@localhost --server2=root:123456@localhost test01:test02
# WARNING: Using a password on the command line interface can be insecure.
# server1 on localhost: ... connected.
# server2 on localhost: ... connected.
# WARNING: Objects in server1.test01 but not in server1.test02:
#        TABLE: t01
#        TABLE: t02
# Compare failed. One or more differences found.

此問題是由於MySQL8.0及以上的版本在mysql的庫中沒有 proc 和 event 這兩個表,執行 mysqldiff 又需要它們,至於為什么個人沒有去深入研究,反正解決了,哈哈!

 

 


 

——安裝:

在官網下載 MySQL Utilities 工具軟件本體( https://downloads.mysql.com/archives/utilities/ )

同時還需要在官網下載必要的依賴軟件( https://downloads.mysql.com/archives/c-python/ )

這是目前我下載的版本,有特殊需求,可以選擇其他版本:

[root@MySQL-120 ~]# ll mysql-*
-rw-r--r-- 1 root root    247024 Mar 20 13:06 mysql-connector-python-2.1.7-1.el7.x86_64.rpm
-rw-r--r-- 1 root root    856440 Mar 20 12:21 mysql-utilities-1.6.5-1.el7.noarch.rpm

 

注意:這里我是家里測試的,我個人的MySQL是8.0.23的版本,而最開始使用的是MySQL5.7.32的版本,我個人的8.0.23版本出現了異常報錯;

           所以將軟件下載的版本降下來,不要下 mysql-connector-python-8.0.23 , 而是選擇2.1.7 ;

[root@MySQL-120 ~]# mysqldiff
Traceback (most recent call last): File "/usr/bin/mysqldiff", line 28, in <module>
    from mysql.utilities.common.tools import check_python_version
ImportError: No module named utilities.common.tools

 

之后將兩個rpm包進行安裝

[root@MySQL-120 ~]# rpm -ivh mysql-connector-python-2.1.7-1.el7.x86_64.rpm 
warning: mysql-connector-python-2.1.7-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-connector-python-2.1.7-1.el################################# [100%]

[root@MySQL-120 ~]# rpm -ivh mysql-utilities-1.6.5-1.el7.noarch.rpm 
warning: mysql-utilities-1.6.5-1.el7.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-utilities-1.6.5-1.el7      ################################# [100%]

 

 

安裝時出現的各種報錯:

1.當你直接安裝 mysql-utilities 的時候報錯:

[root@MySQL-120 ~]# rpm -ivh mysql-utilities-1.6.5-1.el7.noarch.rpm 
warning: mysql-utilities-1.6.5-1.el7.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.el7.noarch

注意:提示中還給顯示了版本要 >= 2.0.0 。

 

2.當你安裝 mysql-connector-python 的時候報錯:

[root@MySQL-120 ~]# rpm -ivh mysql-connector-python-2.1.7-1.el7.x86_64.rpm 
warning: mysql-connector-python-2.1.7-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
    python-setuptools is needed by mysql-connector-python-2.1.7-1.el7.x86_64

注意:提示你需要一個叫做 python-setuptools 的軟件包,有外網的情況下可以直接 yum install -y python-setuptools 進行安裝。

 

3.還是當你安裝 mysql-connector-python 的時候報錯:

[root@MySQL-120 ~]# rpm -ivh mysql-connector-python-2.1.7-1.el7.x86_64.rpm 
warning: mysql-connector-python-2.1.7-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
    net-tools is needed by mysql-connector-python-2.1.7-1.el7.x86_64

注意:根據這個情況可以看出,你缺少了什么包,都會在報錯中為你提示,比較好處理,直接找對應的進行下載安裝就可以了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM