SailsJS deployment to Heroku, connect to Mongolabs MongoDB - mongodb

I am right now attempting my first Heroku deployment of a SailsJS API. My app uses SailsJS v0.11 andsails-mongo 0.11.2.
I have updated config/connections.js to include the connection information to MongoDB database I have hosted for free at Mongolab.
mongodb: {
adapter: 'sails-mongo',
url: "mongodb://db-user:password123#ds047812.mongolab.com:47812/testing-db"
}
Also updated config/models.js to point to that adapter.
module.exports.models = {
connection: 'mongodb',
migrate: 'safe'
};
This is basically all I have changed from running the code locally, when I deploy to Heroku the app crashes and I get this error...
/home/zacharyhustles/smallChangeAPI/node_modules/connect-mongo/lib/connect-mongo.js:186
throw err;
^
at Socket.emit (events.js:107:17)
2015-07-08T19:37:00.778316+00:00 app[web.1]:
at Socket.<anonymous> (/app/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
Error: Error connecting to database: failed to connect to [localhost:27017]
How do I get rid of this, and make sure Sails does not try connecting to localhost db?

Ok, the problem was with storing sessions.
My solution was to setup a Redis database to store sessions.
In config/sessions.js make sure everything is commented out except for the method you want for session store.
Mine looked like this:
adapter: 'redis',
host: 'example.redistogo.com',
port: 1111,
db: '/redistogo',
pass: 'XXXXXYYYYYYXYXYXYYX',
This solved my posted problem, hope this helps another person out.

Related

connecting to mongodb replicaSet with nestjs and typeorm is not working

problem
I'm trying to connect to mongodb with nestjs(^8.2.3) and typeorm(^0.2.28)
In test environment, connecting to mongodb standalone server is working. For your information, node mongodb library version is ^3.6.2.
production sample code(nestjs server)
I referred the typeorm code to write mongodb options
import { TypeOrmModule } from '#nestjs/typeorm';
import { MongoConnectionOptions } from 'typeorm/driver/mongodb/MongoConnectionOptions';
export const configForOrmModule = TypeOrmModule.forRootAsync({
imports: [],
useFactory: async () => {
const mongodbConfig: MongoConnectionOptions = {
type: 'mongodb',
username,
// for replicaSet (production)
hostReplicaSet: 'server1.example.com:20723,server2.example.com:20723,server.example.com:20723',
replicaSet: 'replicaSetName'
port: Number(port),
password: encodeURIComponent(password),
database,
authSource,
synchronize: true,
useUnifiedTopology: true,
entities: [Something],
};
return mongodbConfig;
},
inject: [],
});
But in production environment, when nestjs server try to connect to mongodb replicaSet, the server get this server selection loop error message over and over again like below. Interesting thing was the domain that the server tried to connect was different from replicaSet hosts(ex. another-hostname not included in server1.example.com:20723,server2.example.com:20723,server.example.com:20723). (+ edited: the another hostname is actual physical server indicated by the dns(server.example.com))
[39m01/28/2022, 2:39:16 AM [31m ERROR[39m [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (3)...[39m
MongoServerSelectionError: getaddrinfo ENOTFOUND <another-hostname>
at Timeout._onTimeout (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:430:30)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
what I’ve tried but these not worked
remove useUnifiedTopology: true option
downgrade mongodb library version to 3.5.11 (I've read in mongodb community there are something bug with topology after 3.6 version)
use host option not the hostReplicaSet
if you need more information, please tell me. thank you for your helping.
It was kubernates DNS issue. The hostReplicaSet server1.example.com:20723,... is resolved to host1 (physical server name. without example.com) but, k8s doesn't know it. so connection was failed.
there are two options
update kubernates /etc/hosts setting to add host1 -> host1.example.com
or update mongodb hostname host1 -> host1.example.com

Getting correct socketPath for TypeORM config

I'm trying to connect a Cloud Run service to Cloud SQL postgres instance. I believe I'm nearly there, but am having some trouble getting the deployed instance to connect properly. My local environment can connect (via SSL) to the database intended for production, but the deployed version can't...
I'm using TypeORM, and have everything setup properly in the configuration...
#Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
const socketPath = configService.get('DB_SOCKET_PATH');
const extra = socketPath ? {
socketPath: socketPath,
ssl: {
rejectUnauthorized: false,
ca: Buffer.from(process.env.DB_SSL_CA, 'base64').toString('ascii'),
cert: Buffer.from(process.env.DB_SSL_CERT, 'base64').toString('ascii'),
key: Buffer.from(process.env.DB_SSL_KEY, 'base64').toString('ascii'),
}
} : { };
return ({
type: 'postgres',
host: socketPath || configService.get('DB_HOST'),
port: configService.get('DB_PORT'),
username: configService.get('DB_USER'),
password: configService.get('DB_PASS'),
database: configService.get('DB_NAME'),
extra: extra,
entities: [__dirname + '/../../modules/**/*.entity{.ts,.js}'],
namingStrategy: new SnakeNamingStrategy(),
synchronize: true,
});
}
})
]
})
export class DatabaseModule { }
Despite that I'm getting an error when I try to use the socketPath as the host rather than the actual host variable (necessary for GCP). It seems that TypeORM is adding extra characters, /.s.PGSQL.5432, at the end of my connection string that I don't want. And just to clarify, the socket path is in the form of /cloudsql/<PROJECT_ID>:<REGION>:<INSTANCE>.
[Nest] 28532 - 02/15/2021, 2:25:07 PM [ExceptionHandler] connect ENOENT <DB_SOCKET_PATH>/.s.PGSQL.5432 +3ms
Error: connect ENOENT <DB_SOCKET_PATH>/.s.PGSQL.5432
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
At an older point in time, this used to work for me but I guess something changed in the TypeORM library. Does anybody have any ideas on this? Thanks!
EDIT: As of now I've gotten it to connect to the server correctly, but it's now giving me an error that says the server doesn't support SSL connections, which makes no sense given that I can connect via SSL fine on my local machine...?
SOLUTION: The issue does not seem to any code's fault, but rather some networking stuff on the GCP side. I configured the service and database to run through a VPC then just used a private IP address for the host.
It seems that TypeORM is adding extra characters, /.s.PGSQL.5432
This is actually intended - the Postgres spec requires that the unix sockets end with this suffix.
[Nest] 28532 - 02/15/2021, 2:25:07 PM [ExceptionHandler] connect ENOENT <DB_SOCKET_PATH>/.s.PGSQL.5432 +3ms
The error means that the socket wasn't found - usually because there was a misconfiguration and the Cloud SQL proxy couldn't start. You can check your logs at the instance start up to see if the proxy left any errors, but generally it'll come down to the following:
The Cloud SQL Admin API needs to be enabled
Your service account needs to have Cloud SQL Connect IAM role (or equivalent)
The service needs to be configured for Cloud SQL.
For a full list of instructions, see the Connecting from Cloud Run to Cloud SQL page.

Connect meteor/nginx app to mongoDB with authentication

I have a meteor app with nginx and on the same server I have a mongoDB database which have an authentication.
When I want to connect on the database in ssh, it works fine with the authentication.
But on the meteor app, I always have a 502 bad gateway error.
I think it's because the application can't connect to the database but I don't know how to connect it.
On the file /etc/init/myapp.conf , I change the mongo url :
export MONGO_URL=mongodb://user:password#127.0.0.1:27017/myapp
but I have always the same problem.
And in the file /var/log/mongodb/mongodb.log, I have these errors :
[initandlisten] connection accepted from 127.0.0.1:45266 #2680 (3 connections now open)
[conn2680] assertion 16550 not authorized for query on myapp.system.indexes ns:myapp.system.indexes query:{ ns: "myapp.users"$
[conn2680] ntoskip:0 ntoreturn:1000
[conn2680] end connection 127.0.0.1:45266 (2 connections now open)
[initandlisten] connection accepted from 127.0.0.1:45268 #2681 (3 connections now open)
[conn2681] assertion 16550 not authorized for query on myapp.system.indexes ns:myapp.system.indexes query:{ ns: "myapp.users"$
[conn2681] ntoskip:0 ntoreturn:1000
[conn2681] end connection 127.0.0.1:45268 (2 connections now open)
When I remove the authentication in the database, it works fine, but I need this authentication.
Do you know how to connect the application to the database with authentication ?
Update :
In the file /home/myapp/myapp.log, I have :
Exception in setInterval callback: MongoError: not authorized for update on myapp.users
at Object.Future.wait (/home/myapp/bundle/programs/server/node_modules/fibers/future.js:449:15)
at [object Object].<anonymous> (packages/meteor.js:213:24)
at [object Object].MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:774:49)
at [object Object].update (packages/mongo/collection.js:581:29)
at AccountsServer.Ap._expireTokens (packages/accounts-base/accounts_server.js:1115:14)
at packages/accounts-base/accounts_server.js:1204:14
at [object Object]._.extend.withValue (packages/meteor.js:1122:17)
at packages/meteor.js:445:45
at runWithEnvironment (packages/meteor.js:1176:24)
- - - - -
at Function.MongoError.create (/home/myapp/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/error.js:31:$
at toError (/home/myapp/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/utils.js:114:22)
at /home/myapp/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/collection.js:1047:60
at getLastErrorCallback (/home/myapp/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/wireprotocol/2_4_s$
at /home/theroofwebapp/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/pool.js:455:18
at process._tickCallback (node.js:355:11)
Connect to mongo shell and authenticate as admin and then try below
use admin
db.system.users.update({"user" : "your_user"}, {$addToSet: {'otherDBRoles.myapp': 'readWrite'}}, false, false)
Replace your_user with your db username and myapp with the targeted database name
Here are some things for you to double check.
Make sure that you are exporting MONGO_URL wherever you are starting your Meteor app. You mentioned that you are doing your export MONGO_URL in /etc/init/myapp.conf. That doesn't sound like the correct place.
Make sure you are actually including your real username and password in the mongodb connection string and the connection string is using the correct port where mongodb is actually running. What you included in your question is a generic example but also included your app's name so its unclear if this is actually what you are using.
Double check the role you setup for the mongodb user used by your Meteor app. Make sure you at least gave the role of readWrite. Here is an example.
db.createUser(
{
user: "meteor",
pwd: "meteor",
roles: [ { role: "readWrite", db: "meteor_app" } ]
}
);

Sails.js - Authorisation issues with remote MongoDB on mLab but working fine locally

Recently, I took over a Sails.js application created for our company by a small team of web developers. They provided me with the source and a database dump. Now, my task is to get it up and running on Heroku. While everything is working okay when I run the app locally, with the remote connection there is an error on startup that says:
MongoError: not authorized on heroku_gbntc8sf to execute command { createIndexes: "agendaJobs", indexes: [ { key: { name: 1, priority: -1, lockedAt: 1, nextRunAt: 1, disabled: 1 }, name: "findAndLockNextJobIndex1" }, { key: { name: 1, lockedAt: 1, priority: -1, nextRunAt: 1, disabled: 1 }, name: "findAndLockNextJobIndex2" } ] }
at Function.MongoError.create ([ROOT_DIR]/node_modules/mongodb-core/lib/error.js:31:11)
at [ROOT_DIR]/node_modules/mongodb-core/lib/topologies/server.js:793:66
at Callbacks.emit ([ROOT_DIR]/node_modules/mongodb-core/lib/topologies/server.js:94:3)
at null.messageHandler ([ROOT_DIR]/node_modules/mongodb-core/lib/topologies/server.js:235:23)
at Socket.<anonymous> ([ROOT_DIR]/node_modules/mongodb-core/lib/connection/connection.js:259:22)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)
Here's a quick checklist of what I've done already:
checked the build log on Heroku - no errors or warnings;
set up mLab Heroku add-on, exported the database, done some manual checks from the mLab dashboard - everything looks okay;
logged in to the database remotely from the mongo command and a mongo:// URL, ran a few simple queries, and obtained information on the database user privileges;
created an identical user (with the heroku_gbntc8sf username, same password, same role, etc.) in the local database.
Here's what the connection configuration looks like:
// config/connections.js
module.exports.connections = {
mongodb: {
adapter: 'sails-mongo',
user: 'heroku_gbntc8sf',
password: [HIDDEN],
host: 'ds159387.mlab.com',
port: 59387,
database: 'heroku_gbntc8sf'
},
// ...
}
// config/env/development.js
module.exports = {
models: {
connection: 'mongodb'
},
// ...
}
// config/env/production.js
module.exports = {
models: {
connection: 'mongodb'
},
// ...
}
At the moment I'm running the server locally, trying to connect to the remote database, to eliminate as many variables as possible. Like I mentioned above, when I set host to '127.0.0.1' and port to 27017, everything works okay. The heroku_gbntc8sf user has basic readWrite permissions in both databases (local and remote). In fact, those two databases are pretty much identical, as far as I know. And yet...
I've read a sizeable chunk of the Sails.js documentation, as well as, the documentation on the sails-mongo adapter. I've searched for similar questions, but I couldn't find anything relevant. I've tried many different things, including a couple of different ways to configure the database connection, but that error is always there.
The reason why I'm posting to StackOverflow is that I cannot rely on the support from the original authors of the app at the moment. Also, I'm new to Sails.js, so I might be doing something wrong without even knowing. I was hoping that I could get away with treating the app as a 'black box' (or like a generic Node application), since my job is only to start the app on Heroku.
I've successfully used mLab in a Sails project recently, but I've used the Mongo URL string format, for example...
mongodbServer: {
adapter: 'sails-mongo',
url : "mongodb://dandanknight:som3P455w0rd#ds044979.mlab.com:44979/databasename"
}
Not sure if it helps, but can't hurt to try! It's also the only way I've successfully got a replicaSet working in Sails incidentally.
It's confusing, but I read the sails-mongo docs as "URL is the way forward, and passing an object is legacy usage" (here)

PropelBundle database:create for postgres

I've installed propel bundle for symfony2.
my database configuration is:
propel:
dbal:
driver: pgsql
user: postgres
password: postgres
dsn: pgsql:host=localhost;port=5432;dbname=test_database
options: {}
attributes: {}
When i wan to create this database from console (console propel: database:create) i have got strange error : Unable to open PDO connection [wrapped: SQLSTATE[08006] [7] FATAL: database "pgsql" does not exist.
i created pgsql database on my localhost and everything was good. Database "test_database" was succesfull created. Can somebody explain me why i got this previous error? On mysql i've created database without any problems.
This issue was a bug in the PropelBundle, it has been fixed by the following commit (even if the commit message is about MySQL, it fixes other RDBMS): https://github.com/propelorm/PropelBundle/commit/b4475d27fb1eb846d10cc2d2e2bd164f037508e3
I 've installed new PropelBundle via Composer and everything is ok now. I think maybe it was a problem with 1.0 bundle , now I have 1.1