I'm trying to connnect to Mongo DB Driver and I am getting this error:
This is code I copy and pasted from the starter documents on the website. I changed the hostname to my first and last name and the uri connection to my server:
Any help is appreciated.
I have also faced this issue, while trying to create a demo app, I updated my password using autogenerate password, i.e I clicked on auto generate password and pasted it in connection string, it worked for me.
Instead of mongodb+srv:// you should be using mongodb://
DNS SeedList Format
mongodb+srv requires additional settings on DNS which contains seedlist, commonly use for sharded clusters and replica set
https://docs.mongodb.com/manual/reference/connection-string/#std-label-connections-dns-seedlist
Standard Connection String Format
If you are connecting to a standalone you should be using the standard connection string format.
mongodb://<username>:<password>:<IP/Hostname>:<Port>/<database>
https://docs.mongodb.com/manual/reference/connection-string/#standard-connection-string-format
Creating User with Password
To create a username with password you should be using db.createUser() which can be found here.
https://docs.mongodb.com/manual/reference/method/db.createUser/
Example ( To create a Admin user )
use admin;
db.createUser({
username: 'brianlevin',
pwd: passwordPrompt(),
roles: [ { role: 'admin', db: 'databasename' }]
})
Related
I am new to sails and its connection to database. I plan to use Heroku to host my app so I would like to use Postgresql.
I have changed config.datastore file with the following code:
default: {
adapter: 'sails-postgresql',
url: 'postgresql://postgres:postgres#localhost:5432/postgres',
max: 1
}
On Sails documentation it sais that url is url: 'postgresql://user:password#host:port/database', but I dont understand where should I get user and password from. Do I need to initiate the postgresql and set it up and define a new database?
I am just trying to understand the mechanics behind this setup.
What you need to do is create a new user for your local PostgreSQL instead of using the default user and database. What I mean is, create a user-specific to your particular project in SQL following this should help https://www.postgresql.org/docs/8.0/sql-createuser.html.(This should also help you set the password)
Then create the database you want to connect to your Sails app to be used by Waterline by following this: https://www.postgresql.org/docs/9.0/sql-createdatabase.html (make sure you grant the user you created permissions for this database as well https://www.postgresql.org/docs/9.0/sql-grant.html)
Then all you need is replace the url property value in config/datastore.js with the value following the format
postgres://USERNAME:PASSWORD#localhost:5432/DATABASE_NAME
N.B: Replace USERNAME, PASSWORD, and DATABASE_NAME with the actual username, password, and database name respectively.
When you are deploying to Heroku, Heroku would provide you with an environment variable called DATABASE_URL if you install the Heroku Postgres add-on. You could just pass that to url like so
url: process.env.DATABASE_URL
I have database hosted on MLab and I am trying to connect it with Compass. I am using host and port given in connection string but it is showing error, here is my screenshot:
What am I doing wrong?
Had same problem manage to solve it like this:
A. Go to your db in mlab and in the tab choose "users"
B. Create a new user ex: username: admin password: 123456
C: Go to compass and fill it this way
Example of your connection path
ds012345.mlab.com:56789/myDBname
hostname
ds012345.mlab.com
port
56789
authentication: username /password
admin // or the name of the user you created in step A
123456 // or password for the user you created in step A
authentication database
myDBname // the name of your database in mlab
I just had the same problem.
I fixed it by updating my version of MongoDB Compass. No problems encountered with version 1.15.4.
Also, Authentication Database should not have the value "admin" but the name of the DB to connect you.
To make your connection easier, don't hesitate to copy your entire connection string into the clipboard. Compass detects it and proposes to automatically fill in the connection form.
The problem that I had was the Authentication Database Compass filled in automatically wasn't correct for my setup. By default, this was pointing to admin but it needs to point to the database that the user is associated with.
Summed up: The database of admin didn't exist.
Just to rule it out, double check what database you're pointing to. It should be in the name, like ds739176/database_name where database_name is, you guessed it, the name of your database.
Hope this helps.
The required credentials are not your login credentials to MLab,
instead these are database user credentials.
How to get them:
click on your DB on MLab.
go to users tab and create new user.
use the created users credentials to access db.
set authenticationdatabase to be your database.
I had the same problem. MongoDB url was working in code but Authentication failed was showing in MongoDB Compass.
When I checked, my mongodb password was iam%40me1234.
Here I'm using %40 HTML hex code in the password for # character.
So, If we will use the original character in the password like iam#me1234 in MongoDB Compass then it will work great.
Here is a full list of Hex codes
What I did:
Click on "Fill in connection fields individually" option in Compass.
Enter the hostname (Example ds051234.mlab.com)
Enter PORT (Example 12345)
Select Username/password in the Authentication option.
Enter username and password (Create a new user in mlab.com for your database)
Authentication Database (Just enter the "database name" in this field. Example: heroku_8967jgy5)
I hope this will help you.
I have created a cluster in MongoDB Atlas and can't seem to be able to write data to it.
const uri = "mongodb+srv://myUser:myPass#myDB-4myav.mongodb.net/portfolio";
I have this as my uri to connect to, and every time I try to write to the database, I get this error:
{ MongoError: not authorized on admin to execute command { insert:
"users", documents: [[{name Daniel} {_id
ObjectIdHex("5aa6d7d6396deb25844ccb52")} {__v 0}]], ordered: false }
I have read that I need to create an admin user with the role of "root" but when I connect to my database using the mongo shell and try creating it, I get this:
Error: couldn't add user: not authorized on admin to execute command
So basically I don't have a user that can write to my database.
I've also tried making a user with every role possible on the MongoDB Atlas website (for my cluster of course) and then connecting through the mongo shell with it, but that failed as well.
To summarize: I've made a new cluster on MongoDB Atlas. How do I write data to it?
Thanks in advance, feel free to point out if I'm missing something simple and stupid.
{ MongoError: not authorized on admin to execute command { insert: "users", documents: [[{name Daniel} {_id ObjectIdHex("5aa6d7d6396deb25844ccb52")} {__v 0}]], ordered: false }
This error indicates your code is trying to insert documents into the admin database, which is a system database reserved for user and role information.
In your connection string you intended to use the portfolio database. However, the srv connection string format does not support specifying a database so your option was ignored and the default database of admin was used. You need to select the portfolio database after connecting.
This was also reported in the Mongoose issue tracker (GitHub issue #6106) where a user posted a workaround you could adapt:
mongoose.connection.on('connected', function() {
// Hack the database back to the right one, because when using mongodb+srv as protocol.
if (mongoose.connection.client.s.url.startsWith('mongodb+srv')) {
mongoose.connection.db = mongoose.connection.client.db('portfolio');
}
console.log('Connection to MongoDB established.')
});
mongoose.connect(...);
I have read that I need to create an admin user with the role of "root" but when I connect to my database using the mongo shell and try creating it, I get this:
You need to manage users via MongoDB Atlas rather than the mongo shell. You can create a user account with the "Read and write to any database" role (or more refined privileges using Advanced Options).
You need to create a user with root role from Mongo Shell,
use admin
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
}
);
exit;
or you need to add new user through atlas(refer below snapshot)
Goto Clusters
click Security
Click ADD NEW USER
type username
create Password based on SCRAM-SHA1 method
choose ROLE Atlas admin
click Add User
After created user, goto Clusters-->OverView --> CONNECT
Verify the IP Whitelist
Choose a connection method:
Connect with Mongo Shell (I am choosing)
Connect your Application
Connect with MongoDB Compass
$ mongo "mongodb+srv://project-u-s3cgp.azure.mongodb.net/test" --username dbAdmin
MongoDB shell version v3.6.1
Enter password:
connecting to: mongodb+srv://project-u-s3cgp.azure.mongodb.net/test
.........
.........
MongoDB Enterprise project-U-shard-0:PRIMARY>db.user.insert({name:"Daniel"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise O2Ci-U-shard-0:PRIMARY>
I hope, you can able to insert the record after creation of root user.
Thanks,
Karthick
My mongodb is now held for ransom with a message of "Your DB is Backed up at our servers, to restore send 0.1 BTC to the Bitcoin Address then send an email with your server ip". After reading many articles, I am still unsure what publicly accessible database mean. Currently I access my database by SSH into my droplet with username and password and connect via port 27017 how can hacker access my db? Please advise me on what to do to prevent this happening in the future! Thank you
To prevent this type of hacking you need make your database secure..
Add the security.authorization setting to the config file
security:
authorization: enabled
Before enable it, make sure you have created root user with credential to login.
Example:
$ use admin
$ db.createUser(
{
user: "superAdmin",
pwd: "mySecurePassword",
roles: [ { role: "root", db: "admin" } ]
})
also you may need to create app separate users for each database with limited access, for more info, I have written one blog here MongoDB enable authentication.
for more info refer MongoDB security best practices and MongoDB security checklist
I have a Parse app, and I'm trying to migrate my app's database to a MongoDB instance on mLab.
I already have a fork of Parse Server set up on Heroku, and I'm using Heroku's mLab MongoDB add-on.
I have a database on mLab called heroku_1ksph3jj, and I should be able to connect to it with the following template:
mongodb://<dbuser>:<dbpassword>#ds047124.mlab.com:47124/heroku_1ksph3jj
However, each attempt returns:
Server returned error on SASL authentication step: Authentication failed.
I'm unsure what to replace <dbuser> and <dbpassword> with. I have a database user with the same name as my database: heroku_1ksph3jjz, so I used that. And I used the password for that user in place of <dbpassword>. Should I have used something else here?
You can get the dbuser and dbpass with:
heroku config | grep MONGODB_URI
Grab the dbuser (example_user) and dbpass (example_pass) from the response:
MONGOLAB_URI: mongodb://example_user:example_pass#mlab.com:12345/db
As of March 2016, mLab.com only supports mongo 3.0+ (as per a conversation with support), because of their new onerous authentication requirements.
This was not on the website, but I hope it helps someone here!
There's a message to create a user for the specific database:
A database user is required to connect to this database. To create one now, visit the 'Users' tab and click the 'Add database user' button
I'm so sorry that this may seem obvious but, you have to remove this characters <> for the migration to work
In your example would look like this:
mongodb://dbuser:dbpassword#ds047124.mlab.com:47124/heroku_1ksph3jj
Check your mongo client version. If it is in older major version (probably 2.x), update it to 3.x
For future visitors - don't use special characters in password .Even if you change the special character to ascii or unicode it wont work for mLab using mongoose.
Also don't use mLab credential , use db user credentials . I created a new user.
Eg. For me a password containing # character was replaced with ascii value %40 in URI , which worked when using native mongodb driver.
But on using mongoose, i was always getting Authentication Failed .
I removed special characters and db was authenticated via mongoose.
It looks like it was the password that was incorrect, which I'm assuming was set up by Heroku's mLab add-on. There was no obvious way to reset this in the mLab UI, so in the end I created another database user (with a new username and password) and was able to connect with that just fine.
Just go to your Heroku dashboard and check your settings.
Under the name field there's a big button "Reveal Config Vars". Click it and you'll see a MONGODB_URI var with a uri to your db. It'll look something like this:
mongodb://heroku_user:PASSWORDyourLOOKINGfor#ds2238985.mlab.com:63295/heroku_user
Your password is right after the semicolon after the heroku user name.
I know I am too late, just for information.
For getting the info of mLab account that got auto created when mLab addon got added to the application in your heroku account, try the below command.
heroku config:get MONGODB_URI
Ref:
http://algebra.sci.csueastbay.edu/~grewe/CS6320/Mat/NodeJS/Heroku/Heroku_MLabMongoDB.html