Mongo DB Make directory for the first time [duplicate] - mongodb

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.

Related

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.
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)

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.

strapi start command not working

I want to start strapi cms in the browser but Tells me :
Make sure your MongoDB database is running
In the event MongoDB is running
How can I solve this problem?
Mongod & mongo is running
I had the same problem and I had to update mongo to 3.6, stop the mongod service using
$ sudo service mongod stop
and run mongod in a terminal and leave it open.
$ mongod
The next time it seemed to recognise the mongo service started by the command
$ sudo service mongod start
Go to your project file.
go to the config/environments/development folder and click on database.json. for username and password you leave it empty. and host you put 127.0.0.1 port: 27017 as in the picture
if you have finished the modifications you open two terminal:
on the first you start mongoDB by doing:
mongod
and once mongoDB has started you start on the other console strapi by making:
strapi start or npm start
Try removing the new plugins (if any) that you have installed.
I have faced this issue 2 times
Once I installed strapi command plugin
Another time I installed seo plugin.
I do no the root cause. But removing those packages and re-installed worked.

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

sudo service mongodb restart gives "unrecognized service error" in ubuntu 14.0.4

I just installed mongoDB on ubuntu 14.0.4.
I tried to start the shell but I'm getting a connection refused error.
me#medev:/etc/init.d$ mongo
MongoDB shell version: 2.6.5
connecting to: test
2014-11-10T15:06:28.084-0500 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-11-10T15:06:28.085-0500 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
So I decided to try to restart the service but that's failing too. I get the following error message:
me#medev:/etc/init.d$ sudo service mongodb restart
mongodb: unrecognized service
me#medev:/etc/init.d$
This is what I have in my /var/log/mongodb/mongod.log - http://pastebin.com/MrHt8tce
what i've tried so far:
I found another post here: can't start mongodb as sudo
which made a comment about remove the mongo lock file.
I deleted the lock file and then retried my command but it still fails as you can see below:
me#medev:/var/lib/mongodb$ sudo rm mongod.lock
me#medev:/var/lib/mongodb$ ls
journal local.0 local.ns _tmp
me#medev:/var/lib/mongodb$ sudo service mongodb start
mongodb: unrecognized service
But I can start it using /etc/init.d as you can see below:
me#medev:/var/lib/mongodb$ sudo /etc/init.d/mongod start
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mongod start
Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the start(8) utility, e.g. start mongod
mongod start/running, process 27469
me#medev:/var/lib/mongodb$ ls
journal local.0 local.ns mongod.lock
me#medev:/var/lib/mongodb$ mongo
MongoDB shell version: 2.6.5
connecting to: test
> db
test
>
Any ideas on why I can't start it using the service command would be appreciated. From what I've read, i should be using sudo service mongodb
Try this:
Write mongodb instead of mongod
sudo service mongodb status
I got the same error one day You should use this:
1.Get the status of your mongo service:
/etc/init.d/mongod status
or
sudo service mongod status
2.If it's not started repair it like this:
sudo rm /var/lib/mongodb/mongod.lock
mongod --repair
sudo service mongodb start
And check again if the service is started again(1)
For me the solution was to replace
service mongod start
with
start mongod
You need to make sure the file (ex. /etc/init.d/mongodb) has execute permissions.
chmod +x /etc/init.d/mongodb
For debian, from the 10gen repo, between 2.4.x and 2.6.x, they renamed the init script /etc/init.d/mongodb to /etc/init.d/mongod, and the default config file from /etc/mongodb.conf to /etc/mongod.conf, and the PID and lock files from "mongodb" to "mongod" too. This made upgrading a pain, and I don't see it mentioned in their docs anywhere. Anyway, the solution is to remove the old "mongodb" versions:
update-rc.d -f mongodb remove
rm /etc/init.d/mongodb
rm /var/run/mongodb.pid
diff -ur /etc/mongodb.conf /etc/mongod.conf
Now, look and see what config changes you need to keep, and put them in mongod.conf.
Then:
rm /etc/mongodb.conf
Now you can:
service mongod restart
I installed mongo server on Debian Jessie using manual from official site.
It didn't started after recommended command sudo service mongod restart with the same error - mongodb: unrecognized service.
After looking into installed package contents, I noticed that it contains only Systemd service unit, but no SystemV init script:
# dpkg -L mongodb-org-server
/.
/usr
/usr/bin
/usr/bin/mongod
/usr/share
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/mongodb-org-server
/usr/share/doc
/usr/share/doc/mongodb-org-server
/usr/share/doc/mongodb-org-server/LICENSE-Community.txt
/usr/share/doc/mongodb-org-server/README
/usr/share/doc/mongodb-org-server/copyright
/usr/share/doc/mongodb-org-server/changelog.gz
/usr/share/doc/mongodb-org-server/GNU-AGPL-3.0.gz
/usr/share/doc/mongodb-org-server/THIRD-PARTY-NOTICES.gz
/usr/share/doc/mongodb-org-server/MPL-2.gz
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/mongod.1.gz
/etc
/etc/mongod.conf
/lib
/lib/systemd
/lib/systemd/system
/lib/systemd/system/mongod.service
But my system was running on SysV init:
# stat /proc/1/exe
File: '/proc/1/exe' -> '/sbin/init'
So, there are 2 options now:
(Continue on SysV) Write sysV init script manually as #khylo mentioned above
(Switch to SystemD) and run systemctl start mongod
For me nothing have helped, I've ended up with a solution:
create /lib/systemd/system/mongod.service file with content
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
then start/stop commands should work
$ sudo service mongod start
For reference - I have Ubuntu 14.04 LTS, MongoDB 3.2.9 installed from
deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse
You can use mongod command instead of mongodb, if you find any issue regarding dbpath in mongo you can use my answer in the link below.
https://stackoverflow.com/a/53057695/8247133
I think you may have installed the version of mongodb for the wrong system distro.
Take a look at how to install mongodb for ubuntu and debian:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
I had a similar problem, and what happened was that I was installing the ubuntu packages in debian
Original Source - https://www.techrepublic.com/article/how-to-install-mongodb-community-edition-on-ubuntu-linux/
If you're on Ubuntu 16.04 and face the unrecognized service error, these instructions will fix it for you:-
Open a terminal window.
Issue the command sudo apt-key adv —keyserver hkp://keyserver.ubuntu.com:80 —recv EA312927
Issue the command sudo touch /etc/apt/sources.list.d/mongodb-org.list
Issue the command sudo gedit /etc/apt/sources.list.d/mongodb-org.list
Copy and paste one of the following lines from below (depending upon your release) into the open file.
For 12.04: deb http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.6 multiverse
For 14.04: deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 multiverse
For 16.04: deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse
Make sure to edit the version number with the appropriate latest version and save the file.
Installation
Open a terminal window and issue command sudo apt-get update && sudo apt-get install -y mongodb-org
Let the installation complete.
Running MongoDB To start the database, issue the command sudo service mongodb start. You should now be able to issue the command to see that MongoDB is running: systemctl status mongodb
Ubuntu 16.04 solution
If you are using Ubuntu 16.04, you may run into an issue where you see the error mongodb: unrecognized service due to the switch from upstart to systemd. To get around this, you have to follow these steps.
If you added the /etc/apt/sources.list.d/mongodb-org.list, remove it with the command sudo rm /etc/apt/sources.list.d/mongodb-org.list
Update apt with the command sudo apt-get update
Install the official MongoDB version from the standard repositories with the command sudo apt-get install mongodb in order to get the service set up properly
Remove what you just installed with the command sudo apt-get remove mongodb && sudo apt-get autoremove
Now follow steps 1 through 5 listed above to install MongoDB; this should re-install the latest version of MongoDB with the systemd services already in place. When you issue the command systemctl status mongodb you should see that the server is active.
I mostly copy pasted the above (with minor modifications and typo fixes) from here - https://www.techrepublic.com/article/how-to-install-mongodb-community-edition-on-ubuntu-linux/
This is a simple solution that worked for me with the same problem (I think):
mv /var/lib/mongodb /var/lib/mongodb_backup
mkdir /var/lib/mongodb
chmod 700 /var/lib/mongodb
chown mongodb:daemon /var/lib/mongodb
systemctl restart mongodb or service mongod restart
If you're running Ubuntu in WSL (Windows Subsystem for Linux), you will have issues because WSL does not currently support systemd.
The link below explains how to run MongoDB without systemd, and even how to add a script for using the service command with WSL.
https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database#mongodb-init-system-differences
tutorials may start MongoDB using the operating system's built-in init system. You might see the command sudo systemctl status mongodb used in tutorials or articles. Currently WSL does not have support for systemd (a service management system in Linux).
You shouldn't notice a difference, but if a tutorial recommends using sudo systemctl, instead use: sudo /etc/init.d/. For example, sudo systemctl status docker, for WSL would be sudo /etc/init.d/docker status ...or you can also use sudo service docker status.