We are using GoDaddy. We want to connect to remote mysql by mysqljs and nodejs. It works on local server but it doesn't work on live server. We are receiving always same error:
Error: connect ETIMEDOUT
Error connecting to Db
Here is my connection code:
var con = mysql.createConnection({
domain: "http://domain.com/",
host: "domain.com",
port: 3306,
user: "username",
password: "password",
database: "databasename"
});
con.connect(function(err){
console.log(err);
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
con.query("SELECT * FROM users WHERE username='"+user+"' and password='"+pass+"' ", function(err, rows) {
if (err) throw err;
if (rows=='') {
alert('Wrong username or password');
}else {
$.each(rows , function(key,value){
if (value.username==user && value.password==pass) {
//window.location =('Home.html');
window.location =('cpanel.html');
}
});
}
});
});
Related
I am creating a facebook chatbot using node js and to store data I used MySQL Database. Currently it is working fine. But, I have a question that, should I need to close the database connection?
I tried to do it but when I close the connection then in the next attempt it is throwing an error that No SQL Connection found
Please tell me the correct way of how to close database connection and reuse it
let databaseConnection = () =>{
let conn = mysql.createConnection({
host: "",
user: "",
password: "",
database: "",
port: ""
});
conn.connect(function (error) {
if (error) {
console.log("Error when connecting to db ",error);
setTimeout(databaseConnection, 2000);
}
else {
console.log("Connected");
}
});
conn.on('error',function(err){
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
databaseConnection();
}else{
throw err;
}
});
return conn;
}
conn = databaseConnection();
for (let i=0;i<10;i++){
query = "SELECT name FROM info where id=?";
value = i
conn.query(query,value, function (err, result, fields) {
if (err) throw err;
console.log(result);
console.log("Data Fetched Successfully.")
})
conn.end()
}
You should end your connection after the loop
for (let i=0;i<10;i++){
query = "SELECT name FROM info where id=?";
value = i
conn.query(query,value, function (err, result, fields) {
if (err) throw err;
console.log(result);
console.log("Data Fetched Successfully.")
})
}
conn.end()
i got the above error
my code was like this
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "username",
password: "password",
socketPath: "/Applications/MAMP/tmp/mysql/mysql.sock"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});
con.end();
i tried to do as they explain in the below link but i did not know how to find my socket path?
enter link description here
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
in my c# I have
IRestResponse response = client.Execute(request);
var content = response.Content;
but the content prints "Internal Error" for all err types
EDIT
Also tried:
pool.getConnection((err, conn) => {
if(err) {
var error = new Error("something is wrong");
callback(error);
}
according to this https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-exceptions.html, still getting Internal Error for all error types
Also, I do get 200 OK on good requests, what I am trying to do is to print an explanatory message to the client in case of an error
I am trying to retrive data from a local SQL Express server from a node.js app, but I get an error saying:
message: 'Login failed for user \'DIR\\maja.okholm\'.',
code: 'ELOGIN' },
name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
I have checked that the local sql server is running and that I have access.
The code in node.js:
router.get('/', function (req, res, next) {
var sql = require("mssql");
// config for your database
var config = {
user: 'DIR\\maja.okholm',
password: '******',
server: 'localhost', //CPX-Q2N4C7MBZ9L\SQLEXPRESS01 localhost (local)\SQLEXPRESS01
database: 'Retracer Pages'
};
// connect to your database
let connection = sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request(connection);
// query to the database and get the records
let result = request.query('select * from Configurations', function (err, recordset) {
if (err) console.log(err)
// send records as a response
//console.log(recordset);
res.send(recordset);
});
});
I tried to changed it to async but without luck.
Any advice is appreciated!
I am having trouble returning results when running queries on my MySQL database using Node.js. Below is an example of my code, which currently outputs nothing to the console.
const mysql = require("mysql");
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'tl'
});
connection.connect();
connection.query = ('SELECT * from employees', function(error, results, fields) {
if (!error) {
console.log(results);
} else {
throw error;
}
});
connection.end();
Authentication is correct.
The database and table are not empty. Running the same query in MySQL itself returns rows.
The code below will give me 'Connection established':
con.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
There is nothing wrong with my DB or table, all is working as expected, apart from running these queries. I've followed the official documentation and others