MyCATApache/Mycat-Server · critical · ConnectionException
err.errno
err.errno
Error message
errMsg
What it means
During the MySQL handshake/authentication phase, MySQLConnectionAuthenticator.handle received an ErrorPacket from the server and rethrows it as ConnectionException(err.errno, errMsg). The message 'can't connect to mysql server, errmsg:...' is logged first. This surfaces the MySQL server's own error (e.g. access denied, unknown database, max connections) at the mycat backend-connection layer.
Solutions
- Read errMsg/errno in the exception to see the MySQL server's reason (1045=access denied, 1049=unknown database, 1040=too many connections)
- Fix the datasource credentials in mycat config to match a MySQL account with access from the mycat host
- Grant privileges: CREATE USER ...@'<mycat-host>'; GRANT ... and FLUSH PRIVILEGES on the MySQL server
- Verify network reachability and that the MySQL server is accepting connections (mysql -h host -u user -p)
Example fix
// before (schema.xml datasource) <dataSource ... user="app" password="wrongpass" /> // after <dataSource ... user="app" password="correctpass" /> <!-- matches MySQL grant for mycat host -->
Defensive patterns
Strategy: try-catch
Validate before calling
try (Connection c = DriverManager.getConnection(url, user, password)) { /* validate credentials before registering datasource with mycat */ } catch (SQLException e) { /* fail fast */ } Try / catch
try { ... } catch (ConnectionException e) { if (e.getErrorCode() == 1045) { fix credentials } else if (e.getErrorCode() == 1040) { retry later / raise max_connections } else { alert ops with errno + errMsg } } Prevention
- Pre-test datasource credentials with a direct mysql client from the mycat host
- Grant MySQL access for the mycat server host explicitly
- Monitor and alert on the 'can't connect to mysql server' warn log
- Keep datasource credentials in a managed secret, rotated with the DB grants
When it happens
Trigger: The backend MySQL server rejects the connection during authentication: wrong user/password in mycat's datasource config, host not allowed, too many connections, or server shutting down.
Common situations: Misconfigured user/password in schema.xml datasources; MySQL grants missing for the mycat host; server-side limits (max_connections, blocked host after failed logins).
Related errors
- can't connect to mysql server ,errmsg
- Unknown Packet!
- global seq and share join and special…
- Unexpected exception
- txIsolation
AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11).
Data as JSON: /api/errors/88ca7e0f6ea1baff.
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)