Cannot connect (TimeOut) to MySQL using nodejs mysql2 via ssh tunnel

54 views Asked by At

Really at a loss as to why I am getting a timeout connecting to my RDS instance, and hoping someone has insight on how to debug this.

I'm trying to run database migrations from Github Actions, on my Aurora RDS instance that is on a private network. In GHA, I have successfully created an ssh tunnel via an EC2 jumpbox. Mapping the ports, and using this tunnel, I have used the standard mysql client to connect to it and outputed a query to the console show databases;, and this is successful (see below). So: the tunnel works, and nothing is blocking the connection from either side.

Here's the script, with debugging commands in it as well:

- name: Open SSH Tunnel
        run: |
          echo -n "${{env.KEY}}" > github.pem
          sudo chmod 600 github.pem
          ssh -f -N -L 3307:${{env.DB_HOST}}:3306 ${{env.USER}}@${{env.HOST}} -i ./github.pem -o StrictHostKeyChecking=no
          nc -zv localhost 3307
          mysql -h 127.0.0.1 -P 3307 -u ${{env.DB_USER}} -p${{env.DB_PASSWORD}} -e "SHOW DATABASES;"
          cd database/migrator
          node index.js

I built a migrator using NodeJS, based on mysql2, that simply runs sql scripts from a folder. Very simple. I have successfully tested this code from my local via a tunnel and the code works. However, on GitHub actions, I get a timeout error, and I have no idea why.

This is where I create the connection:

function getConnection() {
    return new Promise((resolve, reject) => {
        const connection = mysql.createConnection({
            host: process.env.DB_HOST,
            port: process.env.DB_PORT,
            user: process.env.DB_USER,
            password: process.env.DB_PASSWORD,
            database: process.env.DB_NAME
        });

        resolve(connection);
    });
}

I connected to the GHA agent using tmate (amazing tool!) to help debug:

      - name: Debug with tmate on failure
        if: ${{ failure() }}
        uses: mxschmitt/action-tmate@v3

On the agent, I dectuple-checked the contents of my env file, and yes, my tunnel maps 3307 to 3306, just to avoid any potential local collisions, I'm using 127.0.0.1 to avoid any mishaps with routing.

DB_HOST=127.0.0.1
DB_PORT=3307
DB_NAME=booking
DB_USER=admin
DB_PASSWORD=<secret>

The DB_PASSWORD is properly set, and regardless I would not get a timeout error if it was wrong.

Why is the connection failing from within the Node context, but not when connecting using the same settings via mysql?

0

There are 0 answers