How configure Rocket Chat and MongoDB in separatelly hosts?

Description

I’m trying to install and configure rocketchat and mongodb in separatelly hosts. One for application and another one for mongodb.
I follown this tutorial https://rocket.chat/docs/installation/manual-installation/ubuntu/

and I did all steps for each part separately (mongodb and rocketchat)

when I ran the main.js command:

MONGO_URL=mongodb://<mongodb-ip-host>:27017/rocketchat?replicaSet=rs01 MONGO_OPLOG_URL=mongodb://<mongodb-ip-host>:27017/local?replicaSet=rs01 ROOT_URL=http://0.0.0.0:3000 PORT=3000 node /var/Rocket.Chat/main.js

I got this error below:

						throw(ex);
						^

MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
  name: 'MongoNetworkError',
  errorLabels: [Array],
  [Symbol(mongoErrorContextSymbol)]: {}
}]
    at Pool.<anonymous> (/var/Rocket.Chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/topologies/server.js:431:11)
    at Pool.emit (events.js:311:20)
    at /var/Rocket.Chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/pool.js:557:14
    at /var/Rocket.Chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/connect.js:39:11
    at callback (/var/Rocket.Chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/connect.js:261:5)
    at Socket.<anonymous> (/var/Rocket.Chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/connect.js:286:7)
    at Object.onceWrapper (events.js:418:26)
    at Socket.emit (events.js:311:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {}
}

PS.: I set the RocketChat on /var/Rocket.Chat folder

I’d like to understand why some part of application are trying to connect in a mongodb locally : 127.0.0.1:27017

Server Setup Information

Rocket.Chat 3.0.0
OS: Ubuntu 18.04 LT
Mongodb 4.0.9
NodeJS 12.14.0

Just guessing since I have not yet tried this myself. When you installed mongodb on the other system, did you configure it to allow connections from remote systems? Maybe Rocket.chat tries the MONGO_URL you specified and then falls back to 127.0.0.1:27017

Make sure in your /etc/mongodb.conf file you have the following line,

bind_ip = 0.0.0.0

Thanks for the reply @dwaynem

My mongodb is already configured with bindIp = 0.0.0.0

Here is my /etc/mongod.conf:

...
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

...

Also, I ran the netstat command, and here is the response:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      1219/mongod         

The firewall is ok as well

Now I am going to be even less helpful and say that “It works for me.” I assume that if I were to encounter any problems they would have occurred during the setup on the web page. Maybe install the mango shell on the system where you have rocketchat and see if you can connect to the mangodb server on the remote system?

dwayne@localhost:~$ sudo systemctl status rocketchat
● rocketchat.service - The Rocket.Chat server
   Loaded: loaded (/lib/systemd/system/rocketchat.service; enabled; vendor prese
   Active: active (running) since Tue 2020-03-31 18:11:57 UTC; 34s ago
 Main PID: 7847 (node)
    Tasks: 11 (limit: 2318)
   CGroup: /system.slice/rocketchat.service
           └─7847 /usr/local/bin/node /opt/Rocket.Chat/main.js

Mar 31 18:11:57 localhost systemd[1]: Started The Rocket.Chat server.
Mar 31 18:12:19 localhost rocketchat[7847]: LocalStore: store created at
Mar 31 18:12:19 localhost rocketchat[7847]: LocalStore: store created at
Mar 31 18:12:19 localhost rocketchat[7847]: LocalStore: store created at
Mar 31 18:12:20 localhost rocketchat[7847]: Setting default file store to GridFS

And there is rocket.chat stuff going on in /var/log/mongodb/mongod.log on the remote system where mongodb is running.

dwayne@localhost:/var/log/mongodb$ sudo tail -f mongod.log
2020-03-31T18:12:51.419+0000 I  INDEX    [conn10] ignoring obselete { background: false } index build option because all indexes are built in the background with the hybrid method
2020-03-31T18:12:51.435+0000 I  INDEX    [conn10] index build: starting on rocketchat.rocketchat_avatars.chunks properties: { v: 2, unique: true, key: { files_id: 1, n: 1 }, name: "files_id_1_n_1", ns: "rocketchat.rocketchat_avatars.chunks", background: false } using method: Hybrid
2020-03-31T18:12:51.435+0000 I  INDEX    [conn10] build may temporarily use up to 200 megabytes of RAM
2020-03-31T18:12:51.435+0000 I  INDEX    [conn10] index build: collection scan done. scanned 0 total records in 0 seconds
2020-03-31T18:12:51.437+0000 I  INDEX    [conn10] index build: inserted 0 keys from external sorter into index in 0 seconds
2020-03-31T18:12:51.438+0000 I  INDEX    [conn10] index build: done building index files_id_1_n_1 on ns rocketchat.rocketchat_avatars.chunks
2020-03-31T18:13:14.172+0000 I  STORAGE  [TimestampMonitor] Removing drop-pending idents with drop timestamps before timestamp Timestamp(1585678386, 1)
2020-03-31T18:13:14.172+0000 I  STORAGE  [TimestampMonitor] Completing drop for ident index-500-6673825855891529967 (ns: rocketchat.rocketchat_raw_imports.$_id_) with drop timestamp Timestamp(1585678368, 2)
2020-03-31T18:13:14.173+0000 I  STORAGE  [TimestampMonitor] Completing drop for ident index-501-6673825855891529967 (ns: rocketchat.rocketchat_raw_imports.$_updatedAt_1) with drop timestamp Timestamp(1585678368, 2)
2020-03-31T18:13:14.173+0000 I  STORAGE  [TimestampMonitor] Completing drop for ident collection-499-6673825855891529967 (ns: rocketchat.rocketchat_raw_imports) with drop timestamp Timestamp(1585678368, 2)

I’m thinking there is something really wrong with what I’m doing
Now I tested with vagrant environment and got the same error.

Am I miss something?

here is my vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.define "app" do |app|
    app.vm.box = "ubuntu/bionic64"
    app.vm.hostname = 'app'
    
    app.vm.network :public_network, ip: "192.168.10.16"

    app.vm.provider :virtualbox do |v|
      v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
      v.customize ["modifyvm", :id, "--memory", 1024]
    end
  end

  config.vm.define "db" do |db|
    db.vm.box = "ubuntu/bionic64"
    
    db.vm.network :public_network, ip: "192.168.10.17"

    db.vm.provider :virtualbox do |v|
      v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
      v.customize ["modifyvm", :id, "--memory", 1024]
    end
  end
end

I’m still using this link to configure my environment: https://rocket.chat/docs/installation/manual-installation/ubuntu/

and here is the commands below:

for mongodb installation:

# 1
sudo apt-get -y update

# 2
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

# 3
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

# 4
sudo apt-get install -y mongodb-org

# 5
sudo sed -i "s/^#  engine:/  engine: mmapv1/"  /etc/mongod.conf

# 6
sudo sed -i "s/^#replication:/replication:\n  replSetName: rs01/" /etc/mongod.conf

# 7
sudo systemctl enable mongod && sudo systemctl start mongod

# 8 (and finally!!)
mongo --eval "printjson(rs.initiate())"

for rocketchat installation:

# 1
sudo apt-get -y update && sudo apt-get install -y curl && curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -

# 2 (PS.: just remove the mongodb-org below)
sudo apt-get install -y build-essential nodejs graphicsmagick

# 3
sudo npm install -g inherits n && sudo n 12.14.0

# 4
curl -L https://releases.rocket.chat/latest/download -o /tmp/rocket.chat.tgz

# 5 
tar -xzf /tmp/rocket.chat.tgz -C /tmp

# 6
cd /tmp/bundle/programs/server && npm install

# 7
sudo mv /tmp/bundle /opt/Rocket.Chat

# 8
sudo useradd -M rocketchat && sudo usermod -L rocketchat

# 9
# PS.: I'm stil cofiguring the rocketchat service, but I'm not considering here on the commands (https://rocket.chat/docs/installation/manual-installation/ubuntu/#configure-the-rocketchat-service)
sudo chown -R rocketchat:rocketchat /opt/Rocket.Chat

# 10 (and finally)
vagrant@app:~$ MONGO_URL=mongodb://192.168.10.17:27017/rocketchat?replicaSet=rs01 MONGO_OPLOG_URL=mongodb://192.168.10.17:27017/local?replicaSet=rs01 ROOT_URL=http://0.0.0.0:3000 PORT=3000 node /opt/Rocket.Chat/main.js

And finally, I’m still got the same error:

/opt/Rocket.Chat/programs/server/node_modules/fibers/future.js:313
						throw(ex);
						^

MongoNetworkError: failed to connect to server [ubuntu-bionic:27017] on first connect [Error: connect ECONNREFUSED 127.0.1.1:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1129:14) {
  name: 'MongoNetworkError',
  errorLabels: [Array],
  [Symbol(mongoErrorContextSymbol)]: {}
}]
    at Pool.<anonymous> (/opt/Rocket.Chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/topologies/server.js:431:11)
    at Pool.emit (events.js:210:5)
    at /opt/Rocket.Chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/pool.js:557:14
    at /opt/Rocket.Chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/connect.js:39:11
    at callback (/opt/Rocket.Chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/connect.js:261:5)
    at Socket.<anonymous> (/opt/Rocket.Chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/connect.js:286:7)
    at Object.onceWrapper (events.js:300:26)
    at Socket.emit (events.js:210:5)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {}
}

Also, I got success to connect the application (192.168.10.16) into database (192.168.10.17) using mongo shell,

The only difference I can see is that I started RocketChat as a service before I ever tried starting it from the command line under my personal user account.

indeed!
I also configured RocketChat as a service, but I believe this can’t bring any differences for the result.
I’ll continue testing and trying to understand what is happening.

Thanks again for the reply @dwaynem !!

i think I could solve this issue.

I just remove the ‘replica Set Name’ from the environment variables on MONGO_URL and MONGO_OPLOG_URL

so, here is my result below:

vagrant@app:~$ MONGO_URL=mongodb://192.168.10.17:27017/rocketchat MONGO_OPLOG_URL=mongodb://192.168.10.17:27017/local ROOT_URL=http://0.0.0.0:3000 PORT=3000 node /opt/Rocket.Chat/main.js
Setting default file store to GridFS
LocalStore: store created at 
LocalStore: store created at 
LocalStore: store created at 
Setting default file store to GridFS
ufs: temp directory created at "/tmp/ufs"
Loaded the Apps Framework and loaded a total of 0 Apps!
Updating process.env.MAIL_URL
Using GridFS for custom sounds storage
Using GridFS for custom emoji storage
Browserslist: caniuse-lite is outdated. Please run next command `npm update`
➔ System ➔ startup
➔ +--------------------------------------------+
➔ |               SERVER RUNNING               |
➔ +--------------------------------------------+
➔ |                                            |
➔ |  Rocket.Chat Version: 3.0.9                |
➔ |       NodeJS Version: 12.14.0 - x64        |
➔ |      MongoDB Version: 4.0.17               |
➔ |       MongoDB Engine: mmapv1               |
➔ |             Platform: linux                |
➔ |         Process Port: 3000                 |
➔ |             Site URL: http://0.0.0.0:3000  |
➔ |     ReplicaSet OpLog: Enabled              |
➔ |          Commit Hash: b7f307df1e           |
➔ |        Commit Branch: HEAD                 |
➔ |                                            |
➔ +--------------------------------------------+

It seems the ‘?replicaSet=rs01’ on url, was causing some problem when the applications tried to connect with mongodb.

For now, I don’t understand what is the impact to not specify the replica set name on url.

I’ll check this and see what happens to consider my app connect this way

Hi @celsoagra, thanks to your clue, I was able to solve this problem too.

The problem is when you specify replicaSet param, you need to have more than 1 host in the earlier host part of the connection string.

e.g.
Environment=MONGO_URL=mongodb://username:password@iphost1:27017,iphost2:27017/dbname?replicaSet=rs01&authSource= dbname

If you don’t have 2 hosts there like above, the replicaSet param made meteor unable to connect and default to 127.0.0.1.

I chose to explain this in detail and hoping it can help the others who may face the same issue. This usually happens because we setup rocket chat for testing or we didn’t scale up Mongodb replica sets yet but using Mongodb standalone replica set setup.

I think the issue that your mongodb server is returning a hostname of 127.0.0.1.
You can get this in the shell of the mongodb server.

$ mongo
rs01:PRIMARY> rs.conf()

Results should be something similar:

{
        "_id" : "rs01",
        "version" : 2,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "127.0.0.1:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("<<masked>>")
        }
}

as seen the host is 127.0.0.1:27017

This can be fixed by executing the below:

cfg = rs.conf()
cfg.members[0].host = "mongodb-server-ip:27017"
rs.reconfig(cfg)