Search code examples
mongodbhomebrewreplicaset

Trying to run a replica set locally on mac with homebrew and run-rs


I have mongo setup with homebrew.

$ brew services start mongodb-community
$ mongosh

I can see my DBs in mongo compass.

enter image description here

I want to run this DB but in a replica set. But I can't figure out how.

I tried using the run-rs package. But it didn't work.

I installed the package.

$ npm install run-rs -g

Then I ran run-rs, but it failed:

$ run-rs --mongod
Purging database...
Running 'mongod' [ 27017, 27018, 27019 ]
Starting replica set...
Error: failed to start mongod with options [
  '--port=27017',
  '--dbpath=/Users/projectpath/backend/data/27017',
  '--bind_ip=localhost',
  '--replSet=rs'
]

So then, I stopped MongoDB.

$ brew services stop mongodb-community

I ran run-rs again.

$ run-rs --mongod
Purging database...
Running 'mongod' [ 27017, 27018, 27019 ]
Starting replica set...
Started replica set on "mongodb://localhost:27017,localhost:27018,localhost:27019?replicaSet=rs"
Connected to oplog
(node:28395) [MONGODB DRIVER] DeprecationWarning: collection.find option [oplogReplay] is deprecated and will be removed in a later version.
(Use `node --trace-deprecation ...` to show where the warning was created)

But this created a data folder with many files in my project directory. I don't want a bunch of project files in my directory. Are these files expected? If so, where should these files go? How do I get them into the correct directory? How come it's not behaving like my other mongo setup?

Now all the DBs I had before in mongo compass are gone. I can get back to them if I kill all mongo processes

$ ps wuax | grep mongo

$ kill (pid number)

and run:

$ brew services start mongodb-community

But How can I get my regular mongo community to use a replica set? Why are my mongo DBs gone when I run run-rs? And why does run-rs put many files in my project directory?

Am I missing how this is supposed to work?

UPDATE

I tried to add the homebrew path.

$ run-rs 5.0.6 --dbpath '/opt/homebrew/var/mongodb'

The data folder isn't added to my project now. But my old DBs shown in the first image are not in Mongo Compass anymore. Now when I switch to brew services start mongodb-community all my dev databases seemed to be removed.


Solution

  • It seems that once you get run-rs to work it purges your DB data so it doesn't seem you can run a replica set with your old db data without saving it somewhere and copying it back. It also seems you have to turn your homebrew mongo off. I'm not really sure what I'm talking about but this is how I got it to work.

    I set up run-rs and had to reseed my dev DB.

    Here's how I set up the local replica set:

    1. Turned off mongo-community:
    $ brew services stop mongodb-community
    
    1. Installed and ran run-rs using my data directory path ('/opt/homebrew/var/mongodb') and my mongo version (5.0.6). Mongo Compass gave me an error when I ran run-rs --mongod instead of run-rs 5.0.6 even though the version was 5.0.6 in both.
    $ npm install run-rs -g
    $ run-rs 5.0.6 --dbpath '/opt/homebrew/var/mongodb' 
    
    # if you don't want to purge the database add the flag --keep
    
    

    Then I ran my seed file and populated a new db at the uri: mongodb://127.0.0.1:27017/wtdev-local?replicaSet=rs

    Make sure to add replicaSet=rs to your URI.

    NOTE

    I kept having issues from running run-rs in the past incorrectly or some other instance of mongo, and then trying to run mongo again while the old one was still running. My terminal would tell me mongo was already running on port 27017. Whenever this happened, I found the PID and killed any mongo PID's.

    $ ps wuax | grep mongo
    
    $ kill (pid number)