Skip to content

Commit 7e31b40

Browse files
authored
support mysql 8.4 (#5231)
1 parent b2b3223 commit 7e31b40

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

driver/src/main/java/com/alibaba/otter/canal/parse/driver/mysql/MysqlConnector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private void negotiate(SocketChannel channel) throws IOException {
293293
encryptedPassword = getPassword().getBytes();
294294
header = authSwitchAfterAuth(encryptedPassword, header);
295295
body = PacketManager.readBytes(channel, header.getPacketBodyLength(), timeout);
296-
} else if ("mysql_native_password".equals(pluginName)) {
296+
} else if (pluginName == null || "mysql_native_password".equals(pluginName)) {
297297
try {
298298
encryptedPassword = MySQLPasswordEncrypter.scramble411(getPassword().getBytes(), authData);
299299
} catch (NoSuchAlgorithmException e) {

parse/src/main/java/com/alibaba/otter/canal/parse/inbound/mysql/MysqlConnection.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ public long queryServerId() throws IOException {
432432
* <li>net_write_timeout</li>
433433
* <li>net_read_timeout</li>
434434
* </ol>
435-
*
435+
*
436436
* @throws IOException
437437
*/
438438
private void updateSettings() throws IOException {
@@ -560,7 +560,7 @@ private void loadBinlogImage() {
560560

561561
/**
562562
* 获取主库checksum信息
563-
*
563+
*
564564
* <pre>
565565
* mariadb区别于mysql会在binlog的第一个事件Rotate_Event里也会采用checksum逻辑,而mysql是在第二个binlog事件之后才感知是否需要处理checksum
566566
* 导致maraidb只要是开启checksum就会出现binlog文件名解析乱码
@@ -627,7 +627,7 @@ public boolean isMixed() {
627627

628628
private String value;
629629

630-
private BinlogFormat(String value){
630+
private BinlogFormat(String value) {
631631
this.value = value;
632632
}
633633

@@ -645,7 +645,7 @@ public static BinlogFormat valuesOf(String value) {
645645
/**
646646
* http://dev.mysql.com/doc/refman/5.6/en/replication-options-binary-log.
647647
* html#sysvar_binlog_row_image
648-
*
648+
*
649649
* @author agapple 2015年6月29日 下午10:39:03
650650
* @since 1.0.20
651651
*/
@@ -667,7 +667,7 @@ public boolean isNoBlob() {
667667

668668
private String value;
669669

670-
private BinlogImage(String value){
670+
private BinlogImage(String value) {
671671
this.value = value;
672672
}
673673

@@ -756,4 +756,23 @@ public boolean isMariaDB() {
756756
return connector.getServerVersion() != null && connector.getServerVersion().toLowerCase().contains("mariadb");
757757
}
758758

759+
// MySQL 8.4版本开始部分命令出现变化
760+
// https://dev.mysql.com/doc/relnotes/mysql/8.4/en/news-8-4-0.html#mysqld-8-4-0-deprecation-removal
761+
public boolean atLeast(int major, int minor) {
762+
if (isMariaDB()) {
763+
return false;
764+
}
765+
String version = connector.getServerVersion();
766+
if (StringUtils.isNotEmpty(version)) {
767+
String[] parts = version.split("\\.");
768+
int majorVer = Integer.parseInt(parts[0]);
769+
int minorVer = Integer.parseInt(parts[1]);
770+
return (majorVer > major) || (majorVer == major && minorVer >= minor);
771+
}
772+
return false;
773+
}
774+
775+
public boolean atLeastMySQL84() {
776+
return atLeast(8, 4);
777+
}
759778
}

parse/src/main/java/com/alibaba/otter/canal/parse/inbound/mysql/MysqlEventParser.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,13 +668,20 @@ private Long findServerId(MysqlConnection mysqlConnection) {
668668
* 查询当前的binlog位置
669669
*/
670670
private EntryPosition findEndPosition(MysqlConnection mysqlConnection) {
671+
String showSql = "SHOW MASTER STATUS";
671672
try {
672-
String showSql = multiStreamEnable ? "show master status with " + destination : "show master status";
673+
if (mysqlConnection.atLeastMySQL84()) {
674+
showSql = "SHOW BINARY LOG STATUS";
675+
}
676+
if (multiStreamEnable) {
677+
showSql = "show master status with " + destination;
678+
}
679+
673680
ResultSetPacket packet = mysqlConnection.query(showSql);
674681
List<String> fields = packet.getFieldValues();
675682
if (CollectionUtils.isEmpty(fields)) {
676683
throw new CanalParseException(
677-
"command : 'show master status' has an error! pls check. you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation");
684+
"command : '" + showSql +"' has an error! pls check. you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation");
678685
}
679686
EntryPosition endPosition = new EntryPosition(fields.get(0), Long.valueOf(fields.get(1)));
680687
if (isGTIDMode() && fields.size() > 4) {
@@ -690,7 +697,7 @@ private EntryPosition findEndPosition(MysqlConnection mysqlConnection) {
690697
}
691698
return endPosition;
692699
} catch (IOException e) {
693-
throw new CanalParseException("command : 'show master status' has an error!", e);
700+
throw new CanalParseException("command : '" + showSql +"' has an error! ", e);
694701
}
695702
}
696703

0 commit comments

Comments
 (0)