I have a connection between Node and MySQL with a external database:
const connectionDB = mysql.createConnection({
host: '178.129.145.252',
port: 3306,
user: '',
password: '',
database: '*****'
})
when I execute my script to start it runs and I can view the users in a table, but later it crashes
enter image description here anyone knows how can I connect with the external database?
Thanks
Your code does not properly handle connection loss to MySQL server.
A quick solution would be to use connection pool.
var db_config = {
host: 'localhost',
user: 'username',
password: 'password',
database: 'example'
};
let pool = mysql.createPool(dbConfig);
pool.on('connection', function (conn) {
if (conn) {
logger.info('Connected');
}
});
// DO SOMETHING HERE
// pool.query('QUERY GOES HERE', (err, rows) => { /* Handle query response */});
Related
I'm using the mysqldump library, and mysql-import. I need to do a restore of my MySQL database, but at the time of doing it, it tells me that you cannot add duplicate files, therefore I manually put DROP TABLE IF EXIST, and it worked and overwritten the database, according to In the Mysqldump documentation there is a way to add the DROP TABLE by default, but I really don't know how to do it, can someone help me?
var mysqldump = require('mysqldump');
const controller = {};
controller.backupDatabase = function(req, res, next) {
if(mysqldump){
mysqldump({
connection: {
host: 'localhost',
user: 'root',
password: '',
database: 'decoracionesalves',
},
dumpToFile: './DecoracionesAlves.sql',
});
}else{
//Hacer algo aqui.
}
};
module.exports = controller;
You can set dropIfExists to true on the schema dump table option.
mysqldump({
connection: {
host: 'localhost',
user: 'root',
password: '',
database: 'decoracionesalves',
},
dump: { schema: { table: { dropIfExist: true } } },
dumpToFile: './DecoracionesAlves.sql',
});
See API docs for SchemaDumpOptions
I am using couchDB and when ever i try to create views it is giving me this error
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 'EUNAUTHORIZED',
body: {
error: 'unauthorized',
reason: 'You are not a db or server admin.'
}
}
I am using Node-Couchdb and i am passing the credentials like this
const NodeCouchDb = require('node-couchdb')
require("dotenv-flow").config();
const couch = new NodeCouchDb({
host: process.env.DB_HOST,
protocol: process.env.DB_PROTOCOL,
port: process.env.DB_PORT
})
const couchAuth = new NodeCouchDb({
auth: {
user: process.env.DB_USER_NAME,
pass: process.env.PASSWORD
}
})
module.exports = {
couch
}
Your code is creating two instances of NodeCouchDB, couch and couchAuth where
couch points to server specified by envars without credentials
couchAuth points to the default server (127.0.0.1:5984) with credentials specified by envars
You need to combine parameters, for example
const NodeCouchDb = require("node-couchdb");
const couch = new NodeCouchDb({
host: process.env.DB_HOST,
protocol: process.env.DB_PROTOCOL,
port: process.env.DB_PORT,
auth: {
user: process.env.DB_USER_NAME,
pass: process.env.PASSWORD,
},
});
module.exports = {
couch
};
I am building lambda function using visual studio code SAM option. I am trying to connect to RDS instance (MYSQL) which is in VPC network.
I tried connecting using following code. I am not getting any error but it is not connecting to DB. I have searched every where but didn't get any solution. I tried following but didn't worked
const fs = require('fs');
const mysqlssh = require('mysql-ssh');
mysqlssh.connect(
{
host: 'XXX.XXX.XX.XX',
user: 'ec2-user',
privateKey: fs.readFileSync('./XXXX-txlarge.pem')
},
{
host: '-staging-instanceXXXXX.rds.amazonaws.com',
user: 'user',
password: 'password',
database: 'db'
}
)
.then(client => {
client.query('SELECT * FROM users', function (err, results, fields) {
if (err) throw err
console.log(results);
mysqlssh.close()
})
})
Instead of using mysql-ssh which is tunneling based type of sql connection module try to use simple mysql module,
const fs = require('fs');
const mysql = require('mysql');
let connection = mysql.createConnection(
{
host: 'XXX.XXX.XX.XX-staging-instanceXXXXX.rds.amazonaws.com',
user: 'user',
password: 'password',
database: 'db'
}
);
connection.connect();
connection.query('SELECT * FROM users', function (err, results, fields) {
if (err) throw err
console.log("rows: " + rows);
context.succeed('Success');
});
PS: You might need to some of code as per you needs, but this is what you should try doing.
must your help!!!
it throw me
Error: connect ECONNREFUSED 10.166.186.156:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14)
here my code :
const express = require("express");
const mysql = require("mysql");
const app = express();
const db = mysql.createConnection({
host: "DESKTOP-2QNN9FA",
user: "DuduLog",
password: "1234",
database: "testDB"
});
db.connect(err => {
if (err) {
console.log(err);
} else console.log("MySql Connected...");
});
app.listen("3000", () => {
console.log("Server started on port 3000");
});
what worng with it? thanks a lot
try use component mysql-ssh.
const connect = mysqlssh.connect({
host: 'xxxxxx',
user: 'xxxxxxx',
password: 'xxxxxx',
port: xx
},
{
host: '127.0.0.1',
user: 'xxxxx',
password: 'xxxxx',
database: 'xxxxxx',
port: 'xxxx'
})
if you use firebase functions you need to configure the panel with a payment method that supports connections to other servers
As a team, we are using node.js to setup the database connection with Mysql in Cpanel.
I did not make the database, but I know the host, user,password, database name, and port. However, in app.js, although I put all the code needed to connect to the database, it fails all the time.
Here is my code in app.js.
var mysql = require('mysql');
// creating a connection to the db
var con = mysql.createConnection({
host: 'localhost',
user: 'myUsername',
password: 'myPassword',
database: 'myDatabase',
port:3306
});
con.connect(function(err) {
if(err){
console.log('Error connecting to DB');
return;
}
console.log('Connection established');
});
Can you show the error?
By the way, you dont need to do a con.connect.
Even you use the var con = mysql.createConnection, con is a mysql resource.
Simple do this:
var mysql = require('mysql');
// creating a connection to the db
var con = mysql.createConnection({
host: 'localhost',
user: 'myUsername',
password: 'myPassword',
database: 'myDatabase',
port:3306
});
con.query('SELECT * FROM table', function(err, results) {
if(err){
console.log('Query error: ', err);
return;
}
console.log(results);
});