mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.27k stars 2.52k forks source link

getConnection always get timed out #2566

Open blu2eh opened 10 months ago

blu2eh commented 10 months ago

So in this code I wrote, I am trying to connect to my database through a ssh tunnel. And when I am only using oracledb.createPool(dbconfig), everything works fine. But when I wrote const connection = await oracledb.getConnection(dbConfig);, the process timed out. The SSH tunnel got connected successfully by the way.

const { Client } = require('ssh2');
const oracledb = require('oracledb');

const configs = require('./config')

const con = new Client();
console.log('Initializing:', configs);
console.log('Initializing:', configs.sshConfig.port);

con.on('ready', function() {
    console.log('Connection :: ready');
    con.shell(function(err, stream) {
        if (err) {
            console.log('err over ')
        } else {
            connectToDatabase();
            var buf = '';
            var bData = false;
            stream.on('data', function(data) {

                if (bData == false) {
                    bData = true;
                    stream.write('su\n');
                }
                if (data.indexOf('Password:') > -1) {
                    stream.write('Pxxxxxxxx')
                }
                console.log(data.toString())
                if (data.indexOf('# ') > -1) {
                    console.log('successful')
                    stream.end()
                }
            }).on('close', function() {
                console.log('success over ')
            }).stderr.on('data', function(data) {
                console.log('stderr: ' + data);
            });
        }
    });
});
con.on('error', function(err) {
    console.log('Connection :: error :: ' + err);
});
con.on('end', function() {
    console.log('Connection :: end');
});
con.on('close', function(had_error) {
    console.log('Connection :: close');
});

con.connect({
    host: configs.sshConfig.host,
    port: configs.sshConfig.port,
    username: configs.sshConfig.username,
    password: configs.sshConfig.password
})

// Connecting to remote database
async function connectToDatabase() {
    try {
        // Configuring remote 
        const dbConfig = {
            user: configs.mysql.user, // database username
            password: configs.mysql.password, //database password
            connectString: 'my connect string', // connect string

        // Creating pool
        const pool = await oracledb.createPool(dbConfig);
        console.log('Connected to the remote Oracle database successfully.');

        // Creating connection
        const connection = await oracledb.getConnection(dbConfig);

        //Closing pool and ssh tunnel
        pool.close((err) => {
            if (err) {
                console.error('Error closing the database connection:', err);
                return;
            }
            console.log('Database connection closed successfully.');
            con.end() // disconnect from ssh
        });
    } catch (err) {
        console.error('Error connecting to the remote Oracle database:', err);
    } finally {

    }
}