Connection to Mysql from NodeJS on Heroku server - mysql

ANy idea how can I connect from NodeJs to the Mysql ClearDB on Heroku?
I was able to connect to the ClearDB Mysql runing on Heroku from Navicat, I even created a table called t_users. But I'm having problems connecting from NodeJs from Heroku.
This is my code, it is very simple: everything works find until it tries to connect to MySQL
web.js:
var express = require("express");
var app = express();
app.use(express.logger());
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'us-cdbr-east-04.cleardb.com',
user : '',
password : '',
database : ''
});
connection.connect();
app.get('/', function(request, response) {
response.send('Hello World!!!! HOLA MUNDOOOOO!!!');
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
response.send('The solution is: ', rows[0].solution);
});
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
This is what I got when I run on the command line: Heroku config
C:\wamp\www\Nodejs_MySql_Heroku>heroku config
=== frozen-wave-8356 Config Vars
CLEARDB_DATABASE_URL: mysql://b32fa719432e40:87de815a#us-cdbr-east-04.cleardb.com/heroku_28437b49d76cc53?reconnect=true
PATH: bin:node_modules/.bin:/usr/local/bin:/usr/bin:/bin
This is the LOG:
http://d.pr/i/cExL
ANy idea how can I connect from NodeJs to the Mysql ClearDB on Heroku?
Thanks

Try this. Hope this will help you
mysql://b32fa719432e40:87de815a#us-cdbr-east-04.cleardb.com/heroku_28437b49d76cc53?reconnect=true
var connection = mysql.createConnection({
host : 'us-cdbr-east-04.cleardb.com',
user : 'b32fa719432e40',
password : '87de815a',
database : 'heroku_28437b49d76cc53'
});
Use this details and connect it in mySQL work bench, and import your localhost db

The base code was ok, I missed some NodeJS code.
I did a video explaining how to connect to MySqlusing NodeJS on a Heroku server, take a look:
http://www.youtube.com/watch?v=2OGHdii_42s
This is the code in case you want to see:
https://github.com/mescalito/MySql-NodeJS-Heroku

createConnections accepts config as well as connectionString.
export function createConnection(connectionUri: string | ConnectionConfig): Connection;
So below solution would work.
var connection = mysql.createConnection('mysql://b32fa719432e40:87de815a#us-cdbr-east-04.cleardb.com/heroku_28437b49d76cc53?reconnect=true);
or you can set the connection url in environment variable DATABASE_URL
var connection = mysql.createConnection(process.env.DATABASE_URL);

One should need to use pool connection for database connection to handle better mysql conncurrent request as follows
var pool = mysql.createPool({
connectionLimit : 100,
host : 'us-cdbr-iron-east-05.cleardb.net',
user : 'b5837b0f1d3d06',
password : '9d9ae3d5',
database : 'heroku_db89e2842543609',
debug : 'false'
});
It just because pool maintain the connection.release() on its own , so you do not have to bother where you should need to release the connection.
On the other hand you should need to provide user, password and database name as i mentioned in my code.
When u get provisioned from heroku then you get a cleardb database name.
on clicking clear db database button you will find username and password.
and to get database url you have to run the following commands in terminal as follows;
heroku login
heroku app -all (it shows the app list name )
heroku config --app appname (it will provide the database url).
Rest you would need to worry, just add all dependency into your package.json before pushing to heroku master.
After then you will have no problem on deploying nodejs application to heroku server.

Looking at the log timestamps, it seems like your connections are timing out. Either create a wrapper for 'getConnection' that checks the connection health and re-establishes if necessary, or try to use the mysql Connection Pool feature, which can do that for you.

Related

Connecting to a remote MySQL database from Stackblitz Node.js project

I have a Node.js Stackblitz project that I am trying to connect to a remote MySQL database. It is not possible to have a MySQL database within Stackblitz, hence trying the remote approach. However I get "Error: connect ETIMEDOUT" whenever I attempt a connection. Any help or pointers much appreciated.
I am using the code below. The remote database is accessible with the credentials I am using and returning data when used outside of Stackblitz. Is remote database access not possible with Stackblitz or am I missing something?
const express = require('express');
const mysql = require('mysql2/promise');
const app = express();
const port = 3010;
const path = require('path');
app.use(express.static('static'));
app.get('/', async function (req, res) {
try {
// create connection
const connection = await mysql.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
database: process.env.DB_DATABASE,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
});
// query database
const [rows, fields] = await connection.execute('SELECT * FROM `user`');
res.send({
rows,
fields,
});
} catch (err) {
console.log('err:', err);
}
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
For anyone looking for an explanation.
Currently a mysql database cannot reside in StackBlitz. Additionally, neither can it connect to an external mysql database on its own either.
You therefore required a backend API outside of StackBlitz to make the database calls, negating the reason for building the backend on StackBlitz in the first place (IMO). Therefore, the suggested setup currently would be a localhost backend API accessed from a frontend in StackBlitz via an ngrok secure address.
StackBlitz are working on getting basic server requests running in the future. So, watch this space.
I hope this answer helps save others time.

How to connect to my remote MySQL database from my Heroku NodeJS app?

I have a NodeJs backend hosted on Heroku. I am trying to connect to my remote MySQL database that is hosted under my Hostwinds plan. When I tried to connect to the DB in my local environment it worked perfectly, but when I deployed my app to Heroku I get an error saying Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'myUserName'#'someRandomIP' (using password: YES).
I have added my Heroku URL to my remote access pannel in cPanel, but that didn't give the user access. I have also tried to add the someRandomIP to the remote access pannel in cPanel, but the weird thing about that IP is that is changes on every deploy/re-deploy. I am positive that my connection string is correct, but just in case: mysql://username:password#ipOfHost/dbName.
Here is my serverside code:
const express = require("express");
const app = express();
const cors = require("cors");
const mysql = require("MySQL");
const { resolve } = require("path");
const stripe = require("stripe")(process.env.STRIPE_PRIVATE_API_KEY);
// enable CORS
app.use(cors({
origin: 'http://localhost:3000'
}))
// form connection object
var connection = mysql.createConnection(process.env.DATABASE_URL);
// connect to the database
connection.connect(function(err) {
if (err) throw err;
console.log("You are connected to the database");
})
app.listen(4242, () => console.log('Node server listening on oneline-backend.herokuapp.com'));
Here is a link to the full error in the Heroku Console: https://i.imgur.com/QXfEVCW.png
Any help would be appreciated! This has caused quite a bit of trouble.

How can I handle MySQL disconnection on NodeJS?

First of all, I'm a beginner on NodeJS. Well, I'm using a shared hosting to my project and when the database reaches 1 minute of inactivity, NodeJS crashes and disconnects me from MySQL. Since I'm using a shared hosting, I can't edit the idle time on the MySQL config and I'll need to handle it in code.
I'm using module.exports to handle my connection, as shown below. So how can I make an auto-reconnection script to take care of my issue? Thank you.
var mysql = require('mysql');
module.exports =
{
handle: null,
connect: function(call){
this.handle = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'test',
timezone: 'utc',
charset : 'utf8'
});
this.handle.connect(function (err) {
if(err) {
console.log("[MySQL] Connection error: " + err.code);
} else {
console.log("[MySQL] Successfully connected");
}
});
}
};
The node mysql module that you are using also has a connection pooling mechanism.
Check out the docs at https://github.com/mysqljs/mysql#pooling-connections
Connection pools will make you task easier. You can then store the connection pool object and use its getConnection method to obtain a connection. Make sure that you release the connection when you are done with it.
If for some reason you cant use connection pooling then you will have to listen for error event on the connection and handle it accordingly. But I strongly recommend that you use connection pool.

how to fix the error of connecting database with nodejs in linux?

I am trying to connect mysql with nodejs, but there is a problem in my code, please help!
var express = require("express");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'admin',
database : 'dbname',
});
var app = express();
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... \n\n");
} else {
console.log("Error connecting database ... \n\n");
}
});
app.listen(3000);
in terminal i am getting this output:
voldemort#Lenovo:~/Documents/cloudprint$ node server.js
Error connecting database ...
The MySQL client establishes a connection to the server using secure authentication by default.
insecureAuth: Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false) 1
Looking at the error traceback in your comment, it looks like your server isn't currently configured to allow secure authentication. 2
If your MySQL server version is <= 5.6.4 then secure auth is off and you may want to enable the old auth protocol by enabling insecureAuth when making the connection in the client.
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'admin',
database : 'dbname',
insecureAuth: true
});
However, I advise updating the passwords for your users to use the more secure password hashing function if your MySQL server version is >= 5.6.5.
There is step-by-step documentation to accomplish this for user accounts.
You may also want to update your MySQL server if you find that you're running an older version that doesn't support the secure auth protocol.

while using MySql, express, angular, nodejs, will the ports be different for express and node

While looking into express + node + mysql, tutorials suggest to start express server by using node server.js (here server.js contains logic to connect to mysql db), which calls upon app.listen() and angular+node tutorials start node server using npm start.
Are they different and I have to start both?
In case, yes, do I need to use diff ports?
In case, no, how to call upon mysql db? Please give some reference or elaborate.
server.js is as follows:
var express = require("express");
var mysql = require("mysql");
var app = express();
var connection = mysql.createConnection({
host : "localhost",
user : "userid",
password : "password",
database : "sakila"
});
connection.connect(function(error){
if(error) {
console.log("Problem with MySQL"+error);
} else {
console.log("Connected with Database");
}
});
app.use(express.static(__dirname + '/bower_components/angular'));
app.get('/app/#/actors_load',function(req,res){
connection.query("SELECT * from actor",function(err,rows){
if(err) {
console.log("Problem with MySQL"+err);
} else {
res.end(JSON.stringify(rows));
}
});
});
app.listen(8000,function(){
console.log("It's Started on PORT 8000");
});
My phonecatapp has following routers:
phonecatApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/phones', {
templateUrl: 'partials/phone-list.html',
controller: 'PhoneListCtrl'
}).
when('/phones/:phoneId', {
templateUrl: 'partials/phone-detail.html',
controller: 'PhoneDetailCtrl'
}).
when('/actors_load', {
templateUrl: 'partials/actors.html',
controller: 'ActorsCtrl'
}).
otherwise({
redirectTo: '/phones'
});
}]);
and controller looks like this:
phonecatControllers.controller('ActorsCtrl',function($scope, $http, $interval){
load_names();
console.log("ActorsCtrl");
/*
$interval(function(){
load_names();
},300);
*/
function load_names(){
$http.get('http://localhost:8000/actors_load').success(function(data){
console.log("kdfhg"+data);
$scope.actors=data;
});
};
});
the partials of html has a link to populate the db data as:
<div>actors</div>
npm install doesn't start a server. It is a command to install packages.
In this particular example the createServer function will accept connection on port 8000. it's taken from node.js docs.
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
As soon as the above code is executed the server is ON. express is a framework for node.js. express creates server on the top of node.js so the express configuration (along with port number) will be executed by node.js. Specified port of express server will run an instance of node.js server, that's the end of the story
To connect to mysql you can use one of the mysql packages for node e.g.:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : '< MySQL username >',
password : '< MySQL password >',
database : '<your database name>'
});
connection.connect();
In above case you are using localhost as a adress of your database.
You can find plethora of mysql packages on npm site: https://www.npmjs.com/search?q=mysql
You may also use mysql.createClient for costumised connection:
var client = mysql.createClient({
user: '??',
password: '??',
port: 8889
});
Note: remember to require your packages otherwise they just simply won't work.
After doing trial and error, I found that srever.js is on server side, angular part of the project is on client side. from angular part, only way to recieve data is using REST API. This role of providing REST API will be facilitated by server.js and hence shall be run on diff port. It means I had to call npm start as well as node server.js. Then I was facing CORS issue, which I resolved by providing
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header ("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
next();
});
in server.js. NOte that to see the effect of this code, you need to start the two servers. This was really a pain point for me. I spent whole day trying various combinations in various places to resolve CORS issue. It seemed not to be working. For some reasons, I stopped the servers and restarted, it was working. After few trials, I realized that even after removing above code, it was working. So, I stopped servers again and this time it was not working, that is how I realized it.
One more thing that I was mingling the two thinking them as one project having client and server side. Any newbie, please understand that client side accesses server side as REST API, so develop them as separate module/project. Do not develop as single module/project.