non-Google MySQL database connection with firebase cloud function - mysql

When i try to connect to my MySQL, server sends me an error Error: connect ETIMEDOUT
Here is sample code
const mysql = require('mysql');
const mysqlConfig = {
connectionLimit: 1,
host: "remote_host_ip",
user: "server_user",
password: "server_pass",
database: "server_db",
port: 3306
};
mysql.createConnection(mysqlConfig).connect(function (err) {
if (!err) {
console.log("Database is connected");
} else {
console.log("Database is not connected " + err);
}
});
I am on firebase blaze(pay as you go) plan.

I know the question title already states it, but you are not using GCP Cloud SQL, right?
In that case, there's a great possibility of your MySQL server is not reachable from Cloud Functions. Are you sure network connectivity is OK?
Besides, even in Cloud Functions, it's a good idea to use connection pools. Consider using it, it will be something like that:
var config = {
user: 'root',
password: 'akdaskdasdaE',
database: 'database1'
}
config.connectionLimit = 10
config.multipleStatements = true
// needed in GCP Cloud SQL, but it seems it's not your case
// config.socketPath = `/cloudsql/__INSTANCE_CONNECTION_NAME__`
var connectionPool = mysql.createPool(config)
connectionPool.on('connection', () => {
console.log(`[connectionPool] new connection opened`)
})
// then you use connectionPool.getConnection(..)

Related

aws rds proxy throws timeout error from nodejs12.x

I'm getting a connection timeout when I try to connect to mysql rds proxy. I'm followed this tutorial
This is my code
import mysql2 from 'mysql2';
import AWS from 'aws-sdk';
const getConnection = async () => {
const signer = new AWS.RDS.Signer({
username: 'my-user-name',
hostname: 'proxy-name.proxy-someid.us-east-1.rds.amazonaws.com',
port: 3306
});
console.info('Connecting to MySQL proxy via IAM authentication');
const rdsSignerAuth = () => () => {
console.info('CALL rdsSignerAuth');
return signer.getAuthToken({
username: 'my-user-name',
region: 'us-east-1',
hostname: 'proxy-name.proxy-someid.us-east-1.rds.amazonaws.com',
port: 3306
});
};
let connection;
try {
connection = await mysql2.createConnection({
host: 'proxy-name.proxy-someid.us-east-1.rds.amazonaws.com',
user: 'my-user-name',
database: 'database-name',
connectTimeout: 60000,
ssl: { rejectUnauthorized: false },
authPlugins: { mysql_clear_password: rdsSignerAuth },
});
console.info('Connected');
}
catch (e) {
console.error(`MySQL connection error: ${e}`);
throw e;
}
return connection;
};
const mysql2Impl = async () => {
const connection = await getConnection();
//console.info({ type: 'connection', connection });
const result = await connection.promise().query('select * from destiny;');
console.info({ type: 'result', result });
};
export async function testRdsProxy(event, context){
console.info(JSON.stringify({ event, context }));
await mysql2Impl();
return 200;
}
And this is the response
Error {
code: 'ETIMEDOUT',
errno: undefined,
message: 'connect ETIMEDOUT',
sqlState: undefined,
}
I already checked that my lambda function has a policy "rds-db:connect" to "*" resource. Besides, I checked that my proxy is in the same VPC and subnet that my rds db. The secret that holds the credentials to RDS is ok. What I am doing wrong?
The doc states that the RDS proxy cannot be accessed public, so your lambda function need to be in the same security group with the rds proxy.
Please aware that when you make your lambda into a vpc, your lambda may lost its ability to access internet.
Thank you.
You can connect RDS proxy even outside VPC by doing VPC peering from same or different account. I did it for one of the project
If you pass IAM certification
check the user-name(mysql user) has execute [INVOKE LAMBDA] permission
If IAM authentication fails
you should let the proxy setup wizard automatically create an IAM like below
Connectivity > IAM role > Create IAM role
                     > IAM authentication > Required

Node MySQL Connection Pool - wait for database to start

How do I check if a MySQL database is ready for some queries from a Node MySQL Connection Pool?
I have a Docker environment consisting of thee containers:
container 1: web server
container 2: api
container 3: database
The database container runs a MySQL database. The api container connects to that database. All three containers are started at the same time. The web server container is up after 0,5s. The api container is up after 2s. The database server is up after 20s.
Currently, the api tries to access the tables of the database before the database is up and running. This leads to errors like connection refused. The following code segment always ends up with the message "Error querying database!" when the MySQL database is not yet up:
const sql: string = 'SELECT * FROM sometable;';
MySQL.createPool({
connectionLimit: 10,
acquireTimeout: 30000,
waitForConnections: true,
database: 'mydatabase',
host: 'localhost',
multipleStatements: true,
password: 'mypassword',
user: 'root',
}).query(sql, (err, result) => {
if (result) {
console.log('Successfully queried database.');
} else {
console.log('Error querying database!');
}
});
Versions in use:
OS: Ubuntu 19.10
Node: v13.6.0
MySQL (Node API): "#types/mysql": "2.15.8" and "mysql": "2.17.1"
MySQL (Docker Database): mysql:5.7.28
TypeScript: 3.7.4
I would like to check (and wait) the database readiness out of the api, possibly using the Connection Pool I use for queries. Is that possible?
Retry to connect with setTimeout():
(answer in Javascript rather than typescript)
'use strict';
const dbpool = require('mysql').createPool({
connectionLimit: 10,
acquireTimeout: 30000,
waitForConnections: true,
database: 'mydatabase',
host: 'localhost',
multipleStatements: true,
password: 'mypassword',
user: 'root',
});
const sql = 'SELECT * FROM sometable;';
const attemptConnection = () =>
dbpool.getConnection((err, connection) => {
if (err) {
console.log('error connecting. retrying in 1 sec');
setTimeout(attemptConnection, 1000);
} else {
connection.query(sql, (errQuery, results) => {
connection.release();
if (errQuery) {
console.log('Error querying database!');
} else {
console.log('Successfully queried database.');
}
});
}
});
attemptConnection();
Here is my test run:
$ sudo service mysql stop; node test.js & sudo service mysql start
[1] 24737
error connecting. retrying in 1 sec
error connecting. retrying in 1 sec
$ Successfully queried database.
FYI, The program never ends because it needs dbpool.end();
Your API should try to connect to the database with a timeout and a certain threshold of connection attempts. However, there are readily available solutions for this scenario.
Try using wait-for-mysql module.
waitForMy = require 'wait-for-mysql'
config =
username: user
password: pass
quiet: true
query: 'SELECT 1'
waitForMy.wait(config)
Here you have a variation but with no need to mysql pooling. I'm using this on my server and it does work:
const mysql = require('mysql')
var db // global, to use later to db.query
var dbInfo = {
host : 'example.org',
database : 'some_database',
user : 'bob',
password : 'secret'
}
function connectToDb(callback) {
const attemptConnection = () => {
console.log('Attempting to connect to db')
dbInfo.connectTimeout = 2000 // same as setTimeout to avoid server overload
db = mysql.createConnection(dbInfo)
db.connect(function (err) {
if (err) {
console.log('Error connecting to database, try again in 1 sec...')
db.destroy() // end immediately failed connection before creating new one
setTimeout(attemptConnection, 2000)
} else {
callback()
}
})
}
attemptConnection()
}
// now you simply call it with normal callback
connectToDb( () => {
console.log('Connection successfully')
// do some queries
db.query(..)
})

Can't connect to mysql (express) with Node.js

Hello Stacksoverflowers,
I have a problem to connect my server.js file to Mysql via Express.
I've already insatlled mysql and express via npm.
when i run my server.js
The only thing it says in my console (MacOS default terminal) is:
"node server running now - using http://localhost:3000"
(I got no errors)
and my webpage also showing correct - "Node Server running - startpage" in the browser.
The problem is that they say nothing like "Connected to the MySQL server" or "Timeout: error messages" ?? (look at #3)
// Dan k
//#1
// My codes from server.js
const express = require('express');
const mysql = require('mysql');
const hostname = "localhost:";
const port = 3000;
//#2
// create connection to DB
const connection = mysql.createConnection({
host: "localhost",
user: 'root',
password: 'root',
port: 3000
});
//#3
// Connect to DB
connection.connect(function(err) {
if (!err) {
console.log('Connected to the MySQL server.');
}
else if (err)
{
return console.error('Timeout: ' + err.message);
}
});
// Routing with Express
const app = express();
//#4
// Startpage (root)
app.get('/', function(request, response){
response.send('Node Server running - startpage...');
response.end();
})
connection.end();
//#5
app.listen(port, (err) => {
if(err) throw err;
console.log("node server running now - using http://"+(hostname)+ .
(port)");
});
I got what is causing the issue. You are using the same port number 3000 to connect to your mysqlDB and at the same time using the same port to run your nodejs application.
This does throw an error but it takes a really long time to throw the error.
So the simple answer here is that the port that you are mentioning in your createConnection() method is incorrect and needs to be changed. This port should be port number on which your mysql DB is running. From the error it is clear that your mysql DB is not running on port 3306. You can check this post to figure out which port is your mysql DB running on. https://serverfault.com/questions/116100/how-to-check-what-port-mysql-is-running-on. Below is a reference for you. Please see the comments.
// create connection to DB
const connection = mysql.createConnection({
host: "localhost",
user: 'root',
password: 'root',
port: 3000 // You need to change this and put in the correct port number.
});

Connect from IBM Cloud Functions to MySQL on local host

I want to connect to my MySQL database on a local host which is not in IBM Cloud using IBM Cloud Functions. I am unable to do that.
I have written Node.js code in IBM Cloud Function.
var mysql = require('mysql');
//,cn="server=localhost;port=3306;database=chatbot;uid=root;password=oracle1#;";
var connection = mysql.createConnection({
server: 'localhost',
user: 'root',
port:3306,
password: 'my_password'
});
function main(params) {
try {
//var connection=mysql.createConnection(cn);
connection.connect();
//var s = JSON.stringify(params['user_input']);
//var v = s.substring(1,11);
//var check= conn.querySync("select count(distinct PHONE_NUMBER) where PHONE_NUMBER='"+v+"'");
var rows = connection.query(
"select * from chatbot.customer_data");
//console.log(rows);
connection.end();
return{message:"TRUE:"+rows[0]['PHONE']};
}
catch (e) {
return { message:"error" };
//return{message:"FALSE"};
}
}
Expected result:
TRUE:RESULTSET
Actual Result:
error
This is a pure network issue. The Cloud function running in IBM Cloud does not have a network route that directs to your local host. If you did want that connectivity then you would need to expose the address of your local host using something like a secure gateway service.
var sql = require('mysql');
var connection = sql.createConnection({
host: 'host name',
user: 'user name',
password: 'passwprd',
database: 'database_name'
});
function main(params) {
try {
connection.connect();
var sql="select * from table _name";
connection.query(sql,function(err,result){
return{message:result};
})
//console.log(rows);
connection.end();
}
catch (e) {
return { message:"error" };
//return{message:"FALSE"};
}
}

Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433

I'm trying to connect to my local MySql server from node.js/express application using mssql with following config:
// Database connection
var sql = require('mssql');
var config = {
user: 'db_user',
password: 'db_pass',
server: 'localhost',
database: 'project_db'
};
sql.connect(config, function (err) {
if (err) {
console.log(err);
}
});
I get this error:
{ [ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433]
name: 'ConnectionError',
message: 'Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433',
code: 'ESOCKET' }
I know I have to enable TCP/IP connections for my local MySql server, but I can not find how to do it on OSX El Capitan. There is nothing like control panel with settings. That's all available at System Preferences > MySql:
Any ideas how to fix this, guys? Thanks for trying to help.
mssql and mysql are two very different things... You should use the node-mysql or some other mysql client library if you are going to be running mysql on your dev machine
https://github.com/felixge/node-mysql
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();