i got Node.js MySQL - Error: connect ECONNREFUSED - javascript

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

Related

running nodejs and mysql, how can I parse the result in client

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

RethinkDB Unhandled Rejection No stack trace

I'm using RethinkDB with Rethinkdbdash (node.js) and for a few days now Im getting this error:
Unhandled rejection (<[{"entries":3,"id":1357186,"item":{"co...>, no stack trace)
Doesn't matter what query do I run, when i try to get any info from the database I always get the same error. If nothing is returned, error looks like this:
Unhandled rejection (<(empty array)>, no stack trace)
This is my current code:
r.table('example').run().then(function(err, result){
if(err) throw err;
console.log(result);
})
You would need to include the database: 'databasename' field in the config object and pass the config object into run, otherwise you could try to specify the db in your ReQL query.
Specify db for connection:
let connection = null
r.connect( { host: 'localhost', port: 28015, user: 'user', password: 'password', database: 'databasename'}, function(err, conn) {
if (err) throw err
else {
connection = conn
r.table('example').run(connection, function(err, result){
if(err) throw err;
else console.log(`${JSON.stringify(result)}`);
})
}
})
Specify db in query:
let connection = null
r.connect( { host: 'localhost', port: 28015, user: 'user', password: 'password'}, function(err, conn) {
if (err) throw err
else {
connection = conn
r.db('databasename').table('example').run(connection, function(err, result){
if(err) throw err;
else console.log(`${JSON.stringify(result)}`);
})
}
})

How to make DBconnection and Node.js Query file separately

I have question. How to connect dbconnection.js and demo_api_select.js I have a problem when declare variable on demo_api_select.js. It doesn't work and have error notification like this:
Error Notification
Please help
dbconnection.js:
var mysql=require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
module.exports = con;
demo_api_select.js
var db = require('./dbconnection');
db.connect(function(err) {
if (err) throw err;
//Select all customers and return the result object:
con.query("SELECT * FROM profil" , function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
I want to make those code work as well as this code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
con.connect(function(err) {
if (err) throw err;
//Select all customers and return the result object:
con.query("SELECT * FROM profil" , function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
Try this code.No need to connect twice to database
var db = require('./dbconnection');
db.query("SELECT * FROM profil" , function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});

How do you pass and execute a mysql query?

I was wondering how to execute a mysql query so that the client can see like "users registered: number"
Server.js:
var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'kitsune',
connectionLimit: 50,
port: 3306 });
connection.connect(function(err) {
if (err) throw err;
});
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
module.exports = connection;
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile('index.html', 'utf-8', function(err, content) {
if (err) {
res.end('error occurred');
return;
}
var countQuery = "SELECT Count(ID) AS NumberOfPenguins FROM Penguins"; console.log(countQuery);
connection.query(countQuery, function(err, rows) {
if (err) throw err;
console.log('Connected!');
});
var renderedHtml = ejs.render(content, {countQuery: countQuery});
res.end(renderedHtml);
});
}).listen(80);
index.html:
<html>
<head>
</head>
<body>
Users registered: <%= countQuery %>
</body>
</html>
This only shows the query but it does not execute it. Any idea on how to execute it?
You executed the query sure but you never did anything with the results. The result is the rows passed in the callback function.
connection.query(countQuery, function(err, rows) {
if (err) throw err;
var renderedHtml = ejs.render(content, {countQuery: rows});
res.end(renderedHtml);
});

NodeJS - MySQL remote mysql connection timeout error

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');
}
});
}
});
});

Categories