Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 - javascript

Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
at Promise.tap.then.catch.err (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:123:19)
From previous event:
at ConnectionManager.connect (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:120:13)
at sequelize.runHooks.then (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:311:50)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
From previous event:
at ConnectionManager._connect (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:311:8)
at ConnectionManager.getConnection (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:247:46)
at Promise.try (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/sequelize.js:638:36)
From previous event:
at Promise.resolve.retry (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/sequelize.js:629:53)
at /Users/wooseungjin/codlab-nodejs/node_modules/retry-as-promised/index.js:70:21
at new Promise (<anonymous>)
at retryAsPromised (/Users/wooseungjin/codlab-nodejs/node_modules/retry-as-promised/index.js:60:10)
at Promise.try (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/sequelize.js:629:30)
From previous event:
at Sequelize.query (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/sequelize.js:578:23)
at QueryInterface.dropTable (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/query-interface.js:254:27)
at Function.drop (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/model.js:1388:32)
at Promise.each.model (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/sequelize.js:873:48)
From previous event:
at Sequelize.drop (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/sequelize.js:873:20)
at Promise.try.then (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/sequelize.js:798:21)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
From previous event:
at Sequelize.sync (/Users/wooseungjin/codlab-nodejs/node_modules/sequelize/lib/sequelize.js:796:8)
at Server.app.listen (/Users/wooseungjin/codlab-nodejs/app.js:16:33)
at Object.onceWrapper (events.js:286:20)
at Server.emit (events.js:198:13)
at emitListeningNT (net.js:1313:10)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
I got a problem with using a database (MySQL).
I want to link my web-server and database using express
but there's an error message which is
Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
Why does the error appear?
const Sequelize = require('sequelize');
const sequelize = new Sequelize('node_api_codelab', 'root', 'root',{
host: 'localhost',
dialect: "mysql" // pick one of 'mysql','sqlite','postgres','mssql',
});
const User = sequelize.define('user', {
name: Sequelize.STRING
});
module.exports = {
sequelize: sequelize,
User: User
}

the only reason for error that either your SQL server is not running or your node application don't have permission to access it

Check the mySql server status.
sudo service mysql status
or
ps aux | grep mysql
If your server is active Then ,
//Setting up the config
var sequelize = new Sequelize('your-database-name', 'db-username', 'db-password', {
host: 'localhost',
dialect: 'mysql' // pick one of 'mysql','sqlite','postgres','mssql',
port: 3306,
});
//Checking connection status
sequelize.authenticate().complete(function (err) {
if (err) {
console.log('There is connection in ERROR');
} else {
console.log('Connection has been established successfully');
}
});

Related

Bit.io randomly terminating connecting with node.js

Bit.io randomly terminates connecting with node.js.
When I try to run the following code my node.js works fine for a few minutes but then randomly crashes and gives the error listed at the bottom of this page. I have tried to fix this but I am stuck. Don't know if it's a problem with bit.io or with me. Thanks!!
Code:
const { Client } = require('pg');
const client = new Client({
user: 'process.env.USER',
host: 'db.bit.io',
database: 'process.env.DATABASE',
password: 'process.env.PASSWORD',
port: 5432,
ssl: true,
});
client.connect();
client.query('SELECT * FROM "HPI_AT_state" limit 10;', (err, res) => {
console.table(res.rows);
})
Error:
node:events:491
throw er; // Unhandled 'error' event
^
Error: Connection terminated unexpectedly
at Connection.<anonymous> (node_modules/pg/lib/client.js:132:73)
at Object.onceWrapper (node:events:627:28)
at Connection.emit (node:events:513:28)
at TLSSocket.<anonymous> (node_modules/pg/lib/connection.js:107:12)
at TLSSocket.emit (node:events:525:35)
at endReadableNT (node:internal/streams/readable:1359:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Emitted 'error' event on Client instance at:
at Client._handleErrorEvent (node_modules/pg/lib/client.js:319:10)
at Connection.<anonymous> (node_modules/pg/lib/client.js:149:16)
at Object.onceWrapper (node:events:627:28)
[... lines matching original stack trace ...]
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
I have figured out the problem. It seems that the issue was because Bit.io terminates the connection if unused for 60 seconds. It was fixed by using Client instead of Pool.
const { Pool } = require('pg');
const pool = new Pool({
user: process.env.USER,
host: 'db.bit.io',
database: process.env.DATABASE,
password: process.env.PASSWORD,
port: 5432,
ssl: true,
});
pool.query('SELECT * FROM "SaveData" limit 10;', (err, res) => {
console.table(res.rows);
});

Connection between MariaDB and NodeJS not working

I'm using a vServer from Ionos for a little project. I installed a MariaDB and NodeJS. Currently I'm trying to connect them both. Here is my code:
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
user:'root',
password: 'xxxxxxxxx',
database: 'christmastrees',
connectionLimit: 5,
});
async function asyncFunction() {
let conn;
try {
conn = await pool.getConnection();
const rows = await conn.query("SELECT * from bäume");
console.log(rows); //[ {val: 1}, meta: ... ]
} catch (err) {
throw err;
} finally {
if (conn) return conn.end();
}
}
But when executing the file (node app), I get an error message after a few seconds
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16)
From event:
at _registerHandshakeCmd (/root/server/node_modules/mariadb/lib/connection.js:745:11)
at /root/server/node_modules/mariadb/lib/connection.js:57:11
at new Promise (<anonymous>)
at Connection.connect (/root/server/node_modules/mariadb/lib/connection.js:56:16)
at createConnectionPoolPromise (/root/server/node_modules/mariadb/lib/pool-promise.js:31:8)
at creationTryout (/root/server/node_modules/mariadb/lib/pool-base.js:373:9)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
So I searched for help and after 2 hours I literally found only one Idea. I added this line of code to my connection...
port: '/var/run/mysqld/mysqld.sock'
I tryed again to use node app, but this time I didn't get an error message, it is loading forever and nothing happens.
I would be so grateful for any help...
So I found the solution to my problem.
I checked if my MariaDB Service is even running and no it wasn't ^^

I am getting an error while connecting to mysql database

I am trying to input some data into a mysql database, but I can't connect to it.
My code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
I have also installed mysql into my system by using npm install mysql in cmd. Whenever I try to execute the above code, I get this error:
C:\Users\Abhishek\Desktop\Data scraping\scraping.js:60
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1129:16)
--------------------
at Protocol._enqueue (C:\Users\Abhishek\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\Abhishek\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\Abhishek\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\Users\Abhishek\Desktop\Data scraping\scraping.js:59:5)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
at Module.load (node:internal/modules/cjs/loader:948:32)
at Function.Module._load (node:internal/modules/cjs/loader:789:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:72:12)
at node:internal/main/run_main_module:17:47 {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
How do I fix this?

Unable to connect to remote MySQL database with mysql package

I have tried to connect to my remote MySQL database server with npm's mysql package but it keeps on throwing this error
But it works fine when I use mysqli with PHP
ERROR:
Error: connect ETIMEDOUT
at PoolConnection.Connection._handleConnectTimeout (/home/rilla/Desktop/dbTest/node_modules/mysql/lib/Connection.js:409:13)
at Object.onceWrapper (events.js:416:28)
at Socket.emit (events.js:310:20)
at Socket._onTimeout (net.js:479:8)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7)
--------------------
at Protocol._enqueue (/home/rilla/Desktop/dbTest/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/rilla/Desktop/dbTest/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at PoolConnection.connect (/home/rilla/Desktop/dbTest/node_modules/mysql/lib/Connection.js:116:18)
at Pool.getConnection (/home/rilla/Desktop/dbTest/node_modules/mysql/lib/Pool.js:48:16)
at Object.<anonymous> (/home/rilla/Desktop/dbTest/test.js:22:6)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true
}
MY CODE:
// Get the mysql service
var mysql = require("mysql");
// Add the credentials to access your database
// var connection = mysql.createConnection({
// host: "my_host", // hosts IP here
// port: 3306,
// user: "my_username",
// password: "my_password",
// database: "my_database",
// });
var pool = mysql.createPool({
connectionLimit: 100,
host: "my_host", // hosts IP here
port: 3306,
user: "my_username",
password: "my_password",
database: "my_database",
});
pool.getConnection(function (err, conn) {
if (err) {
console.log(err);
} else {
console.log('it works now')
}
});
// connect to mysql
// connection.connect(function (err) {
// // in case of error
// if (err) {
// console.log(err);
// console.log(err.code);
// console.log(err.fatal);
// } else {
// console.log('it works now')
// }
// });
I have tried with sequelize too but no avail. I can't figure why it's not connecting

Connection to server keeps failing connect ETIMEDOUT 94.195.191.104:27017

Each time I try to connect to my server to access the documents in the database, it comes back with a connect ETIMEDOUT error.
I own the server, running Mongo 4.0, have run mongod and left running, there is data in the collection.
let MongoClient = require('mongodb').MongoClient;
let uri = "mongodb://notmyrealusername:notmyrealpassword#thomas.misson.info/tom-portfolio";
MongoClient.connect(uri, { useNewUrlParser: true }, async function(err, db) {
try{
if (err) throw err;
let res = await db.collection("projects").find().count()
console.log(res);
db.close();
}
catch(err)
{
console.log(err);
}
});
Error message:
{ MongoNetworkError: connect ETIMEDOUT 94.195.191.104:27017
at Socket.err (H:\Documents\Personal projects\node_modules\mongodb-core\lib\connection\connect.js:287:16)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
I have opened the port using netsh advfirewall firewall add rule name="Open mongod port 27017" dir=in action=allow protocol=TCP localport=27017
Have now added new interface to the mongod.cfg file.
# network interfaces net:
port: 27017
bindIp: 127.0.0.1, 94.195.191.104

Categories

Resources