# How configure Rocket Chat and MongoDB in separatelly hosts?

**URL:** <https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285>\
**Category:** Community Support\
**Created:** [March 30, 2020, 4:23am UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285 "2020-03-30T04:23:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![celsoagra](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/celsoagra/32/1641_2.png) [@celsoagra](https://forums.rocket.chat/u/celsoagra)\
**Post date:** [March 30, 2020, 4:23am UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285/1 "2020-03-30T04:23:05Z")

</div>

### 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/](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:

```auto
						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

---

<div class="post-metadata">

**Author:** ![dwaynem](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/dwaynem/32/1640_2.png) [@dwaynem](https://forums.rocket.chat/u/dwaynem)\
**Post date:** [March 31, 2020, 1:09am UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285/2 "2020-03-31T01:09:54Z")

</div>

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,

```auto
bind_ip = 0.0.0.0

```

> **[Allow mongo to connect from remote IP](http://jitu-blog.blogspot.com/2013/06/allow-mongo-to-connect-from-remote-ip.html)**
>
> Access to mongo is controlled with iptables for Linux based OS. Step 1 Make sure in your  /etc/mongodb.conf file you have the following...

---

<div class="post-metadata">

**Author:** ![celsoagra](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/celsoagra/32/1641_2.png) [@celsoagra](https://forums.rocket.chat/u/celsoagra)\
**Post date:** [March 31, 2020, 2:19am UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285/3 "2020-03-31T02:19:12Z")

</div>

Thanks for the reply @dwaynem

My mongodb is already configured with bindIp = 0.0.0.0

Here is my /etc/mongod.conf:

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

...

```

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

```auto
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

---

<div class="post-metadata">

**Author:** ![dwaynem](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/dwaynem/32/1640_2.png) [@dwaynem](https://forums.rocket.chat/u/dwaynem)\
**Post date:** [March 31, 2020, 6:31pm UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285/4 "2020-03-31T18:31:54Z")

</div>

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](https://docs.mongodb.com/manual/mongo/) 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)
```

---

<div class="post-metadata">

**Author:** ![celsoagra](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/celsoagra/32/1641_2.png) [@celsoagra](https://forums.rocket.chat/u/celsoagra)\
**Post date:** [March 31, 2020, 11:59pm UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285/5 "2020-03-31T23:59:35Z")

</div>

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:

```auto
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/](https://rocket.chat/docs/installation/manual-installation/ubuntu/)

and here is the commands below:

for mongodb installation:

```auto
# 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:

```auto
# 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:

```auto
/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,

---

<div class="post-metadata">

**Author:** ![dwaynem](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/dwaynem/32/1640_2.png) [@dwaynem](https://forums.rocket.chat/u/dwaynem)\
**Post date:** [April 1, 2020, 12:13am UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285/6 "2020-04-01T00:13:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![celsoagra](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/celsoagra/32/1641_2.png) [@celsoagra](https://forums.rocket.chat/u/celsoagra)\
**Post date:** [April 1, 2020, 2:14am UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285/7 "2020-04-01T02:14:35Z")

</div>

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 !!

---

<div class="post-metadata">

**Author:** ![celsoagra](https://sea1.discourse-cdn.com/flex021/user_avatar/forums.rocket.chat/celsoagra/32/1641_2.png) [@celsoagra](https://forums.rocket.chat/u/celsoagra)\
**Post date:** [April 1, 2020, 2:22am UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285/8 "2020-04-01T02:22:35Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![charles](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@charles](https://forums.rocket.chat/u/charles)\
**Post date:** [July 3, 2020, 12:32pm UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285/9 "2020-07-03T12:32:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dizzle](https://avatars.discourse-cdn.com/v4/letter/d/a9adbd/32.png) [@dizzle](https://forums.rocket.chat/u/dizzle)\
**Post date:** [July 6, 2020, 11:49am UTC](https://forums.rocket.chat/t/how-configure-rocket-chat-and-mongodb-in-separatelly-hosts/6285/10 "2020-07-06T11:49:32Z")

</div>

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.

```auto
$ mongo
rs01:PRIMARY> rs.conf()

```

Results should be something similar:

```auto
{
        "_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:

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

```
