使⽤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 st1 but not st2:
#        TABLE: t2
# WARNING: Objects st2 but not st1:
#        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 st01 but not st02:
#        TABLE: t01
安装mysql失败#        TABLE: t02
# Compare failed. One or more differences found.
此问题是由于MySQL8.0及以上的版本在mysql的库中没有 proc 和 event 这两个表,执⾏ mysqldiff ⼜需要它们,⾄于为什么个⼈没有去深⼊研究,反正解决了,哈哈!
——安装:
这是⽬前我下载的版本,有特殊需求,可以选择其他版本:
[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.arch.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 ls import check_python_version
ImportError: No module ls
之后将两个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
<                          >>>>>>### [100%]
Updating /
1:mysql-connector-python-2.1.7-1.el>>>>>>### [100%]
[root@MySQL-120 ~]# rpm -ivh mysql-utilities-1.6.arch.rpm
warning: mysql-utilities-1.6.arch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
<                          >>>>>>### [100%]
Updating /
1:mysql-utilities-1.6.5-1.el7      >>>>>>### [100%]
安装时出现的各种报错:
1.当你直接安装 mysql-utilities 的时候报错:
[root@MySQL-120 ~]# rpm -ivh mysql-utilities-1.6.arch.rpm
warning: mysql-utilities-1.6.arch.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.arch
注意:提⽰中还给显⽰了版本要 >= 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
注意:根据这个情况可以看出,你缺少了什么包,都会在报错中为你提⽰,⽐较好处理,直接对应的进⾏下载安装就可以了。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论