如何在mysql npm中的连接选项中提供SERVICE_NAME?

时间:2017-09-15 06:00:59

标签: mysql node.js jdbc tcp node-mysql

我有一个类似这样的JDBC连接字符串

jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=OFF)(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1711))(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1712)))(CONNECT_DATA=(SERVICE_NAME=servicename)(SERVER=DEDICATED)))

我已将连接创建为

var connection = mysql.createConnection({
    host: "hostname",
    port: "1711",
    user: "user", 
    password: "password"
});

如何向连接选项提供SERVICE_NAME(如jdbc字符串)? 没有它我收到以下错误:

Error: connect ECONNREFUSED <some_ip>:1711
    at Object.exports._errnoException (util.js:1028:11)
    at exports._exceptionWithHostPort (util.js:1051:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
    --------------------

编辑: 将端口更改为1712会在花费大量时间后给出不同的错误。

Error: Connection lost: The server closed the connection.
    at Protocol.end (..\node_modules\mysql\lib\protocol\Protocol.js:113:13)
    at Socket.<anonymous> (..\node_modules\mysql\lib\Connection.js:109:28)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:186:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
编辑:抱歉。我没有看到数据库是oracle并且直接开始尝试mysql。

1 个答案:

答案 0 :(得分:0)

我假设你的mysql服务器在端口1711上运行而不是默认端口。 如果您收到Connection lost错误,则意味着您的网络出现问题,请稍后重新连接服务器。

var conn;

function handleDisconnect() {
  conn = mysql.createConnection(//your config); 


  conn.connect(function(err) {              
    if(err) {                                    
      console.log('error when connecting to database:', err);
      setTimeout(handleDisconnectConnection, 5000); 
    }                                     
  });                                    

  connection.on('error', function(err) {
    console.log('db error', err);
    if(err.code === 'PROTOCOL_CONNECTION_LOST') { 
      handleDisconnectConnection();                        
    } else {                                      
      throw err;                                  
    }
  });
}

handleDisconnectConnection();

希望这对你有用