Wednesday, January 8, 2020

How to enable remote connection in mongodb?

Software Required
-mongodb version 4.0.4
-macOS Mojave

Problems
  • Try to connect mongodb using the REMOTE_IP address on my mac machine, always encounter the error message as shown below. 
 
server-0   | MongoError: failed to connect to server [REMOTE_IP:27017] 
on first connect [MongoError: connect ECONNREFUSED REMOTE_IP:27017]

  • Use the telnet client to connect the mongodb
telnet REMOTE_IP 27017

  • Error message as shown below is displayed
Trying REMOTE_IP...
telnet: connect to address REMOTE_IP: Connection refused
telnet: Unable to connect to remote host


Solutions
  • Open mongod.conf file that located at /usr/local/etc folder, add the REMOTE_IP into the file
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1,REMOTE_IP

  • Restart mongodb using the command below, please include the path of mongod.conf in the command.
 # mongod --config /usr/local/etc/mongod.conf


  • Use the telnet client to connect the mongodb, it will work like a charm!
 telnet REMOTE_IP 27017

    Small quote: How people treat you is their karma, how you react is yours!

    Monday, January 6, 2020

    Error "Attempted to create a lock file on a read-only directory: /data/db, terminating" is displayed when start mongod

    Problems
    • Try to start mongodb on my machine using the command below
     mongod
    • Error as shown below is displayed
    ...
    2020-01-06T23:33:40.434-0600 I STORAGE  [initandlisten] exception in initAndListen: 
    IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, 
    terminating
    ...

    Solutions

    • Run the command below to grant the current user to have the write access to /data/db directory, it will work like a charm when you restart the mongodb again.
     sudo chown -R $USER /data/db