{"record":{"id":"7d2a8b070a9f9511","repo":"alibaba/canal","slug":"unknown-host","errorCode":null,"errorMessage":"Unknown host","messagePattern":"Unknown host","errorType":"exception","errorClass":"CanalParseException","httpStatus":null,"severity":"error","filePath":"parse/src/main/java/com/alibaba/otter/canal/parse/inbound/mysql/MysqlEventParser.java","lineNumber":336,"sourceCode":"\n    private final long generateUniqueServerId() {\n        try {\n            // a=`echo $masterip|cut -d\\. -f1`\n            // b=`echo $masterip|cut -d\\. -f2`\n            // c=`echo $masterip|cut -d\\. -f3`\n            // d=`echo $masterip|cut -d\\. -f4`\n            // #server_id=`expr $a \\* 256 \\* 256 \\* 256 + $b \\* 256 \\* 256 + $c\n            // \\* 256 + $d `\n            // #server_id=$b$c$d\n            // server_id=`expr $b \\* 256 \\* 256 + $c \\* 256 + $d `\n            InetAddress localHost = InetAddress.getLocalHost();\n            byte[] addr = localHost.getAddress();\n            int salt = (destination != null) ? destination.hashCode() : 0;\n            return ((0x7f & salt) << 24) + ((0xff & (int) addr[1]) << 16) // NL\n                   + ((0xff & (int) addr[2]) << 8) // NL\n                   + (0xff & (int) addr[3]);\n        } catch (UnknownHostException e) {\n            throw new CanalParseException(\"Unknown host\", e);\n        }\n    }\n\n    protected EntryPosition findStartPosition(ErosaConnection connection) throws IOException {\n        if (isGTIDMode()) {\n            // GTID模式下，CanalLogPositionManager里取最后的gtid，没有则取instance配置中的\n            LogPosition logPosition = getLogPositionManager().getLatestIndexBy(destination);\n            if (logPosition != null) {\n                // 如果以前是非GTID模式，后来调整为了GTID模式，那么为了保持兼容，需要判断gtid是否为空\n                if (StringUtils.isNotEmpty(logPosition.getPostion().getGtid())) {\n                    return logPosition.getPostion();\n                }\n            } else {\n                if (masterPosition != null && StringUtils.isNotEmpty(masterPosition.getGtid())) {\n                    return masterPosition;\n                }\n            }\n        }","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/alibaba/canal/blob/87be50e87686a3e8af08c368d0e1ffd1f59eb04a/parse/src/main/java/com/alibaba/otter/canal/parse/inbound/mysql/MysqlEventParser.java#L318-L354","documentation":"generateUniqueServerId() calls InetAddress.getLocalHost(); if the host's name cannot be resolved to an address, the JVM throws UnknownHostException and canal wraps it in CanalParseException(\"Unknown host\"). The server id is derived from the local host IP bytes, so host resolution is mandatory when slaveId is not explicitly set.","triggerScenarios":"canal.instance.slaveId is unset (<=0) and the container/host lacks a resolvable hostname in /etc/hosts or DNS. InetAddress.getLocalHost() then fails.","commonSituations":"Running canal in a minimal container without a localhost entry for its own hostname; a misconfigured /etc/hosts; DNS unreachable at startup.","solutions":["Set canal.instance.slaveId explicitly to a unique positive integer so generateUniqueServerId() is never called.","Ensure /etc/hosts contains an entry mapping the host's hostname to 127.0.0.1 (or its real IP).","Fix DNS/resolution so `hostname -f` and InetAddress.getLocalHost() succeed."],"exampleFix":"# /etc/hosts — ensure the container's hostname resolves\n127.0.0.1   localhost my-canal-host\n# or set slaveId explicitly in instance.properties\ncanal.instance.slaveId=12345","handlingStrategy":"validation","validationCode":"// Avoid relying on InetAddress.getLocalHost()\nif (slaveId <= 0) {\n    slaveId = readConfiguredSlaveId(); // from canal.instance.slaveId\n    if (slaveId <= 0) {\n        // sanity: ensure host resolution works before dump\n        try { InetAddress.getLocalHost(); }\n        catch (UnknownHostException uhe) {\n            throw new IllegalStateException(\"Cannot resolve local host and no canal.instance.slaveId set. Fix /etc/hosts or set slaveId.\", uhe);\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set canal.instance.slaveId to a unique positive integer.","Ensure /etc/hosts maps the container hostname to 127.0.0.1.","Verify `hostname -f` resolves in the canal runtime environment."],"tags":["network","hostname","server-id","config","canal-parse"],"backgroundTag":null,"analyzedSha":"87be50e87686a3e8af08c368d0e1ffd1f59eb04a","analyzedAt":"2026-08-14T04:30:11.918Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}