MyCATApache/Mycat-Server · critical · ConnectionException
can't connect to mysql server ,errmsg
Error message
can't connect to mysql server ,errmsg:{errMsg} What it means
MySQLConnectionAuthenticator.handle() processes the MySQL handshake response. When the server replies with an ErrorPacket instead of OK/EOF during authentication, the packet's error message is read and a ConnectionException with the server error number is thrown. This means the backend MySQL server rejected the connection attempt at the protocol/auth stage.
Solutions
- Read the errMsg in the exception/log: fix the exact MySQL error — most often wrong user/password in the datasource config.
- Grant access on the MySQL server: CREATE/ALTER USER and GRANT privileges for the MyCat host, and use a compatible auth plugin (mysql_native_password if needed).
- Check server limits (max_connections, host cache) if the message indicates refused/too many connections.
- Update the backend connection config in schema.xml/server.xml and restart MyCat.
Example fix
// before (MySQL 8 user with caching_sha2_password failing) CREATE USER 'mycat'@'%' IDENTIFIED WITH caching_sha2_password BY 'pw'; // after ALTER USER 'mycat'@'%' IDENTIFIED WITH mysql_native_password BY 'pw'; GRANT ALL ON *.* TO 'mycat'@'%';
Defensive patterns
Strategy: try-catch
Validate before calling
// validate backend credentials before adding datasource
try (Connection c = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + schema, user, pass)) {
c.createStatement().execute("SELECT 1");
} Try / catch
try {
session.executeOnBackend(...);
} catch (ConnectionException e) {
logger.error("backend auth/connect failed errno={} msg={}", e.getErrNo(), e.getMessage());
// alert / mark datasource down, do not hot-retry auth failures
} Prevention
- Keep schema.xml datasource credentials in sync with MySQL users.
- GRANT access for the MyCat host and use mysql_native_password for older stacks.
- Monitor backend connections and max_connections to avoid server-side refusals.
When it happens
Trigger: A backend datasource connecting with wrong user/password, a user without access to the target schema, host blocked by MySQL grants ('Host ... is not allowed to connect'), or the server returning errors like 'too many connections' during handshake.
Common situations: Schema config (schema.xml / user config) credentials out of sync after a MySQL password change, missing GRANT for the MyCat host, max_connections exhausted on the backend, or MySQL 8 caching_sha2_password with an older client stack.
Related errors
AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11).
Data as JSON: /api/errors/28715d37d81f2f6e.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/io/mycat/backend/mysql/nio/MySQLConnectionAuthenticator.java:93
source.setHandler(new MySQLConnectionHandler(source));
source.setAuthenticated(true);
boolean clientCompress = Capabilities.CLIENT_COMPRESS==(Capabilities.CLIENT_COMPRESS & packet.serverCapabilities);
boolean usingCompress= MycatServer.getInstance().getConfig().getSystem().getUseCompression()==1 ;
if(clientCompress&&usingCompress)
{
source.setSupportCompress(true);
}
if (listener != null) {
listener.connectionAcquired(source);
}
break;
case ErrorPacket.FIELD_COUNT:
ErrorPacket err = new ErrorPacket();
err.read(data);
String errMsg = new String(err.message);
LOGGER.warn("can't connect to mysql server ,errmsg:"+errMsg+" "+source);
//source.close(errMsg);
throw new ConnectionException(err.errno, errMsg);
case EOFPacket.FIELD_COUNT:
auth323(data[3]);
break;
default:
packet = source.getHandshake();
if (packet == null) {
processHandShakePacket(data);
// 发送认证数据包
source.authenticate();
break;
} else {
throw new RuntimeException("Unknown Packet!");
}
}
} catch (RuntimeException e) {View on GitHub (pinned to 65f8d8beb7)