Reputation: 15725
I have a node js app with express which is deployed on openshift. I have created databases via phpmyadmin 4.0 cartridge. I am able to connect to the database but when I make any query it throws error ECONNREFUSED.
I got this error as a response on my browser which is as follows,
{"status":false,"error":{"code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","fatal":true}}
the code for my app is like this,
var connection = mysql.createConnection({
host : process.env.OPENSHIFT_MYSQL_DB_HOST,
user : process.env.OPENSHIFT_MYSQL_DB_USERNAME,
password : process.env.OPENSHIFT_MYSQL_DB_PASSWORD,
database : process.env.OPENSHIFT_GEAR_NAME
});
connection.connect();
console.log("connected to login database");
var strQuery="insert into login values('"+req.body.uname+"','"+req.body.password+"','"+req.body.name+"','"+req.body.mobile+"');";
connection.query( strQuery, function(err){
if(err) {
res.json({
"status":false,
"error":err
});
res.end();
}else{
res.json({"status":true });
res.end();
}
});
connection.end();
I found an answer here connect ECONNREFUSED - node js , sql
but I can only access my database through phpmyadmin 4.0, How do I solve this ?
Upvotes: 1
Views: 1045
Reputation: 15725
I was only adding the host name, now I added the port
var connection = mysql.createConnection({
host : process.env.OPENSHIFT_MYSQL_DB_HOST,
port :process.env.OPENSHIFT_MYSQL_DB_PORT,
user : process.env.OPENSHIFT_MYSQL_DB_USERNAME,
password : process.env.OPENSHIFT_MYSQL_DB_PASSWORD,
database : process.env.OPENSHIFT_GEAR_NAME
});
and a restart worked for me.
Upvotes: 1
Reputation: 450
EDIT: The answer below is not entirely valid, as restarting the application solved the issue, but my hope is that the below may help someone else with a similar issue.
Goto your Application console, click the nodejs application, add the mysql cartridge there. Once you do you should get access to the vars. I suspect you created a separate application for mysql, rather than adding it to the existing node one.
I was able to print all the vars out by creating a simple express route in the default nodejs application cartridge:
self.routes['/vars'] = function(req, res) {
var html = "<html><body>Host: " + process.env.OPENSHIFT_MYSQL_DB_HOST + "<br />";
html += "Port: " + process.env.OPENSHIFT_MYSQL_DB_PORT + "<br />";
html += "User: " + process.env.OPENSHIFT_MYSQL_DB_USERNAME + "<br />";
html += "Pass: " + process.env.OPENSHIFT_MYSQL_DB_PASSWORD + "<br />";
html += "Sock: " + process.env.OPENSHIFT_MYSQL_DB_SOCK + "<br />";
html += "URL: " + process.env.OPENSHIFT_MYSQL_DB_URL + "<br />";
html += "</body></html>"
res.send(html);
}
Hitting http://<openshift_application_url>/vars
returned (this will be different for you):
Host: 127.6.20.2
Port: 3306
User: adminPAkSHvw
Pass: d7DX9ATPh4uG
Sock: undefined
URL: mysql://adminPAkSHvw:[email protected]:3306/
The complete server.js
is here in case you want to try just copy/paste. If the MySQL cartridge exists within your node+express app, you'll get the vars printed out and then your code should work.
Upvotes: 3