sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
3.96k stars 594 forks source link

Can not connect to Apache Doris because client flags are conflicted with server flags #2268

Open fenying opened 6 months ago

fenying commented 6 months ago

I am trying to work with Apache Doris recently, but I got connection errors since I changed mysql2 from v2.3.3 to v3.6.3.

For example, the code below, should work perfectly using mysql2@2.3.3, but not with mysql2@3.6.3:

const mysql2 = require('mysql2/promise');

(async () => {

    const conn = await mysql2.createConnection({
        host: '192.168.1.123',
        port: 9030,
        user: 'username',
        password: 'password',
        database: 'database_name',
        debug: true,
        trace: true,
    });

    console.dir(await conn.query('SHOW TABLES;'));
})();

I execute this code, and got following print like in v3.6.3:

Server hello packet: capability flags:557576=(connect with db, protocol 41, secure connection, plugin auth)
Sending handshake packet: flags:12252111=(long password, found rows, long flag, connect with db, odbc, local files, ignore space, protocol 41, ignore sigpipe, transactions, reserved, secure connection, multi results, plugin auth, connect attrs, plugin auth lenenc client data, session track)

and some print in v2.3.3:

Server hello packet: capability flags:557576=(connect with db, protocol 41, secure connection, plugin auth)
Sending handshake packet: flags:11203535=(long password, found rows, long flag, connect with db, odbc, local files, ignore space, protocol 41, ignore sigpipe, transactions, reserved, secure connection, multi results, plugin auth, plugin auth lenenc client data, session track)

The only different thing is the connect attrs flag, which is not supported by remote server, but the client keep sending it. And then the server would refuse the handshake and we got an error like this:

Error: Connection lost: The server closed the connection.
    at Object.createConnection (/home/angus/projects/playground/doris-v3/node_modules/mysql2/promise.js:253:31)
    at /home/angus/projects/playground/doris-v3/test.js:5:31
    at Object.<anonymous> (/home/angus/projects/playground/doris-v3/test.js:17:3)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47 {
  code: 'PROTOCOL_CONNECTION_LOST',
  errno: undefined,
  sqlState: undefined
}

Node.js v20.9.0

I think the problem should be caused by the new default client flags added in file connection_config.js#L230.

And there is no where checking if server supports this flag or not.

I suggest to check both connection.serverCapabilityFlags and clientFlags in client_handshake.js#72 or handshake_response.js#41

However this change may cause some unexpected affects and lead to other problems.

sidorares commented 6 months ago

I wonder if #2261 is related

fenying commented 6 months ago

I wonder if #2261 is related

oh no... I search only about doris, didn't find this issue...😐

I checked his PR, but it seems like a server-side patch, not work with Doris. I think this should be a client-side problem.

wangbokun commented 5 months ago

I had the same problem, I replaced msyql2 with mysql library, which is now available

jeffreys-cat commented 2 months ago

Have the same problem

CherishLyf commented 1 month ago

+1...

WuQic commented 2 weeks ago

I'm also having the same issue Is there a solution, here's the error stack

Add command: ClientHandshake
 raw: 0a352e372e393900c61c00005e4c604e6f57626e0008822100000800150000000000000000000052535f4a5c4a636f5d5f5f4b006d7973716c5f6e61746976655f70617373776f726400
Trace
    at Connection.handlePacket (/workspaces/sugo-cold-chain-link/server/node_modules/mysql2/lib/connection.js:433:17)
    at PacketParser.onPacket (/workspaces/sugo-cold-chain-link/server/node_modules/mysql2/lib/connection.js:97:12)
    at PacketParser.executeStart (/workspaces/sugo-cold-chain-link/server/node_modules/mysql2/lib/packet_parser.js:75:16)
    at Socket.<anonymous> (/workspaces/sugo-cold-chain-link/server/node_modules/mysql2/lib/connection.js:104:25)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
0 undefined ==> ClientHandshake#unknown name(0,,78)
Server hello packet: capability flags:557576=(connect with db, protocol 41, secure connection, plugin auth)
Sending handshake packet: flags:12252111=(long password, found rows, long flag, connect with db, odbc, local files, ignore space, protocol 41, ignore sigpipe, transactions, reserved, secure connection, multi results, plugin auth, connect attrs, plugin auth lenenc client data, session track)
0 7366 <== ClientHandshake#unknown name(1,,138)
0 7366 <== 86000001cff3ba0000000000e00000000000000000000000000000000000000000000000726f6f7400148787fbc783c9c58fe9062d64cf6b608e83e36e6874657374006d7973716c5f6e61746976655f70617373776f726400300c5f636c69656e745f6e616d650c4e6f64652d4d7953514c2d320f5f636c69656e745f76657273696f6e05332e392e37
/workspaces/sugo-cold-chain-link/server/node_modules/mysql2/promise.js:253
  const createConnectionErr = new Error();
                              ^

Error: Connection lost: The server closed the connection.
    at Object.createConnection (/workspaces/sugo-cold-chain-link/server/node_modules/mysql2/promise.js:253:31)
    at /workspaces/sugo-cold-chain-link/server/test.js:19:31
    at Object.<anonymous> (/workspaces/sugo-cold-chain-link/server/test.js:21:3)
    at Module._compile (node:internal/modules/cjs/loader:1198:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
    at Module.load (node:internal/modules/cjs/loader:1076:32)
    at Function.Module._load (node:internal/modules/cjs/loader:911:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47 {
  code: 'PROTOCOL_CONNECTION_LOST',
  errno: undefined,
  sqlState: undefined
}