I'm struggling to connect with sails to a mongodb database that uses a database for authentication named "dbadmin". Where I am DBA decided to have all users centralized in a users database.
I can connect to this "dbadmin" database but then sails complains it cannot create collections there.
How can I use an authentication database and then a different database for collections?
You can define two database connection in your connections config file and then you can pass the connection in your data model directly
see the example:
in your config file
// server/config/connections.js
dbadmin: {
adapter: 'sails-mongo',
host: 'dbadmin-host',
port: 27017,
user: 'username',
password: 'password',
database: 'dbadmin'
}
dbuser: {
adapter: 'sails-mongo',
host: 'dbuser-host',
port: 27017,
user: 'username',
password: 'password',
database: 'dbuser'
}
and now you can use in your model:
// server/api/models/AdminUser.js
module.exports = {
connection:'dbadmin',
attributes: {
name : { type: 'string' },
pass : { type: 'string' }
}
};
// server/api/models/Users.js
module.exports = {
connection:'dbuser',
attributes: {
name : { type: 'string' },
pass : { type: 'string' }
}
};
NOTE: you can not make any collection between this two model .
It turns out that using the URL for the connection has more possibilities. So all I had to do is append at the end authSource=dbadmin
module.exports = {
models: {
connection: 'mongoRM'
},
connections: {
mongoRM: {
adapter: 'sails-mongo',
url: 'mongodb://user:password#db_url:port/database?authSource=dbadmin'
}
}
}
Related
I am getting the error : error: Error: Consistency violation: Unexpected error creating db connection manager: MongoError: Authentication failed. Password is fine and I am able to connect with MongoExpress from another server and Mongo Compass. I triple checked the password and even copy-pasted the connection URL to make sure it was not a typo error.
I have followed the instructions (https://sailsjs.com/documentation/tutorials/using-mongo-db) but the only difference is I added the password in the URL ('mongodb://user:password#localhost:27017/mydb') as the Sails auto-migrate process was throwing a mongo-error like need Auth to drop.
Mongo db version v4.4.4
sails-mongo 1.2.0
Sails v1.4.2.
--datastore.js
default: {
adapter: 'sails-mongo',
url: 'mongodb://user:password#localhost:27017/mydb',
}
--model.js
attributes: {
//MONGO
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'string', columnName: '_id' },
}
use admin
db.createUser(
{
user: "user",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "readWrite", db: "mydb" } ]
}
)
Thanks for any help
I have deployed a meteor app to port 80 of my AWS instance using MUP. I deployed a second meteor app to port 3000, but this time omitting mongo setup and specifying mongo url in the mup.js file. The setup works fine and the second app is deployed but none of my publications seems to work. I have tried the same setup with two test Apps previously and it worked.
MUP.JS of App 1
module.exports = {
servers: {
one: {
host: 'IP',
username: 'ubuntu',
pem: 'path to my pem file'
}
},
meteor: {
name: 'Dashboard',
path: 'Path to my project',
servers: {
one: {}
},
buildOptions: {
serverOnly: true,
},
docker: {
image: 'abernix/meteord:base',
},
env: {
PORT: 80,
ROOT_URL: 'base url/',
MONGO_URL: 'mongodb://mongodb:27017/dbname'
},
deployCheckWaitTime: 320,
enableUploadProgressBar: true
},
mongo: {
oplog: true,
port: 27017,
servers: {
one: {},
},
},
};
MUP.JS of App 2
module.exports = {
servers: {
one: {
host: 'IP',
username: 'ubuntu',
pem: 'path to my pem file'
}
},
meteor: {
name: 'DashBoard2',
path: 'Path to my project',
servers: {
one: {}
},
buildOptions: {
serverOnly: true,
},
docker: {
image: 'abernix/meteord:base',
},
env: {
PORT: 3000,
ROOT_URL: 'base url/',
MONGO_URL: 'mongodb://mongodb:27017/dbname'
},
deployCheckWaitTime: 320,
enableUploadProgressBar: true
},
};
Are you certain the connection to the database has been made? Typically the Meteor console will give an error if it can't connect to the MongoDB database. Also not sure if you have omitted your connection string, but it should be;
mongodb://user:password#server:port/database
I have a MEAN project and this is a snippet from my server.js
var db = require('./config/db');
// url : 'mongodb://localhost/cdnserver'
// results are same for 127.0.0.1 or the machines ip
console.log(mongoose.connect(db.url));
mongoose.set('debug', true);
mongoose.connection.on('connected', function () {
console.log('Mongoose default connection open to ' + db.url);
});
// If the connection throws an error
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
This is a setup that has been working well for over 3 months now. Now I am replicating my whole MEAN stack along with database to other machine. I took a mongodump and did mongorestore. The restored db setup looks fine through mongo from terminal.
However, when starting the server, connection-open callback is not getting called. disconnected and error callbacks are getting called if I stop the mongodb service. How do I debug this further?
I am attaching the console output from both the setups.
Setup 1 :
Mongoose {
connections:
[ NativeConnection {
base: [Circular],
collections: {},
models: {},
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'cdnserver',
options: [Object],
otherDbs: [],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
_listening: false,
db: [Object] } ],
plugins: [],
models: {},
modelSchemas: {},
options: { pluralization: true } }
Server up on 80
Mongoose default connection open to mongodb://localhost/cdnserver
1
Mongoose: videos.find({}) { skip: 0, limit: 5, fields: undefined }
Setup 2:
Mongoose {
connections:
[ NativeConnection {
base: [Circular],
collections: {},
models: {},
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'cdnserver',
options: [Object],
otherDbs: [],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
_listening: false,
db: [Object] } ],
plugins: [],
models: {},
modelSchemas: {},
options: { pluralization: true } }
Server up on 80
1
1
Mongoose default connection disconnected
Mongoose default connection error: Error: connection closed
cat /var/log/mongodb/mongodb.log shows exactly the same output in both the machines.
Update 1: The setup started working properly out of blue and it stopped again. I am not able to figure out what is making this happen.
I figured it out finally, the new setup was using a newer version of nodejs.
When I moved to 6.x from 7.x it worked fine. I guess the mongoose, node 7, mongodb versions didnt go well together.
Blockquote
i will planing use more database on same models on sails.Just wanna change db on progress.How can i do it on sails configure
Just change your connection config in config/connections.js to the db you will be using, then, in the model set the connection, example:
Connection
mysql: {
adapter: 'sails-mysql',
host: 'your-host',
user: 'user',
password: 'pass',
database: 'your-db'
port: 3306
}
Model
module.exports = {
schema: true,
connection: 'mysql',
tableName: 'users',
attributes: {
user:{
type:"string",
primaryKey: true,
unique: true
},
password:{
type:"string",
unique: true
}
}
};
I use sails 0.9.
Here is adapters.js code:
module.exports.adapters = {
mongo: {
module: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
database: 'tracker'
},
mongo2: {
module: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
}
};
And model:
module.exports = {
adapter: 'mongo2',
config: {
database: 'offer'
},
attributes: {
name: {
type: 'string',
unique: true,
required: true
}
}
};
From docs http://sailsjs.org/#!documentation/models i get that this model will be saved in database named "offer", but it use db named "sails". Looks like its just ignore config section of model.
What is my mistake?
You should put the database in adapter config itself
mongo2: {
module: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
database: 'offer'
}