External Database
Hey everyone!
I want to connect my Parse Server to an external MongoDB I have... Here's what I've done:
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://username:pass@domain:27017/DB-name',
cloud: './cloud/main.js',
appId: process.env.APP_ID,
masterKey: process.env.MASTER_KEY,
fileKey: xxxxxx,
serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',
.....,
And this is what I get as soon as I start the Parse Server:
warn: Unable to ensure uniqueness for usernames: URIError: URI malformed
at decodeURIComponent (native)
at module.exports (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/url_parser.js:143:15)
at connect (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/mongo_client.js:486:16)
at /home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/mongo_client.js:240:7
at Function.MongoClient.connect (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/mongo_client.js:236:12)
at MongoStorageAdapter.connect (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js:142:44)
at MongoStorageAdapter._schemaCollection (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js:185:19)
at MongoStorageAdapter.getAllClasses (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js:338:19)
at /home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Controllers/SchemaController.js:426:34
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
warn: Unable to ensure uniqueness for user email addresses: URIError: URI malformed
at decodeURIComponent (native)
at module.exports (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/url_parser.js:143:15)
at connect (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/mongo_client.js:486:16)
at /home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/mongo_client.js:240:7
at Function.MongoClient.connect (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/mongo_client.js:236:12)
at MongoStorageAdapter.connect (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js:142:44)
at MongoStorageAdapter._schemaCollection (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js:185:19)
at MongoStorageAdapter.getAllClasses (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js:338:19)
at /home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Controllers/SchemaController.js:426:34
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
warn: Unable to ensure uniqueness for role name: URIError: URI malformed
at decodeURIComponent (native)
at module.exports (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/url_parser.js:143:15)
at connect (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/mongo_client.js:486:16)
at /home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/mongo_client.js:240:7
at Function.MongoClient.connect (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb/lib/mongo_client.js:236:12)
at MongoStorageAdapter.connect (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js:142:44)
at MongoStorageAdapter._schemaCollection (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js:185:19)
at MongoStorageAdapter.getAllClasses (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js:338:19)
at /home/sotiris_wait_for_it_kaniras/parse-server/node_modules/parse-server/lib/Controllers/SchemaController.js:426:34
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
(node:67124) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): URIError: URI malformed
(node:67124) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): URIError: URI malformed
I'm not sure if it matters, but my password contains special characters like #%- I'm saying this because of this:
databaseURI (required) - The connection string for your database, i.e. mongodb://user:[email protected]/dbname. Be sure to URL encode your password if your password has special characters
Any ideas?
PS: Note that I have enabled SSH keys to the server where mongodb is hosted.
You probably need to URI-Encode the special characters in your password.
https://www.urlencoder.org/
@nbering Hello and thanks for your response!
I encoded my password as you said and this is what I get now:
warn: Unable to ensure uniqueness for usernames: MongoError: failed to connect to server [domain:27017] on first connect [MongoError: connect ECONNREFUSED IP:27017] at Pool.
(/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb-core/lib/topologies/server.js:336:35) at emitOne (events.js:96:13) at Pool.emit (events.js:188:7) at Connection. (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb-core/lib/connection/pool.js:280:12) at Connection.g (events.js:292:16) at emitTwo (events.js:106:13) at Connection.emit (events.js:191:7) at Socket. (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb-core/lib/connection/connection.js:187:49) at Socket.g (events.js:292:16) at emitOne (events.js:96:13) warn: Unable to ensure uniqueness for user email addresses: MongoError: failed to connect to server [domain:27017] on first connect [MongoError: connect ECONNREFUSED IP:27017] at Pool. (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb-core/lib/topologies/server.js:336:35) at emitOne (events.js:96:13) at Pool.emit (events.js:188:7) at Connection. (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb-core/lib/connection/pool.js:280:12) at Connection.g (events.js:292:16) at emitTwo (events.js:106:13) at Connection.emit (events.js:191:7) at Socket. (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb-core/lib/connection/connection.js:187:49) at Socket.g (events.js:292:16) at emitOne (events.js:96:13) warn: Unable to ensure uniqueness for role name: MongoError: failed to connect to server [domain:27017] on first connect [MongoError: connect ECONNREFUSED IP:27017] at Pool. (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb-core/lib/topologies/server.js:336:35) at emitOne (events.js:96:13) at Pool.emit (events.js:188:7) at Connection. (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb-core/lib/connection/pool.js:280:12) at Connection.g (events.js:292:16) at emitTwo (events.js:106:13) at Connection.emit (events.js:191:7) at Socket. (/home/sotiris_wait_for_it_kaniras/parse-server/node_modules/mongodb-core/lib/connection/connection.js:187:49) at Socket.g (events.js:292:16) at emitOne (events.js:96:13) (node:9905) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): MongoError: failed to connect to server [domain:27017] on first connect [MongoError: connect ECONNREFUSED IP:27017] (node:9905) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoError: failed to connect to server [domain:27017] on first connect [MongoError: connect ECONNREFUSED IP:27017]
In addition, this is my index.js:
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var ParseDashboard = require('parse-dashboard');
//var allowInsecureHTTP = true;
var path = require('path');
//process.env.MONGODB_URI = 'mongodb://user@domain/dbName';
var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://user:SSHPASS@domain:dbPort/dbName',
cloud: './cloud/main.js',
appId: process.env.APP_ID || 'xxx',
masterKey: process.env.MASTER_KEY || 'xxx', //Add your master key here. Keep it se$
fileKey: 'xxx',
serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse', // Don't forget to change to https if needed
this is correct format :- mongodb://username:password@
Are you sure the database is listening on the proper port and that this port is exposed/accessible to the server?
@agwl-saurabh I think I've got it right: mongodb://username:[email protected]: 27017/dbName right?
@flovilmart I'm positive about the proper port, but not if it's accessible to the server... How can I know?
I usually make a simple request from something like telnet, or use the mongo CLI to confirm that I have a good connection from the app server to the database server.
I see you have "SSH Key Password" in your comment now. Is this a mongodb user, or are you trying to tunnel through SSH this way?
@nbering The mongod is placed on an Ubuntu Server, which is accessed by SSH Keys...
So, to index.js I have mongodb://username:SSH_PASS@domain:dbPort/dbName.
Could it be an issue?
The nodejs mongo client doesn't handle SSH tunneling. You'll either need to reconfigure your MongoDB server to allow remote access, or setup a tunnel by another method. I don't have any recommendations for what that method might be... I've only used SSH tunneling in that way for administration or development, not production application servers.
The username and password in the mongodb url are for users in the mongodb server if you have auth enabled.
this is correct format.. mongodb://username:password@:27017/dbname
where user name is mongodb user name password is mongodb password not ssh password.
user below to create new db and use above. you have issue with mongoDB
db.createUser( ... { ... user: 'username', ... pwd: 'pass', ... roles: [{role: 'readWrite', db:'dbname'}] ... } ... )
make sure your mongoDB have web access. for this you need to change in your "mongod.conf" and when you type mongoDB URL on broser (http://URL:27017/) you should get "It looks like you are trying to access MongoDB over HTTP on the native driver port."
then it will work. I have two of my set up working fine on EC2 my mongo is on EC2.
Hello @agwl-saurabh how can I give my MongoDB web access?
@samigos do not share your credentials, please....
@flovilmart O_o I haven't shared anything...
Concerning my connection issue, I tried opening the port with you ufw, but nothing has changed!
/etc/mongod.conf Find and remove the following line entirely, or prefix it with a # to comment it out: bindIp: 127.0.0.1
problem is when you install mongodb by default it listen to localhost
now to test if mongodb is wokring fine over web
add below in your config file.
http: enabled: true RESTInterfaceEnabled: true
sudo service mongod start
You should now be able to go to your browser and type http://<xx.xx.xx.xx>:27017/ and see the message
you are trying to access mongo db over web.
Please note this issue is not related to parse server this is mongodb issue please try to find mongoDB issue over other form.
if you still have issue and not able to understand try to search regarding below
http: enabled: true RESTInterfaceEnabled: true
one more thing if you have any issue in DB connection i will suggest you to try to connect to help world node js app then try to connect to parse it will always be easy. and if your DB is wokring fine over 27017 above test is perfect .
@Samigos This doesn't seem to be an issue with parse-server itself, but a problem in the configuration of your mongoDB. Please head over to stackoverflow, or server fault as you may find experts that can guide you into deploying and maintaining your database.