Search code examples
mongodbmongodb-compassmongodb-authentication

I followed the mongoDB docs and now cannot access my DB I get this error: connect ECONNREFUSED 127.0.0.1:27017


Below I list the directions I followed from the docs

Problem

  1. After following the docs listed below I am unable to access my database. I most definitely did not forget my password as I actually saved the query that I ran to create the user I will list the query I ran below.

  2. I get the following error when I try to connect my local host instance in MongoDB compass

connect ECONNREFUSED 127.0.0.1:27017
  1. I think the issue might lie in the fact that I was running these command in the mongodb compass mongosh terminal and not the mongod

use admin
db.createUser(
  {
    user: "max",
    pwd: "max",
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" }
    ]
  }
)

What I need Help with:

I need help accessing my local database and if possible setting up authentication on the schema

Below this line is the docs I followed


Start MongoDB without access control

Start a standalone mongod instance without access control. Open a terminal and run the following command as the mongod user:

mongod --port 27017 --dbpath /var/lib/mongodb

The mongod instance in this tutorial uses port 27017 and the /var/lib/mongodb data directory.

The tutorial assumes that the /var/lib/mongodb directory exists and is the default dbPath . You may specify a different data directory or port as needed.

TIP When mongod starts, it creates some system files in the /var/lib/mongodb directory. To ensure the system files have the correct ownership, follow this tutorial as the mongod user. If you start mongod as the root user you will have to update file ownership later.

  1. Connect to the instance Open a new terminal and connect to the database deployment with mongosh
mongosh --port 27017

If you are connecting to a different deployment, specify additional command line options, such as --host , as needed to connect.

  1. Create the user administrator IMPORTANT Localhost Exception You can create the user administrator either before or after enabling access control. If you enable access control before creating any user, MongoDB provides a localhost exception which allows you to create a user administrator in the admin database. Once created, you must authenticate as the user administrator to create additional users.

I made sure that I made a user

Using mongosh switch to the admin database add the myUserAdmin user with the userAdminAnyDatabase and readWriteAnyDatabase roles":

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" }
    ]
  }
)

Solution

  • I just uninstalled and reinstalled both MongoDB and MongoDB compass.