how do I start mongodb via /etc/init.d/mongodb with a config file? - mongodb

I have a config file for mongo that specifies an alternate port:
deploy#ip-xxx-xxx-22-107 ~/app $ tail /etc/mongodb.conf
port = 27033
Not very complex. I'm trying to use a file based config instead of command line, seems like a better idea. I'm on Ubuntu 11. The docs say:
On some packaged installs of MongoDB (for example Ubuntu & Debian),
the default file can be found in /etc/mongodb.conf, which is
automatically used when starting and stopping MongoDB from the service
I definitely can start mongo with sudo /etc/init.d/mongodb restart but it's starting on the wrong port, 27017:
deploy#ip-xxx-xxx-22-107 ~/app $ sudo lsof -iTCP | grep mongo
mongod 3594 mongodb 5u IPv4 260392 TCP localhost:27017 (LISTEN)
mongod 3594 mongodb 7u IPv4 260395 TCP localhost:28017 (LISTEN)
So there's mongo, but not the right port. Whenever I try passing in a config file to the mongodb command I get an error:
sudo /etc/init.d/mongodb -f /etc/mongodb.conf restart
* ERROR: wrong args ( -f )
I suspect the /etc/init.d/mongodb command isn't passing on my config request when it's starting mongo. But the config file should be read by default according to the docs. Is something wrong with my Ubuntu install (it's on a PaaS host)? Is there no advantage to using /etc/init.d/mongodb so I should just ignore this? I can get it to read the config file via mongod -f /etc/mongodb.conf but my docs say to use /etc/init.d/mongodb.

Where did you install the mongo package from? If you installed from the default repositories, then you may have a very old version - I just tried on 11.04 (don't have 11.10 handy, sorry) and I got 1.6.4 which is very old (current stable release as of writing this is 2.0.2).
You should remove the version you have, and add the one from the 10gen/MongoDB repositories. The instructions for doing so are here:
http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages
That will give you the latest mongod version and likely resolve your issues, because you seem to be doing things correctly.
Once you have the mongod started, then connect to it and try running this:
use admin
db.runCommand({getCmdLineOpts: 1})
That should give you all the passed and parsed options. There is a sample output shown on the Docs page for the command. It should allow you to see where the mongod is pointing for the config file and what is being parsed from it.

Related

MongoDB server with OS Catalina

I upgrade my Mac OS to Catalina and now my server is not working well. I reinstalled all the system from scratch and reinstall with brew, however when trying to connect to Mongo website learning database the system does not allow through the shell. It returns in the end of the body the following message:
"ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":48}
I don't know about MAC OS but I faced the same issue on my Ubuntu 20.04 recently. I was trying to run the mongodb server on local machine using a specified path data which is a directory(folder). But it was giving me the same exitcode:48 as mentioned by you. I solved it by stopping the mongod server running on my system using the command:
sudo systemctl stop mongod
And then executed the command:
mongod --dbpath=data --bind_ip 127.0.0.1
With the following code, on Terminal you can find PID for mongodb and kill it:
lsof -i :27017 | awk '{print $2}' | tail -n 1 | xargs kill -9
The default mongodb port number is 27017. You can change it if you have another mongodb port number.
FYI, awk, tail are just extracting the PID number for the given port and, if it exists, then it goes and terminates it.
You already tried to specify another port?
Like mongo shell: mongod --port 27018
In my case, I went through the error, and found it was due to the permission issue for,
data/db folder creation
it was not able to open the socket.
Fixed it by below
created the data/db folder in my convenient directory, ie in my common codebase folder
> mkdir data && cd data && mkdir db
> pwd
/User/{uname}/codebase/data/db
mongob --dbpath="/User/{uname}/codebase/data/db"

Change default MongoDB port on Ubuntu Snap installation

I have installed MongoDB using using the following command using snap package manager.
snap install mongo32;
It's working fine. However, I want to change the port & I couldn't find mongodb.conf for this installation. I searched for it in the following locations:
/snap/mongo32
/etc
Recursive grep search gave me the file which contains default port number(32017) for this installation in this file:
/snap/mongo32/2/command-mongod.wrapper
However, this file is read-only, and I assume that it is not a good idea to make changes to a read-only file. Please advice how should I change the default port for mongo32 snap installation. Thanks.
You can see in the snapcraft manifest file here that the default port number is hardcoded:
mongod:
command: bin/run.sh mongod --port=32017 --smallfiles --noprealloc
The mongo snaps follow the convention mongoNN where NN is the port number NN017 (e.g. mongo33 will use the port 33017)

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

Mongo is letting me down

I've been trying to run mongo on my system, but I just doesn't want to start. I've installed mongodb with brew. Both mongo and mongod are on my system and I can use them. But the mongod process won't start.
These are the steps I'm taking after I installed mongo on my system with brew.
$ mongod
all output going to: /usr/local/var/log/mongodb/mongo.log
$
It seems mongod is closing directly, because I don't see any process running and it gives me the dollar sign back again. Thereby there's no rotating icon in my terminal, so it isn't running.
If I open a new window in the terminal after running the above command, I'm getting this error:
$ mongo
MongoDB shell version: 2.4.3
connecting to: test
Thu May 23 12:22:09.314 JavaScript execution failed: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112
exception: connect failed
$
This output is a logical error seen from the fact mongod didn't do a thing. But when I run this:
$ ps -ef | grep mongod
501 99123 15827 0 12:24PM ttys001 0:00.00 grep mongod
$
So I'm not sure or it's running or not.. You guys know a solution for this? Because I can't figure out what I'm doing wrong..
update
I've tried to change the db path and that works partially. I'm now able to run mongod command, but I need to specify the dbPath even though I have the dbPath changed in the config file.
$ mongod --dbpath ~/data/db/
And when I try to run mongod by itself, it won't run..
$ mongod
all output going to: /usr/local/var/log/mongodb/mongo.log
$
I don't have a working solution, but from your output, it seems like mongod is running daemonized. You could try setting the 'fork' option to false (or uncommenting it) in /usr/local/Cellar/mongodb/2.4.x-x86_64/mongod.conf and running mongod with the -v / --verbose flag to see if that gives you more details.
Also, are you sure you're actually using the homebrew version of mongo and not some previously installed version? You can check which mongod is used by running:
➜ ~ which mongod
/usr/local/bin/mongod
And the following to see if that's indeed the version that got installed using homebrew:
➜ ~ ls -l /usr/local/bin/mongod
lrwxr-xr-x 1 user admin 41 21 mrt 22:06 /usr/local/bin/mongod -> ../Cellar/mongodb/2.4.x-x86_64/bin/mongod
The default data directory is /data/db. In order to have that work by default you need to do two things:
sudo mkdir -p /data/db
sudo chown `whoami` /data/db

Mongodb replications sets...tying ot get tutorial to work

I am trying to do the Replica Sets tutorial for mongodb. I am using ubuntu on 64 bit and installed using sudo apt-get install mongodb-10gen.
Tutorial is at http://www.mongodb.org/display/DOCS/Replica+Sets+-+Basics
I stop the service using
/etc/init.d/mongodb stop
I then try:
/etc/init.d/mongodb start --rest --replSet myset
When I go to http://localhost:28017/_replSet I get this message.
REST is not enabled. use --rest to turn on. check that port 28017 is
secured for the network too.
So...how do I resolve? How do I open a port on ubuntu is that is the problem? Did I do things correct?
Thanks
You need to edit your /etc/mongodb.conf file , setting rest = true and replSet = myset, and restart mongo.
Init.d script don't accept parameters. Or: /etc/init.d/mongodb is not the same as /usr/bin/mongod - The parameters you're passing are not making it to the mongo executable.