Connecting Multiple Mongodbs in the same Meteor Project and deploy using Mup

Hello,

I have already developed a meteor project that has one kind of category and uses just one mongo database. Now I want to expand this project by adding new categories and creating separate databases for each category in one server. I tried connecting multiple databases this way:

const db = new MongoInternals.RemoteCollectionDriver(mongoUrl);
const TestColl = new Mongo.Collection(“test”, { _driver: db });

It works in development as I expected.
But I cannot deploy to the server using MUP. I got an error in the step of verifying deployment, like below.

=> Starting meteor app on port 3000
/built_app/programs/server/node_modules/fibers/future.js:313
throw(ex);
^

    MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
        at Timeout._onTimeout (/built_app/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/core/sdam/topology.js:437:30)

Is there any solution for this error? Or please let me know how to work with multiple databases that are working well in your own.

Thank you.

Natively it wont work because Meteor only lets you run one server. You can simply aggregate the data in via a separate script using any of the plethora of excellent npm packages for this purpose. Get up to speed here: MongoDB Tutorial

A basic architecture would be

app/ ← all of your meteor stuff
datascripts/ ← all of your database stuff, aggregate and write to the main db from here (no meteor crap here just your scripts)

Use crontab to automate. If you don’t know how to write a cron use this useful free tool Crontab.guru - The cron schedule expression editor*

Of course this is only possible if you have root on your own server, anything else will require paid subscriptions (not the pub/sub kind! :joy::skull:) and obfuscate the free tools that have been available for several decades and run the internet, open source is free just read the source. Compile it. Use it.

Hi, Meteor can work with multiple MongoDB databases with some limitations but it seems your problem is not the Meteor app.

I think your problem is related to your MongoDB not running or running in a different port.

Are you sure your MongoDB is up and running in port 27017 of your machine? Could you share your mup config files?

Beside only being able to tail one oplog what other limitations are you thinking of?

MongoDB is running well on port 27017.
My MUP config file is like that.

module.exports = {
servers: {
one: {
// TODO: set host address, username, and authentication method
host: ‘ip’,
username: ‘root’,
// pem: ‘./path/to/pem’
password: ‘password’
// or neither for authenticate from ssh-agent
}
},

meteor: {
    // TODO: change app name and path
    name: 'pjName',
    path: '../.',

    servers: {
        one: {},
    },

    buildOptions: {
        serverOnly: true,
    },

    env: {
        // TODO: Change to your app's url
        // If you are using ssl, it needs to start with https://
        PORT: 8080,
        ROOT_URL: 'domain',
        MONGO_URL: 'mongodb://localhost/db',
        //MONGO_OPLOG_URL: 'mongodb://mongodb/local',
        TZ: 'Asia/Bangkok'
    },

    docker: {
        // change to 'abernix/meteord:base' if your app is using Meteor 1.4 - 1.5
         //image: 'abernix/meteord:node-8.4.0-base',
      image: 'zodern/meteor:root',
        //image: 'abernix/meteord:node-12.14.0-base',
        //image: 'abernix/meteord:base',
    },

    // Show progress bar while uploading bundle to server
    // You might need to disable it on CI servers
    enableUploadProgressBar: true
},

mongo: {
    port: 27017,
    version: '4.4',
    oplog: true,
    servers: {
        one: {}
    }
},

// (Optional)
// Use the proxy to setup ssl or to route requests to the correct
// app when there are several apps

// proxy: {
//   domains: 'mywebsite.com,www.mywebsite.com',

//   ssl: {
//     // Enable Let's Encrypt
//     letsEncryptEmail: 'email@domain.com'
//   }
// }

};

One boring limitation is to be able to have just one collection with a specific name across all the databases, but you can turn off this error so it’s ok, just boring.

But the main one is what you said about the oplog but if the databases are in the same cluster that is probably not a problem.

I think I may have found other limitations but these are the big ones that I remember.

For example, in Meteor Cloud (I was working at Meteor when we implemented it) we connect to multiple MongoDBs in different regions, at the time I created a wrapper so I could perform the same action across all the databases as they were the same in terms of structure, just spread in different regions, it worked greatly. They are probably still running the same code until today :slight_smile:

So yes, Meteor for sure works with Multiple MongoDBs.

MongoDB is running well on port 27017

I don’t get it.

If MongoDB is running fine there is no reason to get this error: MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

I think you have something wrong there.

Multiple databases are not going to cause this error. This is just a simple connection problem.

If you need further help feel free to DM me, we can jump into a call.

How was this resolved?