Home > Enterprise >  Unable to make request to my mariadb database using node server
Unable to make request to my mariadb database using node server

Time:11-27

I'm currently learning how to setup a node server and i'm making an api that does some requests to my mongodb database hosted on my vps.

The problem is that when I listen my POST request and that I make my SQL request to my database, the connection times out and the server shutdown.

I have tried to add new users to mariadb with all privileges, I tried use sequelize too.

But none of those solutions works, it still times out everytime a make a query to my database. I can connect to phpmyadmin and do some request on it so I think that my database is running fine.

Here is my code:


router.post('/login', async function(req,res) {
    let conn;
    try {
        // establish a connection to MariaDB
        conn = await pool.getConnection();

        // create a new query
        var query = "select * from people";

        // execute the query and set the result to a new variable
        var rows = await conn.query(query);

        // return the results
        res.send(rows);
    } catch (err) {
        throw err;
    } finally {
        if (conn) return conn.release();
    }
})

The way I connect to my database in my database.js file


const pool = mariadb.createPool({
    host: process.env.DATABASE_HOST,
    user: process.env.DATABASE_USER,
    password: process.env.DATABASE_PASSWORD,
    database: process.env.DATABSE_NAME,
});



// Connect and check for errors
module.exports={
    getConnection: function(){
        return new Promise(function(resolve,reject){
            pool.getConnection().then(function(connection){
                resolve(connection);
            }).catch(function(error){
                reject(error);
            });
        });
    }
}

module.exports = pool;

And my error:


Node.js v17.0.1
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Server started
/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/misc/errors.js:61
  return new SqlError(msg, sql, fatal, info, sqlState, errno, additionalStack, addHeader);
         ^

SqlError: retrieve connection from pool timeout after 10001ms
    at Object.module.exports.createError (/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/misc/errors.js:61:10)
    at timeoutTask (/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/pool-base.js:319:16)
    at Timeout.rejectAndResetTimeout [as _onTimeout] (/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/pool-base.js:342:5)
    at listOnTimeout (node:internal/timers:559:11)
    at processTimers (node:internal/timers:500:7) {
  text: 'retrieve connection from pool timeout after 10001ms',```



Thanks for you help !

CodePudding user response:

Three possibilities come to mind:

  1. There is a typo in database name:

    database: process.env.DATABSE_NAME

    database: process.env.DATABASE_NAME

  2. Your environment variables are not being properly set. Are you using dotenv to load these from an .env file?

    https://www.npmjs.com/package/dotenv

If not, how are you setting the process.env values at runtime?

  1. If the environment values are indeed set:
  • verify that these environment values are correct
  • verify which interface your MariaDB server is listening on: It's possible the server is using a bind-address configuration and only listening on 127.0.0.1 (which is the default on Debian/Ubuntu)
  • You want to make sure the server is listening on: 0.0.0.0 (all interfaces, not only localhost)
  • Related