1

From the terminal, the application launches and connects with MongoDB, but when I attempt to access it at the IP address of the droplet (with the correct port), I am getting an error:

MongoError: not authorized on cmf to execute command (cmf is the dbname).

I followed the install & setup instructions here, creating a user and afterward added the role of root described here. This was for the admin database.

When logged into the Mongo shell, I can access data thru db.status(), and when using admin db I can run show users and see the user.

I am confused whether I lack configuration for the cmf database. When using cmf db, when I run show users, nothing is returned.

For unknown reasons, the application cannot connect with mongodb. I am now thoroughly confused about where to go from here.

If anyone can point me in the right direction, I would appreciate it.

wscourge
  • 10,657
  • 14
  • 59
  • 80
JimB814
  • 510
  • 8
  • 24

1 Answers1

0

You need to create the database user with password (like in the tutorial included in your question), then upon connecting:

var dbHost = 'mongodb://username:password@localhost:port/cmf';
mongoose.connect(dbHost);

Where:

  • username is the username of created user,
  • password is the password,
  • port is the correct port of running mongodb (defaults to 27017).
wscourge
  • 10,657
  • 14
  • 59
  • 80
  • Thanks. Getting error: MongoError: Authentication failed..." I replaced the variables in the dbHost string with the user values that display when I run `show users` after `use admin`, and I used 27017 as the port value. When you say "create the database user" do you mean to create a user (db.createUser) in cmf db instead of admin (admin was used in the setup instructions)? – JimB814 Oct 17 '17 at 17:31
  • Yes, you need an user in `cmf` to connect to `cmf`. To see if it works, in `dbHost` replace `cmf` with `admin` and use it to connect to the `admin`, just for the quick test. – wscourge Oct 17 '17 at 17:35
  • Progress. It connected! – JimB814 Oct 17 '17 at 17:39
  • SO happy for you! – wscourge Oct 17 '17 at 17:53
  • Yes, getting closer. Thank you! Now I'm trying to figure out how to add a user for the cmf db. – JimB814 Oct 17 '17 at 17:56
  • First, in your mongo shell: run `use cmf`, then in `cmf` shell run `db.createUser(user)` with the `user` param constructed as here: https://docs.mongodb.com/manual/reference/method/db.createUser/ – wscourge Oct 17 '17 at 17:59
  • Success! I created the user in cmf, modified app.js. The app loads and connects with MongoDB, and now displays at the droplet IP address. Thank you so much for taking the time to help me through this! – JimB814 Oct 17 '17 at 18:23
  • Pleasure is mine, I'm happy to be useful :). – wscourge Oct 17 '17 at 18:25