When running mongo on a remote aws server (ubuntu) what is the best way to keep a mongo instance running after sshing to it to start it?
Take a look here. In short, you should use mongod --fork --logpath /var/log/mongod.log
Start it as a service. Here's the command
sudo service mongod start
You should be able to use the auto-generated init script if you installed the -10gen distro: http://www.mkyong.com/mongodb/how-to-install-mongodb-on-ubuntu/
You can use screen too for this purpose.
First execute the command
screen
Then run your server using
mongod --dbpath=/example/dbpath
Then detach it by pressing ctrl+a and then pressing d
The MongoDB documentation has two useful links
Installing on Ubuntu
http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages
Starting and stopping MongoDB
http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo
You need to use the --fork and --log parameters to detach the process from the ssh login.
Related
I have 3 servers running a MongoDB replica set.
I currently start the node like this after restarting a server:
mongod --replSet "name_of_replicaset" --bind_ip localhost,this_server_hostname_in_host_file --fork --syslog --auth --keyFile "path_to_file"
The problem is that I have to do this every single time I restart a server.
I've checked online and the official documentation for MongoDB but I cannot find a way to make the server startup the node by itself.
Is there a way to have the server startup the node after it restarting?
You can do this using systemctl.
Create a service for this and enable the same via systemctl enable command. It will automatically start once you restart the server.
So, I was making an app that needs to use MongoDB transactions. But the Mongoose documentation told me that "MongoDB currently only supports transactions on replica sets, not standalone servers." So I thought I basically need to switch my Standalone MongoDB instance to Replica Set (whatever that means).
The MongoDB documentation gave me the instruction of how to do this with a few steps:
Shutdown the Standalone MongoDB instance
Restart the instance using the --replSet option
mongod --port 27017 --dbpath /var/lib/mongodb --replSet rs0 --bind_ip localhost
Connect the Mongo Shell
Call rs.initiate() inside the Shell
I'm stuck at step 2. All I know, when you want to start using MongoDB, you have to start its daemon first, using sudo systemctl start mongod, and then start using it by connecting your app. but that step told me to use mongod command to start the Mongod daemon, instead of systemctl. I tried the command but got the following error:
DBException in initAndListen, terminating","attr":{"error":"IllegalOperation: Attempted to create a lock file on a read-only directory: /var/lib/mongodb"}}
At first, I thought it was some kind of a privilege issue, so I ran it again with sudo but then it ended up destroying my entire database and prevented me from starting the MongoDB the "normal way" with giving me errors that I cannot remember.
I just reinstalled the whole MongoDB to get it back to work fine. Now I'm at the same place as yesterday, unable to convert to replica set, only now my entire database is gone. What do I do to enable it?
When running the mongod as a service, use /etc/monogd.conf to set the configuration. Note that the location or name of this file might have been changed in the mongod.service file in your system.
See replication options for how to set that in the file.
I'm using the nginx/PHP/MongoDB stack and trying to set up my development environment on Windows by creating two batch files.
start.bat
cd C:\www\nginx
start nginx
cd C:\www\php
start php-cgi.exe -b 127.0.0.1:9000
cd C:\www\mongodb
start mongod
stop.bat
cd C:\www\nginx
nginx -s quit
cd C:\www\php
taskkill /F /IM php-cgi.exe
cd C:\www\mongodb
mongo --eval "use admin; db.shutdownServer(); quit" # this doesn't work
mongo --eval stop_mongod.js # this doesn't work either
Using taskkill to stop mongod isn't an option, since that may lead to data corruption.
Any suggestions?
I'm not sure that's a proper way to do, sending a kill could probably cause damage to your mongo server, and you'll need to repair your database after in case of crash.
Maybe you already solved this question but here is what I do :
mongo admin --eval "db.shutdownServer()"
I'm automatically connected on the admin's collection and next, I just have to run the extinction.
Here is the official link about how to stop Mongodb properly : http://api.mongodb.org/wiki/current/Starting%20and%20Stopping%20Mongo.html
Best
Open Command Prompt/Powershell as Administrator.
Execute
net stop MongoDB
From the mongo shell documentation:
use dbname
This command does not work in scripted mode. Instead you will need to explicitly define the database in the connection (/dbname in the example above).
Alternately, you can also create a connection within the script:
db2 = connect("server:27017/otherdbname")
I've come up with the following code:
Save the following snippet in stop_mongod.js file:
db = connect("localhost:27017/admin");
db.shutdownServer();
quit();
Adjust the connection string if necessary. Then from the command line or within your batch script:
mongo stop_mongod.js
If the server is running as the foreground process in a terminal, this
can be done by pressing
Ctrl-C
Another way to cleanly shut down a running server is to use the shutdown command,
> use admin
> db.shutdownServer();
Open Command prompt as an administrator
net stop mongodb
From the Windows command line. I'm using MongoDB 3.4 Server on 64-bit Windows 7 Pro SP1.
The following links explain the steps:
configure-a-windows-service-for-mongodb-community-edition
manually-create-a-windows-service-for-mongodb-community-edition
The following console command line terminates MongoDB at start-up:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Windows\System32>"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --remove
2017-12-06T08:15:47.883-0600 I CONTROL [main] Trying to remove Windows service 'MongoDB'
2017-12-06T08:15:47.884-0600 I CONTROL [main] Service 'MongoDB' removed
The MongoDB service create command line (shown below) is located in the Windows registry here:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\MongoDB
To check, do the following:
C:\Windows\System32>sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe\" --service --c
onfig=\"C:\Program Files\MongoDB\Server\3.4\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"
[SC] CreateService SUCCESS
C:\Windows\System32>net start MongoDB
The MongoDB service is starting.
The MongoDB service was started successfully.
C:\Windows\System32>net stop MongoDB
The MongoDB service is stopping.
The MongoDB service was stopped successfully.
C:\Windows\System32>"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --remove
2017-12-06T08:16:36.504-0600 I CONTROL [main] Trying to remove Windows service 'MongoDB'
2017-12-06T08:16:36.505-0600 I CONTROL [main] Service 'MongoDB' removed
C:\Windows\System32>
If you install mongodb as a service on windows. you can stop the service using following command in elevated command prompt
net stop mongodb
I guess, using TASKKILL /IM mongod.exe is fine to terminate the mongodb server gracefully.
I have a Docker image configured with node.js (with express) and mongoDB.
I run the mongod service in the background: mongod --fork --logpath /var/lib/mongodb.log. I start my node.js app: npm start which results in an interactive shell(shows the requests to server).
But if I want to monitor the DB changes being made by my node.js application, each time I am forced to stop the node server (ctrl + c) and launches the mongoDB interactive shell using: mongo.
So the next time if I want to run my node.js app, I had to stop the mongoDB interactive shell (ctrl + c) and run the server all over again.
Is there any way to run both node.js interactive shell and mongoDB interactive shell simultaneously, may be in two different terminal window in Docker ?
The image below shows the snapshot of my terminal.
I am using Ubuntu 15.04 and Docker version 1.5.0, build a8a31ef
I would suggest not running these services in the same container. Run each one in a separate container and use docker-compose to manage building and running the containers.
docker-compose logs will show you the output of each service.
Managing the services in separate containers will let you modify each independently, and gives you an environment that is closer to a production setup.
I would recommend you try installing tmux. You can add the following to your Dockerfile to make tmux available in the container:
RUN apt-get update && apt-get install -y tmux
tmux will provide you with a screen that can represent multiple windows with multiple panes, handling the I/O for you.
Alternatively, you can use CTRL+Z, fg, bg, to change the process your viewing in the foreground. A final solution might be to run docker exec in two separate terminals.
Lastly, not exactly related to your question, you could expose the port to mongod to your host and connect to it via your local mongo CLI client or a GUI client such as Robomongo.
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