mysqljs / mysql

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

Retrying MySQL connectinon on Docker #2449

Closed ghost closed 2 years ago

ghost commented 3 years ago

Hello, I'm making a website and I use Docker to combine everything. I've made a discord bot to manage content for the site. The bot worked on my local machine (not using Docker), and apparently my discord bot starts faster than MySQL, so when the bot tries to connect, it refuses the connection. My friend made a queuer for my site to process videos, and it has a section to keep retrying to connect since he knows that it won't connect on the first try because of Docker. Is there a way to keep trying to reconnect using MySQLjs? Thank you.

dougwilson commented 3 years ago

Hi! There are two methods:

  1. The createConnection function is what creates a connection. Just keep calling that as much as you like to retry.
  2. You could use the Pool of this module and that will internally call createConnection each time you attempt a query until it gets connected.

I hope this helps!

ghost commented 3 years ago

I see, so if I just did

con.connect(function(err) {
    if (err) {
      console.error('error connecting, trying again: ');
      await new Promise(resolve => setTimeout(resolve, 30000));
      con.connect();
    }
    console.log('connected as id ' + con.threadId);
});

, would it hopefully just work without a problem? I'm kind of new to Node, sorry if this is a bad solution.

ghost commented 3 years ago

Never mind, just tried it out and this type of wait only works in asynchronous functions apparently.

ghost commented 3 years ago
fulpbot       | Error: connect ECONNREFUSED 127.0.0.1:3306
fulpbot       |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)

I made it keep retrying, but this happens every single time. I'm guessing this is a docker issue, because my Docker's apache connects just fine to it.

var con = mysql.createConnection({
    host: "localhost",
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASSWORD,
    database: process.env.MYSQL_DATABASE
});
FROM node:14.15.2

WORKDIR /usr/src/fulpbot

COPY package*.json ./
RUN npm i

COPY . .
CMD [ "node", "./src/bot.js" ]
    discordbot:
        build:
            context: ./fulpbot
        depends_on:
            - sql
        networks:
            - fulp_vnet
        environment:
            - MYSQL_USER=root
            - MYSQL_PASSWORD=${MYSQL_ROOT_PASSWORD}
            - MYSQL_DATABASE=fulptube
            - MYSQL_HOST=sql
            - DISCORD_TOKEN=discordtoken
        container_name: fulpbot

Ask me if you need more info, i'm kind of confused why it's refusing the connection.

ghost commented 3 years ago

Also, yes, Apache does use the same environment variables.


    php:
        build:
            context: ./phpwsql
        depends_on:
            - sql
        networks:
            - fulp_vnet
        environment:
            - MYSQL_USER=root
            - MYSQL_PASSWORD=${MYSQL_ROOT_PASSWORD}
            - MYSQL_DATABASE=fulptube
            - MYSQL_HOST=sql
        volumes:
            - ./www/:/var/www/html
            - ./www/dynamic/:/dynamic
        ports:
            - "80:80"
        container_name: fulpphp```
genosmrpg7899 commented 3 years ago

Real.

e-l-i-j-a-h commented 3 years ago

Real.

dougwilson commented 2 years ago

Hi, unfortunately connecting to your MySQL server is outside the scope of this module; if Node.js says the connection is refused, there is something wrong in the environment in some way, or perhaps Node.js itself cannot connect.