{"record":{"id":"28715d37d81f2f6e","repo":"MyCATApache/Mycat-Server","slug":"can-t-connect-to-mysql-server-errmsg-errmsg","errorCode":null,"errorMessage":"can't connect to mysql server ,errmsg:{errMsg}","messagePattern":"can't connect to mysql server ,errmsg:(.+?)","errorType":"exception","errorClass":"ConnectionException","httpStatus":null,"severity":"critical","filePath":"src/main/java/io/mycat/backend/mysql/nio/MySQLConnectionAuthenticator.java","lineNumber":93,"sourceCode":"\t\t\t\tsource.setHandler(new MySQLConnectionHandler(source));\n\t\t\t\tsource.setAuthenticated(true);\n\t\t\t\tboolean clientCompress = Capabilities.CLIENT_COMPRESS==(Capabilities.CLIENT_COMPRESS & packet.serverCapabilities);\n\t\t\t\tboolean usingCompress= MycatServer.getInstance().getConfig().getSystem().getUseCompression()==1 ;\n\t\t\t\tif(clientCompress&&usingCompress)\n\t\t\t\t{\n\t\t\t\t\tsource.setSupportCompress(true);\n\t\t\t\t}\n\t\t\t\tif (listener != null) {\n\t\t\t\t\tlistener.connectionAcquired(source);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase ErrorPacket.FIELD_COUNT:\n\t\t\t\tErrorPacket err = new ErrorPacket();\n\t\t\t\terr.read(data);\n\t\t\t\tString errMsg = new String(err.message);\n\t\t\t\tLOGGER.warn(\"can't connect to mysql server ,errmsg:\"+errMsg+\" \"+source);\n\t\t\t\t//source.close(errMsg);\n\t\t\t\tthrow new ConnectionException(err.errno, errMsg);\n\n\t\t\tcase EOFPacket.FIELD_COUNT:\n\t\t\t\tauth323(data[3]);\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tpacket = source.getHandshake();\n\t\t\t\tif (packet == null) {\n\t\t\t\t\tprocessHandShakePacket(data);\n\t\t\t\t\t// 发送认证数据包\n\t\t\t\t\tsource.authenticate();\n\t\t\t\t\tbreak;\n\t\t\t\t} else {\n\t\t\t\t\tthrow new RuntimeException(\"Unknown Packet!\");\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t} catch (RuntimeException e) {","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/backend/mysql/nio/MySQLConnectionAuthenticator.java#L75-L111","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (MySQL 8 user with caching_sha2_password failing)\nCREATE USER 'mycat'@'%' IDENTIFIED WITH caching_sha2_password BY 'pw';\n// after\nALTER USER 'mycat'@'%' IDENTIFIED WITH mysql_native_password BY 'pw';\nGRANT ALL ON *.* TO 'mycat'@'%';","handlingStrategy":"try-catch","validationCode":"// validate backend credentials before adding datasource\ntry (Connection c = DriverManager.getConnection(\"jdbc:mysql://\" + host + \":\" + port + \"/\" + schema, user, pass)) {\n  c.createStatement().execute(\"SELECT 1\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  session.executeOnBackend(...);\n} catch (ConnectionException e) {\n  logger.error(\"backend auth/connect failed errno={} msg={}\", e.getErrNo(), e.getMessage());\n  // alert / mark datasource down, do not hot-retry auth failures\n}","preventionTips":["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."],"tags":["mysql","authentication","backend-connection","error-packet"],"backgroundTag":"authentication-required","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}