Why won't Sequelize/Tedious connect to a localhost database? - javascript

I've been building a NodeJS/React app which connects to an MS SQL Server database. It was working fine, I uploaded it to the Windows hosting and it was working fine connecting to the dev database on a third party hosting company using this config file.
const Sequelize = require('sequelize');
const config = {
user: "myusername",
password: "mypassword",
server: "database_server",
database: "mydatabase",
options: {
enableArithAbort: true
},
};
const seqInstance = new Sequelize(
//
config.database,
config.user,
config.password,
{
dialect: "mssql",
host: config.server,
dialectOptions: {
encrypt: true,
options: {
validateBulkLoadParameters: true
}
},
}
);
module.exports = { seqInstance };
But when I try to connect to the production database which is on the same server, localhost, using this change to the config -
const config = {
user: "mynewusername",
password: "mynewpassword",
server: "localhost",
database: "myproductiondatabase",
options: {
enableArithAbort: true
},
};
it refuses to connect and throws this error.
There are other app on the server connecting to that localhost sql server databases. Why is this small change not working?

Related

Connect to mysql on correct port JS

hey using a javascript app im making as a backend server for my website i keep getting this as an error when trying to connect to my mysql server.
ConnectionError: Failed to connect to win2k16-mcdmz:1433 - Could not connect (sequence)
and this is my script to connect to the server. is there any ideas on why it is set to 1433?
private initializeServer() {
const config = {
user: 'MCAdmin',
password: '*******',
server: 'win2k16-mcdmz',
port: 3306,
database: 'ValorantLFG',
encrypt: true
};
mysql.connect(config, (err) => {
if (err) { console.log(err); }
this.init.lfgRequestsTimeout();
//this.init.noticesTimeout();
});
}

Connecting to MSSQL server with Sequelize

Using the following tedious code, I can successfully connect to an Azure SQL Server.
const Connection = require('tedious').Connection;
const connection = new Connection({
userName: '[USER]',
password: '[PASSWORD]',
server: '[HOSTNAME]',
options: {encrypt: true}
});
connection.on('connect', (err) => {
if (err) {
console.log('error connecting', err);
} else {
console.log('connection successful');
}
});
However, using what should be the equivalent Sequelize code, I get a connection timeout error.
const Sequelize = require('sequelize');
const sequelize = new Sequelize('[DBNAME]', '[USER]', '[PASSWORD]', {
dialect: 'mssql',
host: '[HOSTNAME]',
dialectOptions: {
encrypt: true
}
});
sequelize.authenticate().then((err) => {
console.log('Connection successful', err);
})
.catch((err) => {
console.log('Unable to connect to database', err);
});
Any thoughts?
Using: sequelize 3.29.0, tedious 1.14.0, SQL Server v12
I was getting below error
SequelizeConnectionError: Server requires encryption, set 'encrypt' config option to true.
I tried it out with Azure SQL Database and below way is working for me.
const sequelize = new Sequelize('DB Name', 'Username', 'Password', {
host: 'Host',
dialect: 'mssql',
dialectOptions: {
options: {
encrypt: true,
}
}
});
If you're trying it out with Azure SQL Database, you might also want to specify a longer request timeout value:
[...]
dialectOptions: {
requestTimeout: 30000 // timeout = 30 seconds
}
[...]
I tried your Sequelize code and it works fine. So you might need to add Client IP address to allow access to Azure SQL Server. To do this, go to the Azure portal, click on All Resources, select your SQL server, click on Firewall in the SETTINGS menu.
Your client address is conveniently included in the list, so you can just click on Add client IP followed by Save. When you run your code now, it should connect.
if you are using sql server management studio then simply replace dialect:'mysql' with dialect:'mssql':
const sequelize = new Sequelize('DB Name', 'Username', 'Password', {
host: 'Host',
dialect: 'mssql',
dialectOptions: {
options: {
encrypt: true,
}
}
});

Ghost on Heroku

I'm trying to host a Ghost blog alongside a Node app I already have running on Heroku, it's working on my local setup but doesn't load on production (Heroku). I feel like it's something small I'm missing so all help is appreciated:
Here is the tutorial I'm following: https://github.com/TryGhost/Ghost/wiki/Using-Ghost-as-an-NPM-module
server.js
"use strict";
const PORT = process.env.PORT || 3000;
const express = require('express');
const app = express();
const parentApp = express();
const path = require('path');
var ghost = require('ghost');
ghost({
config: path.join(__dirname, 'ghost/config.js')
}).then(function (ghostServer) {
parentApp.use(ghostServer.config.paths.subdir, ghostServer.rootApp);
ghostServer.start(parentApp);
});
config.js
// # Ghost Configuration
// Setup your Ghost install for various [environments](http://support.ghost.org/config/#about-environments).
// Ghost runs in `development` mode by default. Full documentation can be found at http://support.ghost.org/config/
var path = require('path'),
config;
config = {
// ### Production
// When running Ghost in the wild, use the production environment.
// Configure your URL and mail settings here
production: {
url: 'http://www.mywebsite.com/blog/',
mail: {},
database: {
client: 'postgres',
connection: {
host: 'host',
user: 'user',
password: 'password',
database: 'database',
port: 5432
}
},
server: {
host: '0.0.0.0',
port: process.env.PORT
},
paths: {
contentPath: path.join(__dirname, '/ghost/content')
}
},
// ### Development **(default)**
development: {
// The url to use when providing links to the site, E.g. in RSS and email.
// Change this to your Ghost blog's published URL.
url: 'http://localhost:2368/blog/',
// Example refferer policy
// Visit https://www.w3.org/TR/referrer-policy/ for instructions
// default 'origin-when-cross-origin',
// referrerPolicy: 'origin-when-cross-origin',
// Example mail config
// Visit http://support.ghost.org/mail for instructions
// ```
// mail: {
// transport: 'SMTP',
// options: {
// service: 'Mailgun',
// auth: {
// user: '', // mailgun username
// pass: '' // mailgun password
// }
// }
// },
// ```
// #### Database
// Ghost supports sqlite3 (default), MySQL & PostgreSQL
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghost-dev.db')
},
debug: false
},
// #### Server
// Can be host & port (default), or socket
server: {
// Host to be passed to node's `net.Server#listen()`
host: '127.0.0.1',
// Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
port: '2368'
},
// #### Paths
// Specify where your content directory lives
paths: {
contentPath: path.join(__dirname, '/content/')
}
},
// **Developers only need to edit below here**
// ### Testing
// Used when developing Ghost to run tests and check the health of Ghost
// Uses a different port number
testing: {
url: 'http://127.0.0.1:2369',
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghost-test.db')
},
pool: {
afterCreate: function (conn, done) {
conn.run('PRAGMA synchronous=OFF;' +
'PRAGMA journal_mode=MEMORY;' +
'PRAGMA locking_mode=EXCLUSIVE;' +
'BEGIN EXCLUSIVE; COMMIT;', done);
}
},
useNullAsDefault: true
},
server: {
host: '127.0.0.1',
port: '2369'
},
logging: false
},
// ### Testing MySQL
// Used by Travis - Automated testing run through GitHub
'testing-mysql': {
url: 'http://127.0.0.1:2369',
database: {
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'root',
password : '',
database : 'ghost_testing',
charset : 'utf8'
}
},
server: {
host: '127.0.0.1',
port: '2369'
},
logging: false
},
// ### Testing pg
// Used by Travis - Automated testing run through GitHub
'testing-pg': {
url: 'http://127.0.0.1:2369',
database: {
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'postgres',
password : '',
database : 'ghost_testing',
charset : 'utf8'
}
},
server: {
host: '127.0.0.1',
port: '2369'
},
logging: false
}
};
module.exports = config;
When I try to access the address I listed on config.js I get
Cannot GET /blog/
Any idea on what is happening?
Thank you

After I deploy node.js application the ghost blog section breaks

I have a node.js application where one of the views is a ghost.js blog, which I integrated by following Ghost's wiki article Using Ghost as an npm module.
Currently, my local version works perfectly.
The Error:
When I visit the deployed website, everything works ok, except when I got to mysite.heroku.com/blog, at which point I get the ghost page looking like .
I've noticed that the application has two localhost branches running simultaneusly (localhost:3000 and localhost:2368/). I'm not sure if that could be causing the error. I've checked out my Herokulogs, and they do not provide any more details than that a GET request was sent to /blog, returning first a 301and then a 404 error.
Also, it might be useful to know that when I click on the Go to front page link it sends me to http://localhost:2368/
My config.js file looks like the following:
var path = require('path'),
config;
config = {
// ### Production
// When running Ghost in the wild, use the production environment
// Configure your URL and mail settings here
production: {
url: 'http://example.com/blog',
mail: {},
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghost.db')
},
debug: false
},
server: {
// Host to be passed to node's `net.Server#listen()`
host: '127.0.0.1',
// Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
port: '2368'
}
},
// ### Development **(default)**
development: {
// The url to use when providing links to the site, E.g. in RSS and email.
// Change this to your Ghost blogs published URL.
url: 'http://localhost:2368/blog',
// Example mail config
// Visit http://support.ghost.org/mail for instructions
// ```
// mail: {
// transport: 'SMTP',
// options: {
// service: 'Mailgun',
// auth: {
// user: '', // mailgun username
// pass: '' // mailgun password
// }
// }
// },
// ```
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghost-dev.db')
},
debug: false
},
server: {
// Host to be passed to node's `net.Server#listen()`
host: '127.0.0.1',
// Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
port: '2368'
},
paths: {
contentPath: path.join(__dirname, '/content/')
}
},
// **Developers only need to edit below here**
// ### Testing
// Used when developing Ghost to run tests and check the health of Ghost
// Uses a different port number
testing: {
url: 'http://127.0.0.1:2369',
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghost-test.db')
}
},
server: {
host: '127.0.0.1',
port: '2369'
},
logging: false
},
// ### Testing MySQL
// Used by Travis - Automated testing run through GitHub
'testing-mysql': {
url: 'http://127.0.0.1:2369',
database: {
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'root',
password : '',
database : 'ghost_testing',
charset : 'utf8'
}
},
server: {
host: '127.0.0.1',
port: '2369'
},
logging: false
},
// ### Testing pg
// Used by Travis - Automated testing run through GitHub
'testing-pg': {
url: 'http://127.0.0.1:2369',
database: {
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'postgres',
password : '',
database : 'ghost_testing',
charset : 'utf8'
}
},
server: {
host: '127.0.0.1',
port: '2369'
},
logging: false
}
};
// Export config
module.exports = config;
It looks like Ghost is configured via a config.js file (see the link you provided), and that you may have it configured for url: 'http://localhost:2368/blog'. Looks like you'll need to change that to your actual URL.
Also, see this https://github.com/cobyism/ghost-on-heroku

Handling database environment configuration in Sails.js

The issue I have is related to the following quote from the official documentation:
Note If any connection to an adapter is used by a model, then all connections to that adapter will be loaded on sails.lift, whether or not models are actually using them. In the example above, if a model was configured to use the localMysql connection, then both localMysql and remoteMysql would attempt to connect at run time. It is therefore good practice to split your connection configurations up by environment and save them to the appropriate environment-specific config files, or else comment out any connections that you don't want active.
How can you configure the connection for a production server?
My connections.js file looks like this:
module.exports.connections = {
mongoDev: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: 'username',
password: 'password',
database: 'database'
},
mongoLive: {
adapter: 'sails-mongo',
host: 'host.mongolab.com',
port: 31681,
user: 'user',
password: 'password',
database: 'database'
}
};
And in my environment config files I've got:
development.js
module.exports = {
models: {
connection: 'mongoDev'
}
};
production.js
module.exports = {
models: {
connection: 'mongoLive'
},
port: 3000,
};
This works on my local machine, because the production database server is on an external server.
On the production environment I'm getting the following error:
[Error: failed to connect to [localhost:27017]]
It works if I remove the mongoDev object from my the connections object.
I've also tried using adaptors.js, but this only resulted in some deprecation errors.
$ sails -v
info: v0.9.9
I'm getting something different when running sails lift:
info: Sails
info: v0.10.5
You want to save the actual connection definition in the either development.js or production.js and remove them from connections.js. It's a little non-intuitive.
development.js
module.exports = {
connections : {
mongoDev: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: 'username',
password: 'password',
database: 'database'
}
},
models: {
connection: 'mongoDev'
}
};
production.js
module.exports = {
connections : {
mongoLive: {
adapter: 'sails-mongo',
host: 'host.mongolab.com',
port: 31681,
user: 'user',
password: 'password',
database: 'database'
}
},
models: {
connection: 'mongoLive'
},
port: 3000,
};

Categories