MyCATApache/Mycat-Server · error · ConfigException

dataHost name duplicated!

Error message

dataHost name ${name}duplicated!

What it means

During loading of schema.xml, XMLSchemaLoader iterates all <dataHost> elements and keeps them in a map keyed by the 'name' attribute. If a second dataHost reuses an already-registered name, the loader throws this ConfigException because dataHost names must be unique for pool routing. Startup aborts.

Solutions

  1. Search schema.xml for duplicate dataHost name attributes; rename the second occurrence to a unique name
  2. Update all <dataNode dataHost="..."> references that should point to the renamed host
  3. If the duplication comes from merged/included config files, deduplicate the fragments before deployment
  4. Restart MyCat and verify the schema loads

Example fix

// before
<dataHost name="hostM1" ...>...</dataHost>
<dataHost name="hostM1" ...>...</dataHost>
// after
<dataHost name="hostM1" ...>...</dataHost>
<dataHost name="hostM2" ...>...</dataHost>
Defensive patterns

Strategy: validation

Validate before calling

// detect duplicate dataHost names in schema.xml
Set<String> seen = new HashSet<>();
NodeList list = doc.getElementsByTagName("dataHost");
for (int i = 0; i < list.getLength(); i++) {
    String name = ((Element) list.item(i)).getAttribute("name");
    if (!seen.add(name)) throw new IllegalStateException("Duplicate dataHost name: " + name);
}

Type guard

null

Try / catch

try {
    schemaLoader.load();
} catch (ConfigException e) {
    LOG.error("Duplicate dataHost name in schema.xml: " + e.getMessage());
    throw new ConfigurationException("Ensure dataHost names are unique", e);
}

Prevention

When it happens

Trigger: Parsing <schema> XML where two <dataHost name="hostM1"> elements (or a duplicated include) define the same name attribute.

Common situations: Copy-pasting a dataHost block to add a second host and forgetting to change the name; merging config fragments that each define hostM1; automated config generation emitting duplicate names.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/4cf4ff032e89dcd3. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/config/loader/xml/XMLSchemaLoader.java:755

        DBHostConfig conf = new DBHostConfig(nodeHost, ip, port, nodeUrl, user, passwordEncryty, password,checkAlive);
        conf.setDbType(dbType);
        conf.setMaxCon(maxCon);
        conf.setMinCon(minCon);
        conf.setFilters(filters);
        conf.setLogTime(logTime);
        conf.setWeight(weight);    //新增权重
        return conf;
    }

    private void loadDataHosts(Element root) {
        NodeList list = root.getElementsByTagName("dataHost");
        for (int i = 0, n = list.getLength(); i < n; ++i) {

            Element element = (Element) list.item(i);
            String name = element.getAttribute("name");
            //判断是否重复
            if (dataHosts.containsKey(name)) {
                throw new ConfigException("dataHost name " + name + "duplicated!");
            }
            //读取最大连接数
            int maxCon = Integer.parseInt(element.getAttribute("maxCon"));
            //读取最小连接数
            int minCon = Integer.parseInt(element.getAttribute("minCon"));
            /**
             * 读取负载均衡配置
             * 1. balance="0", 不开启分离机制,所有读操作都发送到当前可用的 writeHost 上。
             * 2. balance="1",全部的 readHost 和 stand by writeHost 参不 select 的负载均衡
             * 3. balance="2",所有读操作都随机的在 writeHost、readhost 上分发。
             * 4. balance="3",所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压力
             */
            int balance = Integer.parseInt(element.getAttribute("balance"));
            /**
             * 负载均衡配置
             * 1. balanceType=0, 随机
             * 2. balanceType=1,加权轮询
             * 3. balanceType=2,最少活跃

View on GitHub (pinned to 65f8d8beb7)