Node.js config file for mysql DB - mysql

I have a small issue when setting up my config file. I'm sure this is something simple but I don't see what is the problem.
I have my config file config.js under config/config.js
var databaseOptions = {
host : 'localhost',
database : 'test',
user : 'root',
password : 'root',
port : '8889'
};
module.exports = databaseOptions;
And then I use it in my model:
var config = require('../config/config.js');
var mysql = require('mysql');
var connection = mysql.createConnection(config.databaseOptions);
But it doesn't work ...
Instead I get an error : TypeError: Cannot read property 'host' of undefined
I also tried like this :
var connection = mysql.createConnection({
host : config.databaseOptions.host,
database : config.databaseOptions.database,
user : config.databaseOptions.user,
password : config.databaseOptions.password,
port : config.databaseOptions.port
});
... but I still get an undefined error.
Any idea ...?

You are exporting databaseOptions directly so you just need:
var databaseOptions = require('../config/config.js');
var connection = mysql.createConnection(databaseOptions);
If you want to use config.databaseOptions, you need to export:
var databaseOptions = {
host : 'localhost',
database : 'test',
user : 'root',
password : 'root',
port : '8889'
};
module.exports = {databaseOptions: databaseOptions} ;
or
module.exports.databaseOptions = {
host : 'localhost',
database : 'test',
user : 'root',
password : 'root',
port : '8889'
};
Then you can use:
var config = require('../config/config.js');
var connection = mysql.createConnection(config.databaseOptions);
The second way will be more flexible if you have more than one object you want to export from config.

Related

I keep getting connection timeout error trying to connect to mysql database using NodeJS

I'm trying to connect to an external mysql database from nodejs using util.pomisify. Below is my code
var http = require('http');
var mysql = require('mysql');
var async = require('async');
var util = require('util');
var dbCon = mysql.createPool({
host : '169.255.58.96',
user : 'username',
password : 'password',
database : 'database',
acquireTimeout : 1000000,
connectTimeout : 1000000,
waitForConnections : true,
queueLimit : 0
});
var query = util.promisify(dbCon.query).bind(dbCon);
But I get time out error. Then I try to connect to the database directly without using util.promisify
var mysqliQuery = dbCon.getConnection((err, connected) => {
console.log('Connecting to Database');
if(!err)
console.log('Database Connected');
else
console.log(err.code);
});
But I still get the ERTIMEDOUT message... Please can someone tell me what I'm doing wrong?
You can try this as a connection creation
const mysql = require('mysql');
const db = mysql.createConnection({
host : '169.255.58.96',
user : 'username',
password : 'password',
database : 'database',
acquireTimeout : 1000000,
connectTimeout : 1000000,
waitForConnections : true,
queueLimit : 0
});
and to make sure if you are connected or not use this:
db.connect((error)=>{
if (error){
console.log(error)
} else {
console.log("Connected..")
}
});

mysql2/promise require database connection file

I have only been working with PHP before going to Node.JS. What I was able to do in PHP when working with MYSQL was that I could include the database.php file in the files I wanted to execure queries in.
It doesn't seem to be the same in Node.Js. This is my database.js file
const mysql = require("mysql2/promise");
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'XXXX',
database: 'nodelogin'
});
module.exports = db;
Then I require this in my file login.js
const db = require("../../database");
However, when I then try to run db.query(sql, [variable]) I get db.query is not a function.
Why is this? It shouldn't be that more complicated or should it?
If you use a connection pool instead, you can include the pool once, then call query on it, like so:
db-config.js
const mysql = require("mysql2/promise");
console.log("Creating connection pool...")
const pool = mysql.createPool({
host: 'localhost',
user: 'user',
database: 'test_db',
password: 'password'
})
module.exports = pool;
test.js
// Require to whereever db-config is
const pool = require('./db-config.js');
async function testQuery() {
const results = await pool.query("select * from users");
console.table(results[0]);
}
testQuery();

How to create mysql connection with pool request?

how include Setup Connections and Parser?
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'localhost',
user : 'medb',
password : '123458',
database : 'test',
connectionLimit: 100
});
What would be next ?
Hi this is complete step to get mysql connection with pool request.
var mysql = require('mysql');
//Setup Connections and Parser
var pool = mysql.createPool({
host : 'localhost',
user : 'me',
password : '12345',
database : 'A2014',
connectionLimit: 100
});
pool.getConnection(function(err, connection) {
connection.query( 'select count(9) from mytbl;', function(err, rows) {
connection.release();
});
pool.end();
});

bug with angular2-meteor app :failed connection to mysql via meteor

i'm trying to connect to mysql database with meteor using nodets:mysql and i'm facing this error :
Unhandled rejection Error: No infromation can be fetched by your database, please check your permissions
this is my part of code :
Meteor.startup(function() {
//Start of changes
var connectionSettings = {
host: '127.0.0.1',
user: 'root',
password: '',
database: 'test'
};
var db = Mysql.connect(connectionSettings);
})
i need to add the port of mysql
Meteor.startup(function() {
//Start of changes
var connectionSettings = {
host: '127.0.0.1',
port : (mysqlport),
user: 'root',
password: '',
database: 'test'
};
var db = Mysql.connect(connectionSettings);
})

How can I use SSL with a node-mysql pooled connection?

I'm using node-mysql by felixge to make database connections via Node.JS. I'm making lots of connections, so I thought it would be a good idea to use a connection pool:
var mysql = require('mysql');
function createPool() {
var pool = mysql.createPool({
dateStrings : true,
host : '***',
user : '***',
password : '***',
database : '***'
});
return pool;
}
However, while node-mysql makes it very simple to use SSL for a normal connection:
var connection = mysql.createConnection({
host : 'localhost',
ssl : {
ca : fs.readFileSync(__dirname + '/mysql-ca.crt')
}
});
It doesn't seem to show in the documentation that the SSL options are available when utilizing a connection pool.
How can I use SSL with a node-mysql pooled connection? or rather, can it be done at all?
I re-read the docs and noticed this line:
Pools accept all the same options as a connection.
Sorry about that. Anyway, the answer is that it accepts the same options as a normal connection.
var mysql = require('mysql');
function createPool() {
var pool = mysql.createPool({
dateStrings : true,
host : '***',
user : '***',
password : '***',
database : '***',
ssl : {
ca : fs.readFileSync(__dirname + '/mysql-ca.crt')
}
});
return pool;
}