Question

Node.js & Mongodb - Dont receive any data from the database droplet

Posted April 27, 2019 1k views
MongoDBNode.jsUbuntu 16.04

Hi, i hope one of you can help me out with this question. I recently created 2 droplets. One of them are containing my node express application and the other is containing my MongoDB database version 3.4.2. My problem is that i can see my node application is connecting to my database, but it is not receiving any data back. When i try to load a page with data from the database, it just gives me an error. I use mongoose to connect to my database droplet.

Setup:
Node.js: I have set this up with mongoose to connect to my database droplet and i have allowed my database droplets ip in the UFW to “Allow In” anywhere. For my node express i am using Nginx as well.

MongoDB: My mongo droplet is setup with version 3.4.2, and when i run netstat -anp i can see the following:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 xx.xxx.xxx.xx:27017     0.0.0.0:*               LISTEN      -               
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -               
tcp        0      0 database_droplet_ip:27017     node_droplet_ip:53274    ESTABLISHED -               
tcp        0      0 database_droplet_ip:22        node_droplet_ip:43929    ESTABLISHED -               
tcp6       0      0 :::22                   :::*                    LISTEN      -     

So for me it looks like my node application is connecting to the database droplet, but as soon as i try to get any data from the database, i just receive an error.
My mongoose string is the following: mongoose.connect("mongodb://username:password@database_ip:27017/database_name?authSource=admin", {useNewUrlParser: true });

In the database i haven’t made any database, cause that is being created by itself, right? So if i write cucumber in the mongoose string, then it creates the database if it doesn’t exist. But in the database i only have admin and local. If i connect the node application to for example mongodb atlas, it connects without any problems.

The UFW on my node application looks like the following:

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
27017                      ALLOW       node_ip            
Nginx Full                 ALLOW       Anywhere                  
Anywhere                   ALLOW       mongodb_ip             
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx Full (v6)            ALLOW       Anywhere (v6)  

and the UFW on my mongo droplet look like:

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
27017                      ALLOW       node_ip            
OpenSSH (v6)               ALLOW       Anywhere (v6) 

The MongoDB conf looks like the following:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,mongodroplet_ip


#processManagement:

security:
  authorization: "enabled"

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

(I have checked that mongo is actually running)

I hope that one of you will be able to help me with this, since i have spend days trying to figure this out. As a last information, it is IPv4 addresses i am using and not IPv6. At my domain provider i have setup A and AAAA for both IPv4 and IPv6 to the domain. I can easily access the application by typing the domain, but not get the data from the database. To install the database droplet i have followed the initial server setup and install and secure mongodb article from digitalocean.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

I solved this myself. It was actually a silly error. Apparently i assigned the wrong role to the database user. After i gave it role “root” instead, everything worked perfectly.

Submit an Answer