Nodejs Mocha test failing Jenkins - mysql

I am testing my db connection using mocha module of nodejs and the test is passing locally.
describe('Access to DB', function(){
describe('#fail', function(){
it('should return -1 because wrong credentials', function(done){
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'head',
database: 'head'
});
connection.connect(done);
});
})
});
./node_modules/.bin/mocha ./test/test.js tests pass when I run this command.
But when I run my job using jenkins. The test fails with error:
Access to DB #fail should return -1 because wrong credentials: [0m[31m Error: connect ECONNREFUSED 127.0.0.1:3306[0m[90m at TCPConnectWrap.afterConnect [as oncomplete]
My jenkins is instaled in different server and Node app in different one. I am comunication between them with SHA keys. Everything is working except for this test. mysql is not installed in jenkins server. Could that be the reason.

Related

How to connect to mysql server from nodejs app using sequelize. But, earlier i used docker and ran a mysql container. Now am unable to connect locally

Whenever i try to connect my express.js app to the node server. I get an error(see below console message).
Earlier, before dockerizing my app everything used to run smoothly but after creating my mysql-image-container(docker) and running mysql through docker everything went well. But now, when i try to connect my app with SQL server and not through the docker-container i get the following error
(node:22136) UnhandledPromiseRejectionWarning: SequelizeAccessDeniedError: Access denied for user 'root'#'localhost' (using password: YES)
at ConnectionManager.connect (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\mysql\connection-manager.js:94:17)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async ConnectionManager._connect (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:220:24)
at async C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:174:32
at async ConnectionManager.getConnection (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:197:7)
at async C:\Users\A\ARISE1\node_modules\sequelize\lib\sequelize.js:304:26
at async MySQLQueryInterface.tableExists (C:\Users\A\ARISE1\node_modules\sequelize\lib\dialects\abstract\query-interface.js:102:17)
at async Function.sync (C:\Users\A\ARISE1\node_modules\sequelize\lib\model.js:939:21)
at async Sequelize.sync (C:\Users\A\ARISE1\node_modules\sequelize\lib\sequelize.js:376:9)
(Use node --trace-warnings ... to show where the warning was created)
(node:22136) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:22136) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I have tried deleting docker file, mysql-image-container(from docker) but am still unable to connect my app with local mysql server. And now if i even try to connect to my database through SQL-command line, i am unable to do so, even with correct credentials. Though I'm able to connect to the database through MySQL Workbench.
The docker command used to build-image/run the container is:
docker run -p 3306:3306 --name nodejs-mysql -e MYSQL_ROOT_PASSWORD=pass -e MYSQL_DATABASE=database -d mysql:5.7
before this, i used to right-click on the dockerfile and choose build image option.
I didn't completely understood the contents of Dockerfile and so i mixed some code from online sources.
Any help would be well appreciated.
const sequelize = new Sequelize( username, password, database, {
host: host,
dialect: dialect
});
Instead of above, it should be this
const sequelize = new Sequelize( database, username, password, {
host: host,
dialect: dialect
});
Please share your code.
Also see the the code below on how you establish a connection
const { Sequelize,DataTypes } = require('sequelize');
const config={
HOST:"localhost",// you can replace this with the url of where your server is hosted or an ip adress
USER:"root",
PASSWORD:"yourpassword",
DB:"yourDb",
dialect:"mysql",//sqlite, mariadb
pool:{
max:5,
min:0,
acquire:30000,
idle:10000
}
const sequelize= new Sequelize(
config.DB,
config.USER,
config.PASSWORD,
{
host:config.HOST,
dialect:config.dialect,
operatorAliases:false,
pool:{
max:config.pool.max,
min:config.pool.min,
acquire:config.pool.acquire,
idle:config.pool.idle
}
}
)
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}

how do i connect to a remote mysql database using node?

i have a node application that is duplicated both on the server (digital ocean) as well as my local machine. im trying to configure the local version so that i can use it as a 'sandbox' environment to make changes and edits before i push the update to the live application on the server.
the databaseconnection.js file contains the following.
const mysql = require('mysql');
function getConnection() {
return mysql.createPool({
connectionLimit: 100,
host: 'ipaddress',
port: 3306,
user: 'forge',
password: 'password',
database: 'mydatabase',
multipleStatements: true
});
}
module.exports = getConnection();
when i access the node application that is stored on the DO server, everything run fine, however, when i try to run it from my localhost environment, i get the following error.
Error: connect ETIMEDOUT
at PoolConnection.Connection._handleConnectTimeout (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Connection.js:409:13)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at Socket._onTimeout (net.js:442:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
--------------------
at Protocol._enqueue (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at PoolConnection.connect (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Connection.js:116:18)
at Pool.getConnection (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Pool.js:48:16)
at Pool.query (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Pool.js:202:8)
at Strategy.passport.use.OutlookStrategy [as _verify] (C:\MAMP\htdocs\WIGHTcloudDEV\config\passport-setup.js:58:18)
at C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\passport-oauth2\lib\strategy.js:202:24
at Request._callback (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\passport-outlook\lib\strategy.js:157:5)
at Request.self.callback (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\request\request.js:185:22)
at Request.emit (events.js:198:13)
i slightly remember doing something with either ssh2 or tunnel forwarder in the past that pertained to this issue but i cant find any of my old files nor can i find anything online that clearly shows what im missing.

Heroku Deployment error:connection refused

I have deployed my app on heroku..but the problem is ,it is giving this error
Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
my config.js file-
module.exports={
port:process.env.PORT || 8082,
production: {
use_env_variable: process.env.DATABASE_URL
},
authentication:{
jwtSecret: process.env.JWT_SECRET|| 'secret'
}
}
my database connection file-
const Sequelize=require('sequelize')
const config=require('../config/config')
let sequelize=null;
if(process.env.DATABASE_URL){
sequelize=new Sequelize(config.production.use_env_variable,{dialect:'mysql'})
}
else{
sequelize=new Sequelize('bloggy','root','12345678',{dialect:'mysql',host:'localhost'})
}
module.exports=sequelize
I am not understanding why it is throwing that error.. it was working fine locally?
can anyone please help?
Ensure that in Heroku you have set the DATABASE_URL in environment variable b running Heroku config:set DATABASE_URL=<your database url>. This will ensure the if statement will return true and execute the first block

Node JS remote mysql database connection error

I am relatively new to the Node JS, and I have been trying to connect to a remote mysql server, but I have been unable to do so. I have been looking for the solutions on here but most of them are for localhost. Here is my code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "###.ipagemysql.com",
user: "user",
password: "mypass",
database: "mydb",
debug: true
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
I am getting the error below:
Error: connect ECONNREFUSED 103.224.212.250:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
UPDATE:
I tried using a different MySQL server and that works fine. Could it be because iPage is blocking the connection because it is coming from a foreign machine? Is this something that can be configured in phpmyadmin?
Possibly port 3306 is not accessible from the application. You can try telnet to server 103.224.212.250:3306 to see if it is opened or not.

connect ECONNREFUSED - node js , sql

I have the next code in a js file:
var mysql = require('mysql');
var TEST_DATABASE = 'nodejs_mysql_test';
var TEST_TABLE = 'test';
var client = mysql.createClient({
user: 'root',
password: 'root',
});
client.query('CREATE DATABASE '+TEST_DATABASE, function(err) {
if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
throw err;
}
});
But I get this error:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connect ECONNREFUSED
at errnoException (net.js:632:11)
at Object.afterConnect [as oncomplete] (net.js:623:18)
As I understand it, this is a connection problem - but how do I solve it?
( I'm working on windows 7)
Thanks!!
I know two ways to solve it:
In mysql.conf, comment skip-networking.
Try to set the socket like this:
var client = mysql.createClient({
user: uuuu,
password: pppp,
host: '127.0.0.1',
port: '3306',
_socket: '/var/run/mysqld/mysqld.sock',});
I got this error when MySQL Server was not running.
I changed my configuration via Initialize Database in MySQL.PrefPane, the System Preferences tool for MySQL on OS X - to Use Legacy Password Encryption - to fix ER_NOT_SUPPORTED_AUTH_MODE.
This config change stopped MySQL Server and then I got ECONNREFUSED when I tried to connect to MySQL from node.js.
Fixed by restarting MySQL Server from the MySQL System Preferences tool.
Try to fix your defined port in mysql and your Node.js script.
You can define the mysqld port in the *.cnf file inside the mysql directory,
and you can define that port when you connect to MySQL in your Node.js script.
Something like this in the cnf file
port = 3306