0

I have mongodb installed in my PC. Some years ago, I created the databases and collection in localhost server before 1year, but today I tried to connect it from MongoDB compass using the connection string,and it is being logged in successfully:

mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false

I want to connect it through the linux terminal,but searching on most of the post and docs I found the username and password is compulsory:

mongo mongodb://username:password@somewhere.mongolayer.com:10011/my_database

So,I am unware of what my username and password is, so I tried to connect it from terminal using:

mongo "mongodb://localhost:27118"

mongo --host localhost --port 27118

but I got error as:

2023-03-03T12:50:22.951+0545 E QUERY    [thread1] Error: couldn't connect to server localhost:27118, connection attempt failed :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

How can I connect to this localhost server from terminal?

AB21
  • 353
  • 1
  • 4
  • 15
  • Hi, Where does your mongoDB run? Inside a docker container? – CoderOfTheForce Mar 03 '23 at 09:37
  • 1. Is the MongoDB server running? 2. If it is running, is it listenning on port 27017 or port 27118? 3. Is authorization enabled on the MongoDB server, i.e., is username/password required? – rickhg12hs Mar 03 '23 at 12:08
  • try to log the docker container, you can use docker logs -f 'container-name' – Tobok Sitanggang Mar 03 '23 at 13:52
  • Maybe this one: https://stackoverflow.com/questions/74609210/cant-connect-to-mongodb-6-0-server-locally-using-nodejs-driver/74610881#74610881 – Wernfried Domscheit Mar 03 '23 at 14:11
  • Username/Password is only required when you enabled [authentication](https://www.mongodb.com/docs/manual/reference/configuration-options/#mongodb-setting-security.authorization). Anyway, error "couldn't connect to server" is not caused by missing credentials. – Wernfried Domscheit Mar 03 '23 at 14:13

0 Answers0