Acces database from appfog's node.js server (Database settings) - mysql

I'm trying to connect to a database using a node.js server both hosted on appfog.com using these settings:
var client = mysql.createConnection({
host: 'mysql-node01.eu-west-1.aws.af.cm',
user: '****#gmail.com',
password: '*******',
database: 'd2dc10d6a450048b587114fa9b11756ed',
port: 3306
});
I'm pretty sure the host, database and maybe port are incorrect but I don't know which values to use, i.e. should I use localhost?

Use the db credentials from the VCAP_SERVICES environment var and do not hard code them. The credentials are provided when the app is started on AppFog. See the Appfog Node Docs
var env = JSON.parse(process.env.VCAP_SERVICES);
var creds = env['mysql-5.1'][0]['credentials']; # grabs the creds for the first mysql database
var client = mysql.createConnection({
host: creds.hostname || 'localhost',
user: creds.username,
password: creds.password,
database: creds.name,
port: creds.port || 3306
});
I created a npm package to help facilitate getting production and development credentials. See AppFog Env
How to use it:
First set a local env var with your local dev database credentials:
export DEV_DB_CREDS='{ "username": "root", "name": "dev-db-name" }'
Then get the creds in the app:
var service = appfog.getService('mysql-db-name', process.env.DEV_DB_CREDS)
var creds = service.credentials
This method allows the same code to work locally and in production.

Related

How do I create a mysql database for my nodejs project?

This should be really basic and simple to do but I can seriously not find any understandable information on how to create a simple database for my nodejs typescript project.
I have installed the following packages with npm:
mysql2
sequelize
sequelize-cli
sequelize-typescript
I have attempted the following commands at the terminal
C:\repos\NodeNew>mysql2 -u root -p
'mysql2' is not recognized as an internal or external command,
operable program or batch file.
C:\repos\NodeNew>mysql -u root -p
'mysql' is not recognized as an internal or external command,
operable program or batch file.
C:\repos\NodeNew>node mysql2 -u root -p
module.js:538
throw err;
^
Error: Cannot find module 'C:\repos\NodeNew\mysql2'
at Function.Module._resolveFilename (module.js:536:15)
at Function.Module._load (module.js:466:25)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
C:\repos\NodeNew>
So how do I CREATE my database so I can connect to it with sequelize etc?
The tools you have installed are only for connecting a Node.js app to MySQL and do not include command-line tools to manage the MySQL server.
I will assume you have installed MySQL and it's running – you should then be able to find its mysql.exe command line client in the bin/ directory in the server's installation directory. If you haven't fiddled with authentication, just running it might work.
When you get to a MySQL prompt, you can follow any old instructions for creating a database; CREATE DATABASE foo; is the gist of it (authentication and permissions being a different story).
Since you're on Windows, you might want to look into HeidiSQL – it's been a while since I've used it, but it's a decent graphical MySQL management tool.
You can also use mysql2 to create the database – illustrated below – but I recommend getting a management tool.
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'mysql',
});
connection.query('CREATE DATABASE foo');
You should have MySQL installed on your computer, to install mysql on Windows see the following page: MySql
Once you have MySQL up and running on your computer. Open the Command Terminal and execute the following:
npm install mysql
Now you have downloaded and installed a mysql database driver. Node.js can use this module to manipulate the MySQL database:
var mysql = require('mysql');
To create a conecction create a file connection.js:
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
to test it save the file and run:
node connection.js
Which will give you this result:
Connected!
It can be solved using beforeConnect hook of sequelize as below:
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const { host, port, username, password } = config;
# create sequelize instance without providing db name in config
sequelize = new Sequelize('', username, password, config);
sequelize.beforeConnect(async (config) => {
const connection = await mysql.createConnection({ host: host, port: port, user: username, password: password });
await connection.query(`CREATE DATABASE IF NOT EXISTS \`${process.env.DB_NAME}\`;`);
config.database = process.env.DB_NAME;
});
Config.json file contains configurations for different dbs like
{
"development": {
"username": "root",
"password": "init#123",
"host": "mysqldb",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"host": "127.0.0.1",
"dialect": "mysql",
}
}
Database name is provided after db connection and creation(if required) in sequelize beforeConnect hook

Error: Handshake inactivity timeout in Node.js v6.9.1 and MySQL

I'm using 'mysql' and 'express-myconnection, when I'm running in localhost and some internet connections using AWS server its working perfectly.Yet when I'm at some of other internet connections with AWS setup its give out the error,Yet those connections have some decent internet speeds.
I'm using node ver. 6.9.1.
I read these Issues were in older versions, I do need to know if anything can be done to fix it by development side without changing server it-self.
'use strict';
var mysql = require('mysql');
var connector = require('express-myconnection');
var dotenv = require('dotenv').config();
var connectionOptions = {
host : process.env.DB_HOST,
database: process.env.DB_NAME,
user : process.env.DB_USER,
password: process.env.DB_PASS,
port : process.env.DB_PORT,
};
var connection = connector(mysql, connectionOptions, 'pool');
module.exports = connection;
Its looks like the mySql timeout happen because my internet connection takes a longer time to connect with the server,so basically its a DNS settings issue with the router.

Unable to connect to MAMP MySQL database using Wakanda's remote connection

I'm trying to connect to a MAMP MySQL database from Wakanda 11.
I tried connecting to the localhost database using Connect to Remote Datastore but I keep getting a Connection failed response. I've also tried using port 127.0.0.1:8081 which it connects to but not when attempting to use port:3306 which is where I have the MySQL database configured on the MAMP server. What am I doing wrong?
I've tried the following script...
model.addSQLCatalog("mysqldb", {
hostname: '127.0.0.1',
port: 3306,
user: 'root',
password: 'xxxxxxxx',
database: 'my_database_name',
ssl: false,
dbType: 'mysql'
});
...but this gives me the following error:
TypeError: JSON.stringify cannot serialize cyclic structures.
How can I fix and make it work on port:3306 so I can see my database structures?
If you are using Wakanda Enterprise Edition 11 or higher there is a MySQL Connector Pro.
Here is an example of connecting with this:
model.mergeSQLCatalog(localName, {
hostname: string 'host name',
port: number remote_port_number,
user: string 'userName',
password: string 'password',
database: string 'SQL database name',
jsFile: string 'configuration JavaScript file',
ssl: boolean true or false,
dbType: string 'mysql' } )
If you are on an older version of Wakanda Enterprise Edition but still using version 7 or higher then there is a Wakanda/MySQL connector.
Here is an example of connecting to MySQL with the connector:
var sql = require('waf-sql');
//use port 3306 and do not use SSL
var dbconn = sql.connect('mysql','192.168.0.21', 'john', 'x54?hsf5x!','arts',3306,false);
var rs = dbconn.update("people", {
name: "smith",
age: 42
}, {
id: 1
});
var rs = dbconn.select("*", "people", {
id: 1
});
var row = rs.getNextRow(); // get the first row
dbcon.close(); // close connection
Here is an example of the available parameters:
var params = {
hostname: [your host name or IP address],
user: [the user name of your DB],
password: [the user password],
database: [the DB name],
port: [the port number of the MySQL Server, by default 3306],
ssl: false,
dbType : 'mysql'
};
If I'm correct your issue has been solved? Does it work for both Windows & Mac ?
To resume , to use ProCOnnector you'll need :
The Enterprise Version of Wakanda
For the current v11 version of Wakanda, you need to use in the model.js file the addSQLCatalog() API.
The parameters accepted are described in the doc
Please note in the next version of Wakanda we 'll provide to wizzard to connect to other DBs instead of the addSQLCatalog(). This will help and ease the process.

Node.js connecting through ssh

I have a node.js server that works but needs to be set up for ssh connections:
var mysql = require('mysql')
var io = require('socket.io').listen(3000)
var db = mysql.createConnection({
host: 'hostname',
user: 'username',
password: '12345',
database: '12345',
port: 3306,
socket: '/var/run/mysqld/mysqld.sock'
})
db.connect(function(err){
if (err) console.log(err)
})
I'm aware that there are ssh npm libraries for this purpose, however the options available (ssh2, node-sshclient, etc) appear to deal with pretty intricate features that may overcomplicate things. I'm looking for the simplest way to connect to my mysql db through ssh. What would be the best way to accomplish this?
If you are running a linux/unix system do the following:
Connect to your mysql server via ssh and proxy the mysql port (default is 3306) via this ssh tunnel.
This works as follows:
1 Type in screen (to start a screen session which is permanent even if the shell gets closed).
2 Type into screen shell:
ssh -L 3306:127.0.0.1:3306 your_servers_domain_or_ip -lyour_login_name
3 Enter your ssh password / or use a PKI auth to avoid manual steps
4 Done... now it’s possible to connect MySQL like you would do when it’s installed on the same machine as your application.
Connect to MySQL from node.js like below:
var db = mysql.createConnection({
host: '127.0.0.1', // Important to connect to localhost after connecting via ssh in screen
user: 'username',
password: '12345',
database: '12345',
port: 3306
});
Sometimes it's preferrable to instantiate the SSH tunnel connection dynamically (in code) rather than separately using OS libraries. For example, it makes it easier to automatically close the connection, share the environment with other developers, or conditionally use an SSH tunnel depending on the environment.
With packages such as tunnel-ssh, this is easy. Building on the example provided, the connection code would look like:
import { createSSHTunnel } from "./sshTunnel";
const { srcAddr, srcPort } = await createSSHTunnel();
var db = mysql.createConnection({
host: srcAddr,
port: srcPort,
user: 'username',
password: '12345',
database: '12345'
});
With all logic cleanly abstracted away in the sshTunnel module, that could look like:
// sshTunnel.js
import { createTunnel } from "tunnel-ssh";
export async function createSSHTunnel(srcAddr = "127.0.0.1", srcPort = 12345) {
const tunnelOptions = {
autoClose: true,
};
const serverOptions = {
port: srcPort,
};
const sshOptions = {
host: process.env.SSH_HOST,
port: parseInt(process.env.SSH_PORT),
username: process.env.SSH_TUNNEL_USER,
password: process.env.SSH_TUNNEL_PASSWORD,
};
const forwardOptions = {
srcAddr: srcAddr,
srcPort: srcPort,
dstAddr: process.env.DB_HOST,
dstPort: parseInt(process.env.DB_PORT),
};
try {
await createTunnel(
tunnelOptions,
serverOptions,
sshOptions,
forwardOptions
);
} catch (error) {
if (error.code === "EADDRINUSE") {
// Assume port is uniquely used by SSH tunnel, so existing connection can be reused
console.log(`Returning existing SSH tunnel on ${srcAddr}:${srcPort}.`);
return { srcAddr, srcPort };
} else {
throw error;
}
}
console.log(`SSH tunnel successfully created on ${srcAddr}:${srcPort}.`);
return { srcAddr, srcPort };
}
Remarks:
The SSH tunnel arbitrarily uses local port 12345
The environment variables involved are:
DB_HOST: the database hostname
DB_PORT: the database port, 3306 in the original MySQL example, 5432 for Postgres etc.
SSH_HOST: the hostname of the machine serving the SSH tunnel
SSH_PORT: the port of the machine serving the SSH tunnel
SSH_TUNNEL_USER: the username for the SSH tunnel
SSH_TUNNEL_PASSWORD: the password for the SSH tunnel

node.js-mysql connection failed on windows7

I am just getting started with nodejs.
My question is that i get an Error:
connect ECONNREFUSED
npm install mysql;
server.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
port: '3306'
});
connection.connect(function(err) {
if(err) {
console.log(err);
}
});
connection.end();
Finally
1.I don't know the "user" and "password";
2."In mysql.conf, comment skip-networking.", I can't find mysql.conf;
3.I have tried this and it's not working, my platform is windows 7.
var client = mysql.createClient({
user: uuuu,
password: pppp,
host: '127.0.0.1',
port: '3306',
_socket: '/var/run/mysqld/mysqld.sock',});
It's clear you have not install mysql server in your windows yet. So you should go to http://www.mysql.com and download the server, install and set up, you can set your username and password and grant privilege to it, so you will know the user and password, and mysql.conf will be inside your installation. And if you are in windows, generally you would not use a UNIX socket to build the connection. So "_socket: '/var/run/mysqld/mysqld.sock'" should not be here.