I'm using node js app with external mysql database. It work properly from localhost and other hosting but on openshift I've got this error Error: connect ECONNREFUSED.
var mysql = require('mysql');
var dbConfig = {
host : 'external.mysql.com',
port : '3306',
user : 'user',
password : 'pass',
database : 'test'
};
var connection = mysql.createConnection(dbConfig);
connection.connect();
connection.query('SELECT * FROM test ', function(err, rows, fields) {
if (err) throw err;
});
connection.end();
I'm also tried telnet external.mysql.com 3306 from openshift and it tell
telnet: connect to address 12.34.56.78: Connection refused
Maybe there is some additional params for external connection in openshift
You should make port forwarding to connect remotely services on openshift platform.
Here is the official tutorial and video.
Related
So i built a frontend where you can fill in a movie name, a review and submit it to a database. Now im trying to connect a mysql database i created to the index.js , so that it gets filled with the first entry. Im trying to accomplish it like this:
const express = require('express');
const app = express();
const mysql = require('mysql');
const db = mysql.createPool({
host: "localhost",
user: "root",
password:"password",
database:'CRUDDatabase',
});
app.get('/', (req, res) => {
const sqlInsert = "INSERT INTO Movie_Reviews(movieName, movieReview) VALUES (1,'inception', 'good movie');"
db.query(sqlInsert, (err, result) =>{
res.send("change done");
});
})
app.listen(3001, () => {
console.log("running on port 3001")
})
But somehow the frontend gets the text ive send "Change done" but the database still doesnt show any entries. Any ideas where my mistake may be? Is it a code mistake or does it have to do with me db configuration. In mysql workbench i just created a default connection without changing anything.
EDIT: The Error seems to be the following:
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
EDIT:
The following comment here solved my problem:
Execute the following query in MYSQL Workbench ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; Where root as your user localhost as your URL and password as your password Then run this query to refresh privileges: flush privileges; Try connecting using node after you do so. If that doesn't work, try it without #'localhost' part.
I think you have an error in your code but you are not showing it as you don't test in err variable, try this code in order to show what error you are getting:
const express = require('express');
const app = express();
const mysql = require('mysql');
const db = mysql.createPool({
host: "localhost",
user: "root",
password:"password",
database:'CRUDDatabase',
});
app.get('/', (req, res) => {
const sqlInsert = "INSERT INTO Movie_Reviews(movieName, movieReview) VALUES (1,'inception', 'good movie');"
db.query(sqlInsert, (err, result) =>{
if(err) {
console.log(err);
res.send(err.toString());
}
res.send("change done");
});
})
app.listen(3001, () => {
console.log("running on port 3001")
})
So as Med Amine Bejaoui pointed out in a comment, the solution is:
Execute the following query in MYSQL Workbench ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; Where root as your user localhost as your URL and password as your password Then run this query to refresh privileges: flush privileges; Try connecting using node after you do so. If that doesn't work, try it without #'localhost' part.
const mysql = require('mysql');
let connection = mysql.createConnection(...);
connection.connect((err)=>{
...
connection.query((err)=>{
...
connection.end();});
});
After I close the connection by using
connection.end()
, if I want to query the database again using the same credentials, do I need to make a new connection by calling
mysql.createConnection(...)
Or can I reuse the same connection by simply calling
connection.connect(...)
A little background: I'm deploying an angular/node.js app to a shared hosting website, and the web host has a maximum limit of 25 concurrent connections to mySQL database, therefore I need to make sure I close a connection properly after a user does his query. I am not sure if I could reuse the connection created by mysql.createConnection(...) after I close that connection, or do I need to create a brand new connection.
You can use one global connection for getting data from db .
If You working on single file than you can write as
app.js one file only
var mysql = require('mysql');
var connection = mysql.createConnection(...);
connection.query('SELECT 1', function (error, results, fields) {
if (error) throw error;
// connected!
});
if you want to use same connection in multiple file than you can write as
app.js
app.use(
connection(mysql, {
host: xxxxx,
user: 'root',
password : xxxx,
port : 3306,
database:dbname
},'pool'),
);
var oem = require('./routes/type');
app.get('/api/oemtype',oem.type);
For the second file
type.js
exports.type = function(req, res){
req.getConnection(function(err,connection){
var query = connection.query('SELECT * FROM type',function(err,rows)
{
if(err)
res.json({
status:0
});
res.send(rows);
res.render('customers',{page_title:"Customers - Node.js",data:rows});
});
});
};
No need to use of connection.end().
i'm trying to connect Mysql with a files java-script by nodejs.
But i want my server apache send my new data automatically to my app android.
For this I have a table in my database who have for name antenne who return the differents values of this table.
I have already do a script php for connect my app android and my database but with a script php isn't a callback server
Indeed i want my server push the data in the app android and not the app android who ask the data to my server
My script java
var express =require('express');
var mysql=require('mysql');
var app=express();
var connect = mysql.createConnection({
//properties...
host: 'localhost',
user:'root',
passeword;'root',
database'Antenne',
});
connection.connect(function(error){
if(!!error){
console.log('Error');
}else{
console.log('Connected');
});
app.get('/',function(req,resp){
//aboutmysql
connection.query("SELECT *FROM antenne"function(error,rows,fields))
if(!!error){
console.log('Error in the query');
}else{
console.log('Sucessful query');
});
app.listen(80);
Using sequel pro I have created a database called test. It has one table called users. In that table there is one user -> id=1, name=Phantom.
I have installed the mysql node module
When I run the code below I get The solution is: undefined.
Can anyone advise how I can connect to database and show the users?
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost:8889',
user : 'root',
password : 'root',
database : 'test'
});
connection.connect();
connection.query('SELECT * from users', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows);
});
connection.end();
It shows the following error :
Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (/Users/fitz035/Desktop/sony/presave/node_modules/mysql/lib/Connection.js:425:13)
I am running the db through MAMP. These are the db settings :
Host: localhost
Port: 8889
User: root
Password: root
Socket: /Applications/MAMP/tmp/mysql/mysql.sock
Node is asynchronous, so connection.end() is likely to happen before your query calls back. Also, specify the port Mysql is running on when non-standard.
try this :
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'test',
port: 8889
});
connection.connect();
connection.query('SELECT * from users', function(err, rows, fields) {
if(err) console.log(err);
console.log('The solution is: ', rows);
connection.end();
});
If it on VPS configured with Firewall, you need to whitelist your IP via SSH. Otherwise it still throws exactly above error even after adding via cPanel's RemoteMYSQl
# csf -a [RemoteIP]
# csf -r
You can do it quickly via WHM too. Just posted as it may help someone.
Check your current MySQL server port and change to:
DB_PORT=3304
I keep getting an error when trying to connect mongoDB. I know there are many questions similar to this one and I have checked all of them and haven't found a solution for my issue.
Here is the exact error:
connection error: { MongoError: connect ECONNREFUSED 127.0.0.1:21017
name: 'MongoError'
message: connect ECONNREFUSED 127.0.0.1:21017
I looked at some other solutions and they say to adjust the mongo.conf file but I can't seem to find the file on my system and I've downloaded MongoDB.
Here is my full code:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
$ = require('cheerio');
/* GET home page. */
mongoose.connect('mongodb://localhost/');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Connected to database');
// we're connected!
});
var pageInfo = {
title: 'JotIT',
owner: 'Emmanuel Obogbaimhe',
message: 'Hi welcome to JotIT. A quick, simple and easy to use note taker.',
date: Date.now(),
age: 22
};
router.get('/', function(req, res, next) {
res.render('index', {
title: pageInfo.title,
author: pageInfo.owner,
message: pageInfo.message,
date: pageInfo.date,
age: pageInfo.age
});
});
module.exports = router;
The reason for getting that kind of error: {MongoError: connect ECONNREFUSED 127.0.0.1:21017}, is that Mongo process is not running on that PORT, or not running at all.
For first check out if mongo process is running:
service mongod status //for Linux machine
For second - check the port of mongo process:
nmap -p- localhost //for Linux machine
For windows, open another terminal and cd into the apps root directory. Then, run $ mongod.exe. I would recommend placing the following into a test.js file:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test.js');
var db = mongoose.connection;
db.on("error", function(error){
console.error("Connection error : " + error)
});
db.once('open', function() {
console.log('Connected to database');
db.close(function(){
console.log("db connection closed");
});
});
Go back to the original terminal and run $ node test.js
I had the same error, It looks like and bad closing in preview mongodb session I just did and It worked out fine
sudo service mongod stop
sudo service mongod start
Had the same problem, caused by running out of hard drive memory space. This caused mongod to keep crashing after restarting it.
Simply increasing memory space of the server, and restarting mongod (either manually or via reboot when service restarts automatically) solved the issue.
Try this
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
$ = require('cheerio');
/* GET home page. */
mongoose.connect('mongodb://localhost:27017/db-dev');
or try with
mongoose.connect('mongodb://0.0.0.0:27017/db-dev');
change your mongoose connection code to:
mongoose.connect('mongodb://localhost:27017/yourdb');
This error can be solved by replacing localhost in the following code with the address of localhost which is 127.0.0.1.
mongoose.connect('mongodb://localhost/');
Try this -> mongoose.connect('mongodb://127.0.0.1/');