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

I have mongo setup with homebrew.
$ brew services start mongodb-community
$ mongosh
I can see my DBs in mongo compass.
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.

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:
Turned off mongo-community:
$ brew services stop mongodb-community
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)

Related

installing MongoDB to Mac

I need help to solve an error that happened while installing MongoDB into Macbook.
I run this command to kick off installing.
brew install mongosh
ALthough I got a warning msg, it seemed like fine.
I waited so long, and the installation got finally done.
Here is the screenshot of the end.
As you can see, I typed below to check if it was successfully installed.
mongosh --version
1.2.2
Finally I tried to run MongoDB, so typed "mongosh". And I got an error to run it and connect to db called cluster0.
mongosh
Current Mongosh Log ID: 621d2adedeee61396fddb367
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.2.2
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
takeichimasahironoMacBook-Air:~ masa$ mongosh "mongodb+srv://cluster0.8tjjn.mongodb.net/myFirstDatabase" --apiVersion 1 --username mongo
Enter password: *****
Current Mongosh Log ID: 621d2b0f5a5370a82628edcc
Connecting to: mongodb+srv://cluster0.8tjjn.mongodb.net/myFirstDatabase?appName=mongosh+1.2.2
MongoServerSelectionError: read ECONNRESET
Any idea to solve this issue?? Thanks for your effort in advance.
In addition to the good answer above -- On newer versions mac os x the root directory is read only so you will need place your mongo data directory somewhere else, see this thread:
Read-only file system when attempting mkdir /data/db on Mac
I also needed to separately install the mongo command line tool from the cask:
$ brew install mongodb-community-shell
I recommend Homebrew for installing and managing applications on macOS. It is installed using the following command in the macOS terminal. Open up the terminal and paste the command:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
The terminal runs through a series of installation operations, and will probably create folders in your local machine to accommodate Homebrews storage requirements. You can find more detailed instructions here. After it's installed, update the Homebrew dependencies and install MongoDB on the command line:
brew update
brew tap mongodb/brew
brew install mongodb-community
It will take a few seconds. Once it's done, create a directory so MongoDB can store its data.
sudo mkdir -p /data/db
Now to make sure this data directory have the right permissions, you'll run this command:
sudo chown -R `id -un` /data/db
Now our data directory is ready with the right permissions. Next run mongo daemon. Which is a service which runs in the background and listens for connections on a given port. Run this command:
mongod
Now mongo daemon will be running in the background and can be used by your applications. Next, check your MongoDB version:
mongo --version
MongoDB shell version v4.2.6
The command-line results will show the version you have installed on your local machine. I recommend using the latest version of libraries and software whenever possible to avoid compatibility issues with client-side applications.

Mongo DB Make directory for the first time [duplicate]

If someone can provide some insights here I would GREATLY appreciate it.
I had a express/node.js app running on MongoDB locally successfully, but upon restarting my computer, I attempted to restart the Mongo server and it began giving errors and wouldn't start. Since then, I have re-installed Mongo several times only to find the same error occurring. this is what I am receiving:
privee:mongodb-osx-x86_64-2.4.6 jonlinton$ ./bin/mongo
MongoDB shell version: 2.4.6
connecting to: test
Mon Aug 26 14:48:47.168 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed
Am I missing a step? Should I be creating a config file?
If you have installed mongodb through homebrew then you can simply start mongodb through (mongodb-community if installted mongodb-community
brew services start mongodb
OR
brew services start mongodb-community
Then access the shell by
mongo
You can shut down your db by
brew services stop mongodb
You can restart your db by
brew services restart mongodb
For more options
brew info mongodb
Updated answer (9/2/2019):
Homebrew has removed mongodb formula from its core repository, see this pull request.
The new way to install mongodb using Homebrew is as follows:
~> brew tap mongodb/brew
~> brew install mongodb-community
After installation you can start the mongodb service by following the caveats:
~> brew info mongodb-community
mongodb/brew/mongodb-community: stable 4.2.0
High-performance, schema-free, document-oriented database
https://www.mongodb.com/
Not installed
From: https://github.com/mongodb/homebrew-brew/blob/master/Formula/mongodb-community.rb
==> Caveats
To have launchd start mongodb/brew/mongodb-community now and restart at login:
brew services start mongodb/brew/mongodb-community
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.conf
Deprecated answer (8/27/2019):
I assume you are using Homebrew. You can see the additional information that you need using brew info $FORMULA
~> brew info mongo 255
mongodb: stable 2.4.6, devel 2.5.1
http://www.mongodb.org/
/usr/local/Cellar/mongodb/2.4.5-x86_64 (20 files, 287M) *
Built from source
From: https://github.com/mxcl/homebrew/commits/master/Library/Formula/mongodb.rb
==> Caveats
To reload mongodb after an upgrade:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Caveats is what you need to follow after installation.
Assuming you have created the data/db directory under bin after install.
Start a terminal for your mongo server
Go to <mongodb-install-directory>/bin directory
Run the command
./mongod
Start a terminal for your mongo shell
Go to <mongodb-install-directory>/bin directory
Run the command (make sure you put the name of the database)
./mongo test
Problem here is you are trying to open a mongo shell without starting a mongo db which is listening to port 127.0.0.1:27017(deafault for mongo db) thats what the error is all about:
Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145 exception: connect failed
The easiest solution is to open the terminal and type
$ mongod --dbpath ~/data/db
Note: dbpath here is "Users/user" where data/db directories are created
i.e., you need to create directory data and sub directory db in your user folder.
For e.g say `
/Users/johnny/data
After mongo db is up. Open another terminal in new window and type
$ mongo
it will open mongo shell with your mongo db connection opened in another terminal.
Mac Installation:
Install brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Update and verify you are good with
brew update
brew doctor
Install mongodb with
brew install mongodb
Create folder for mongo data files:
mkdir -p /data/db
Set permissions
sudo chown -R `id -un` /data/db
Open another terminal window & run and keep running a mongo server/daemon
mongod
Return to previous terminal and run a mongodb shell to access data
mongo
To quit each of these later:
The Shell:
quit()
The Server
ctrl-c
For those that could be facing the same problem and the solutions suggested above aren't working, for example in my case, I had installed mongodb-community, so you might wanna run the command below to restart your mongo server.
For those that installed mongodb-community using brew
brew services start mongodb-community
additionally you may want mongo to run on another port, then paste this command on terminal,
mongod --dbpath /data/db/ --port 27018
where 27018 is the port we want mongo to run on
assumptions
mongod exists in your bin i.e /usr/local/bin/ for mac ( which would be if you installed with brew), otherwise you'd need to navigate to the path where mongo is installed
the folder /data/db/ exists
mongo => mongo-db console
mongodb => mongo-db server
If you're on Mac and looking for a easier way to start/stop your mongo-db server, then MongoDB Preference Pane is something that you should look into. With it, you start/stop your mongo-db instance via UI. Hope it helps!
Make sure you are logged in as root user in your terminal.
Steps to start mongodb server in your mac
Open Terminal
Run the command sudo su
Enter your administrator password
run the command mongod
MongoDb Server starts
Hope it helps you.
Thanks
Nothing less likely to be outdated that the official docs: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
What worked for me on MacOS 12.6.1:
Go to Homebrew copy the "Install Brew " link from there , paste and run .run
To check the version of brew
brew
or
brew -v
then
brew tap mongodb/brew
brew install mongodb-community
sudo mkdir -p /System/Volumes/Data/data/db
sudo chown -R `id -un` /System/Volumes/Data/data/db
sudo mongod --dbpath /System/Volumes/Data/data/db
this would run your MongoDB .
Download MongoDB and install it on your local machine.
Link https://www.mongodb.com/try/download/enterprise
Extract the file and put it on the desktop.
Create another folder where you want to store the data. I have created mongodb-data folder.
Then run the below command.
Desktop/mongodb/bin/mongod --dbpath=/Users/yourname/Desktop/mongodb-data/
Before the hyphen is the executable path of your mongoDB and after hyphen is your data store.

Installed docker and ran a container with mongodb, now brew's mongodb isn't working

I recently installed docker and ran a container with mongodb, now my local brew's mongodb isn't working.
I am using a mac and originally installed mongodb using homebrew. brew services list now shows the mongodb service as 'running' but in yellow (apparently this means unknown), and I cannot connect using mongochef. This began happening after only once stopping the service brew service stop mongodb, as it was preventing me from using docker with this project: https://meanjs.org/. I stopped the service because there was a conflict for the mongodb port. Now even after destroying all the docker containers, I can't get mongodb working on my machine. I have tried reinstalling mongodb using brew, and stopping and starting the service.
I have also tried deleting the file in /tmp as here:
MongoDB Failing to Start - ***aborting after fassert() failure
Any suggestions about where to look and what to do?
So this turned out to be one of those really annoying problems that comes up when you have a lot of other stuff to do. I am still not completely sure if it was one issue or multiple issues. If I was to try and fix it again though, this is the order I would go in:
I removed mongo completely and reinstalled (this finally fixed the mongodb service's status from being shown in yellow instead of in green), as here: https://gist.github.com/katychuang/10439243:
# checks to see if running
launchctl list | grep mongo
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
launchctl remove homebrew.mxcl.mongodb
pkill -f mongod
rm -f ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
brew uninstall mongodb
# double check existence
ls -al /usr/local/bin/mong*
ls -al ~/Library/LaunchAgents
# this was mentioned in the comments
rm -R /usr/local/var/mongodb
However, I think my data was corrupted too. Unfortunately mongod --repair (mongodb error: how do I make sure that your journal directory is mounted) was saying it couldn't repair the data without running with --journaled but that didn't work either. So I deleted my databases in /data/db I didn't need them anyway because I have backups and just restored them easily.
Hope that can help anyone having similar issues.
Sidenote:
The mongod command on the command line gave some helpful output in tracking this down.
Also vim ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist allowed me to see which command was being run:
<string>/usr/local/opt/mongodb/bin/mongod</string>
<string>--config</string>
<string>/usr/local/etc/mongod.conf</string>
This showed me that /usr/local/etc/mongod.conf was being used, and realising that whilst running /usr/local/opt/mongodb/bin/mongod was working, running:
/usr/local/opt/mongodb/bin/mongod --config /usr/local/etc/mongod.conf was not working, helped me narrow the problem down

How to connect a 2.6 mongo shell to a 2.4.9 mongod?

Environment
Linux Mint 17.1
localhost
Default MongoDB port 27017
No username or password needed to connect from script.
Background
I installed MongoDB with:
apt-get install mongodb-10gen=2.4.9
I then 'pinned' the package so that no updates would be applied to it:
echo "mongodb-10gen hold" | sudo dpkg --set-selections
Desired Behaviour
I want to upgrade to the latest version of MongoDB and am looking at the official guide for this process:
http://docs.mongodb.org/manual/release-notes/2.6-upgrade
Early on it states:
To begin the upgrade procedure, connect a 2.6 mongo shell to your MongoDB 2.4 mongos or mongod and run the db.upgradeCheckAllDBs() to check your data set for compatibility.
Question
How do I connect a 2.6 mongo shell to a 2.4.9 mongod?
I would guess that I somehow need to just install the 2.6 mongo shell and run it but:
I don't know how to do that and
I don't want to break anything.
I've done a mongodump of all database so have a backup.
I made an attempt to install mongo 2.6 individually but that caused the other packages to be marked for removal. I ended up just uninstalling the old packages and re-installing the new ones and include the process here for reference.
Some of the steps below didn't work, but i've included them and their results as documentation of what was tried. I had previously done a mongodump of all databases so had a backup.
The end result is that everything currently seems to be 'working' apart from the admin database restore.
01. Shut down mongod
mongo
use admin
db.shutdownServer()
from: http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/
02. Removed the 'pinned package' state of mongodb-10gen with:
echo "mongodb-10gen install" | sudo dpkg --set-selections
from: https://help.ubuntu.com/community/PinningHowto
03. I then tried to install the latest mongo with:
sudo apt-get install -y mongodb-org-shell
from: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
04. The shell version seemed to be updated:
mongo --version
MongoDB shell version: 2.6.6
05. But then I couldn't connect with mongo:
mongo
connecting to: test
2015-01-04T00:08:50.482+1000 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-01-04T00:08:50.483+1000 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
or mongod:
mongod
The program 'mongod' is currently not installed. You can install it by typing:
sudo apt-get install mongodb-server
06. I ran the following to see the history of apt-get installs/removals:
less /var/log/apt/history.log
and got:
Start-Date: 2015-01-04 00:02:08
Commandline: apt-get install -y mongodb-org-shell
Install: mongodb-org-shell:amd64 (2.6.6)
Remove: mongodb-10gen:amd64 (2.4.9)
End-Date: 2015-01-04 00:02:21
07. I ran:
dpkg --get-selections
and saw:
mongodb-10gen deinstall
I think 'deinstall' means it is marked for removal.
08. I decided to remove all mongodb-10gen packages with:
sudo apt-get purge mongodb-10gen
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gir1.2-clutter-gst-2.0 gir1.2-ges-1.0 gstreamer1.0-gnonlin libav-tools
libavdevice53 libavfilter3 libavresample1 libges-1.0-0 libgoocanvas-common
libgoocanvas3 libmlt++3 libmlt-data libmlt6 libqjson0 libquicktime2
libsoprano4 melt python-dateutil python-gst-1.0 python-matplotlib
python-matplotlib-data python-mlt python-pygoocanvas python-pyparsing
python-tz python3-bs4 python3-markdown shared-desktop-ontologies
soprano-daemon wkhtmltopdf
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
mongodb-10gen*
0 to upgrade, 0 to newly install, 1 to remove and 3 not to upgrade.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n]
Y
(Reading database ... 246460 files and directories currently installed.)
Removing mongodb-10gen (2.4.9) ...
Purging configuration files for mongodb-10gen (2.4.9) ...
dpkg: warning: while removing mongodb-10gen, directory '/var/lib/mongodb' not empty so not removed
from: https://askubuntu.com/a/147177
09. I made a backup of the directory that could not be removed ie /var/lib/mongodb:
sudo cp -avr /var/lib/mongodb/ /home/
10. And then removed it.
cd /var/lib/
sudo rm -rf mongodb/
11. I searched to see if any mongodb-10gen packages were remaining:
dpkg --get-selections | grep "mongodb-10gen"
which returned nothing.
12. I installed mongodb-org at version 2.6.6 through Synaptic Package Manager.
13. I checked all mongodb packages were at the same version:
mongodump --version
mongorestore --version
mongo --version
mongod --version
mongos --version
# all returned 2.6.6
14. I then did a mongorestore with each database dump eg:
mongorestore --db dname_01 /path/to/dump/dbname_01/
mongorestore --db dname_02 /path/to/dump/dbname_02/
It seems to have worked on all databases but fails on the admin database:
assertion: 17415 Cannot restore users with schema version 1 to a system with server version 2.5.4 or greater
And the users database threw some anomalies eg:
Restoring to users.pending_registrations without dropping. Restored data will be inserted without raising errors; check your server log
Restoring to users.roles without dropping. Restored data will be inserted without raising errors; check your server log
Restoring to users.users without dropping. Restored data will be inserted without raising errors; check your server log
15. I ran db.upgradeCheckAllDBs():
mongo
use admin
db.upgradeCheckAllDBs()
Checking database local
Checking collection local.startup_log
Checking collection local.system.indexes
Checking database dname_01
...
Checking database admin
Everything is ready for the upgrade!
true
from: http://docs.mongodb.org/manual/release-notes/2.6-upgrade/#preparedness
I know you have managed to solve your question, but the following link helped me and I thought I could post it here for future reference and in case somebody else finds it hard upgrading mongodb:
http://blog.lecstor.com/mongodb-upgrade-24-to-26-in-debian
EDIT: A more appropriate answer, quoting the link above:
You have to download the tar version of MongoDB and run the shell straight from there:
$ curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.6.1.tgz
$ tar -zxvf mongodb-linux-x86_64-2.6.1.tgz
$ ./mongodb-linux-x86_64-2.6.1/bin/mongo
To run the check, you also need to be using the admin database, so..
>use admin
switched to db admin
>db.upgradeCheckAllDBs()
Checking database mydb1
Checking collection mydb1.coll1
Checking collection mydb1.coll2
Checking database mydb2
Checking collection mydb2.coll1
Checking collection mydb2.coll2
Everything is ready for the upgrade!
true

getting an error when firing up mongodb in terminal [duplicate]

I'm getting the following error when I try to run "mongod" in the terminal. I've tried uninstalling, reinstalling, and restarting the machine. Any suggestions on how to get it working would be amazing.
ERROR:
dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
Side note:
Node also stopped working on my machine around the same time that I got this error.
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
Any help would be much appreciated!
This should work to ensure that the directory is set up in the right place so that Mongo can find it:
sudo mkdir -p /data/db/
sudo chown `id -u` /data/db
You need to create the directory on root /data/db or set any other path with the following command :
mongod --dbpath /srv/mongodb/
See the example link
I solved the problem with :
sudo mongod --dbpath=/var/lib/mongodb and then mongo to access the mongodb Shell.
Change the user of the new data directory:
chown mongodb [rute_directory]
And try another time to start the mongo service
service mongod start
I solve the same problem with this.
Daemons (usually ending with d) are normally started as services. Starting the service (daemon) will allow mongodb to work as designed (without permission changes if integrates well with your distro). I start it using the service named mongodb instead of starting mongod directly--on distro with systemd enable on startup then run like:
sudo systemctl enable mongodb
sudo systemctl start mongodb
or, on distro with upstart (if you have /etc/init) or init (if you have /etc/init.d) ( https://www.tecmint.com/systemd-replaces-init-in-linux/ ) instead run:
sudo service mongodb enable
sudo service mongodb start
If you have a distro with rc ("run commands") such as Gentoo (settings in /etc/init.d) (https://forums.gentoo.org/viewtopic-t-854138-start-0.html) run:
rc-update add mongodb default
/etc/init.d/mongodb start
In a distro/version of FreeBSD which still has rc (check whether your version switched to systemd, otherwise see below):
add the following line to /etc/rc.conf:
mongod_enable="YES"
then:
sudo service mongod start
After starting the service, an unpriveleged user can use mongo, and each user will have separate data.
I also got the error that "The file /data/db doesn't exist" when I tried to save my file using the "mkdir -p /data/db" command(using both with and without sudo command). But later on one site, a person named Emil answered that the path "/data/db" no longer works on Mac, so use "~/data/db" instead
i.e., use the command
mkdir -p ~/data/db
instead of previous command.
Moreover, use
mongod --dbpath ~/data/db
to run mongod
It worked for me, hope it work for others too facing the same problem