MongoDB Start and stop

Source: Internet
Author: User
Tags sigint signal

MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for Web applications. It is widely used for its advantages such as high performance, easy to deploy, easy to use and easy to store data. Its installation configuration is fairly simple, like the lightweight MySQL, but features tittle. This article mainly describes the start and stop of MongoDB.

First, prepare the environment


1. Current Environment

# More/etc/redhat-release

CentOS Release 6.7 (Final)


2, Installation Mongod

# which Mongod

/var/lib/mongodb/bin/mongod


Reference:

Fast installation of MongoDB under Linux

Installing MongoDB under Windows platform


3. Create Data Catalog

# MKDIR-PV/DATA/MONGODATA/{RS1,RS2,RS3}

# mkdir-pv/var/lib/mongodb/conf


Second, start MongoDB


1, start MongoDB based on command line mode

# Mongod--dbpath=/data/mongodata/rs1--logpath=/data/mongodata/rs1/rs1.log &


The default port is

[Email protected] rs1]# Netstat-nltp|grep Mongod

TCP 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 5062/mongod


2. configuration file-based command line startup

Vi/var/lib/mongodb/conf/rs2.conf


Port = 27000

DBPath =/data/mongodata/rs2

LogPath =/data/mongodata/rs2/rs2.log

Smallfiles = True

Fork = True

Pidfilepath =/var/run/mongo.pid


# Mongod--config/var/lib/mongodb/conf/rs2.conf &


# # Author:leshami

# # Blog:http://blog.csdn.net/leshami


# Netstat-nltp|grep 27000

TCP 0 0 0.0.0.0:27000 0.0.0.0:* LISTEN 5356/mongod


3. Start MongoDB in daemon mode

# Mongod--dbpath=/data/mongodata/rs3--logpath=/data/mongodata/rs1/rs3.log--fork--port 28000


# Netstat-nltp|grep Mongod

TCP 0 0 0.0.0.0:28000 0.0.0.0:* LISTEN 5465/mongod

TCP 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 5435/mongod

TCP 0 0 0.0.0.0:27000 0.0.0.0:* LISTEN 5448/mongod


4, the use of system services to start Mogodb

Startup scripts


# Vi/etc/init.d/mongod


#!/bin/sh

# chkconfig:2345 93 18


#MogoDB Home Directory

Mongodb_home=/var/lib/mongodb


#mongodb command

mongodb_bin= $MONGODB _home/bin/mongod


#mongodb config file

mongodb_conf= $MONGODB _home/conf/mongodb.conf


#mongodb PID

Mongodb_pid=/var/run/mongo.pid


#set Open File limit

system_maxfd=65535


Mongodb_name= "MongoDB"

. /etc/rc.d/init.d/functions


if [!-F $MONGODB _bin]

Then

echo "$MONGODB _name Startup: $MONGODB _bin not exists!"

Exit

Fi


Start () {

ULIMIT-HSN $SYSTEM _maxfd

$MONGODB _bin--config= "$MONGODB _conf"--fork # #added @20160901

Ret=$?

If [$ret-eq 0]; Then

Action $ "Starting $MONGODB _name:"/bin/true

Else

Action $ "Starting $MONGODB _name:"/bin/false

Fi


}


Stop () {

pid=$ (PS aux |grep "$MONGODB _name" |grep "$MONGODB _conf" |grep-v grep |wc-l)

if [[$PID-eq 0]];then

Action $ "Stopping $MONGODB _name:"/bin/false

Exit

Fi

Kill-hup ' Cat $MONGODB _pid '

Ret=$?

If [$ret-eq 0]; Then

Action $ "Stopping $MONGODB _name:"/bin/true

Rm-f $MONGODB _pid

Else

Action $ "Stopping $MONGODB _name:"/bin/false

Fi


}


Restart () {


Stop

Sleep 2

Start

}


Case "$" in

Start

Start

;;

Stop

Stop

;;

Status

Status $prog

;;

Restart

Restart

;;

*)

echo $ "Usage: $ {Start|stop|status|restart}"

Esac


# chmod U+x/etc/init.d/mongod


# service Mongod Start

About-to-fork child process, waiting until server was ready for connections.

Forked process:5543

Child process started successfully, parent exiting

Starting MongoDB: [OK]


Third, stop MongoDB


1. Send a signal to the Mongod process

# # #SIGINT信号


# Ps-ef|grep Mongod|grep rs1

Root 5435 4914 1 19:13 pts/2 00:00:14 mongod--dbpath=/data/mongodata/rs1--logpath=/data/mongodata/rs1/rs1.lo G


# Kill-2 5435


2016-08-30t17:02:00.528+0800 I Control[signalprocessingthread] got signal 2 (Interrupt), would terminate after current CMD Ends

2016-08-30t17:02:00.530+0800 I REPL [signalprocessingthread] stopping replication applier threads

2016-08-30t17:02:00.554+0800 I STORAGE [conn1253] got request after shutdown ()

2016-08-30t17:02:00.774+0800 I CONTROL [Signalprocessingthread] now exiting

2016-08-30t17:02:00.774+0800 I NETWORK [Signalprocessingthread] shutdown:going to close listening sockets ...

2016-08-30t17:02:00.774+0800 I NETWORK [signalprocessingthread] closing listening socket:6

2016-08-30t17:02:00.775+0800 I NETWORK [signalprocessingthread] closing listening socket:7

2016-08-30t17:02:00.775+0800 I NETWORK [signalprocessingthread] removing socket file:/tmp/mongodb-27017.sock

2016-08-30t17:02:00.775+0800 I NETWORK [Signalprocessingthread] shutdown:going to flush diaglog ...

2016-08-30t17:02:00.775+0800 I NETWORK [Signalprocessingthread] shutdown:going to close sockets ...

2016-08-30t17:02:00.775+0800 I STORAGE [signalprocessingthread] shutdown:waiting for FS preallocator ...

2016-08-30t17:02:00.775+0800 I STORAGE [signalprocessingthread] shutdown:final commit ...

2016-08-30t17:02:00.775+0800 I JOURNAL [Signalprocessingthread] journalcleanup ...

2016-08-30t17:02:00.775+0800 I JOURNAL [Signalprocessingthread] Removejournalfiles

2016-08-30t17:02:00.777+0800 I NETWORK [conn1254] End connection 192.168.1.247:58349 (0 connections now OPEN)

2016-08-30t17:02:00.779+0800 I JOURNAL [signalprocessingthread] terminating durability thread ...

2016-08-30t17:02:00.881+0800 I JOURNAL [JOURNAL writer] JOURNAL writer thread stopped

2016-08-30t17:02:00.882+0800 I JOURNAL [durability] durability thread stopped

2016-08-30t17:02:00.882+0800 I STORAGE [signalprocessingthread] shutdown:closing All Files ...

2016-08-30t17:02:00.884+0800 I STORAGE [Signalprocessingthread] Closeallfiles () finished

2016-08-30t17:02:00.884+0800 I STORAGE [Signalprocessingthread] shutdown:removing FS lock ...

2016-08-30t17:02:00.885+0800 I CONTROL [Signalprocessingthread] dbexit:rc:0


# # #SIGTERM信号

# Ps-ef|grep Mongod|grep Rs3


# Ps-ef|grep Mongod|grep Rs3

Root 5465 1 1 19:14? 00:00:13 mongod--dbpath=/data/mongodata/rs3--logpath=/data/mongodata/rs1/rs3.log--fork--port 28000

# kill-4 5465


Signal Generation method

SIGINT via CTRL + C will send this signal to the process when

The Sigterm kill command does not add parameters to send this signal.


Impact on the process

The SIGINT signal is received by the current process tree, which means that not only the current process will receive a signal, but its child processes will also receive

Sigterm only the current process receives a signal and the child process does not receive it. If the current process is killed, then the parent process of its child process will be init, which is the PID 1 process


After the above signal is issued

New connection requests are no longer accepted

Wait for an existing connection to finish processing

To close all open connections

Writes out the memory data to the disk

Safe stop


2. Stop Mongod using the System service script method

# Ps-ef|grep Mongod

Root 5675 1 3 19:33? 00:00:00/var/lib/mongodb/bin/mongod--config=/var/lib/mongodb/conf/rs2.conf

Root 5689 4950 0 19:33 pts/3 00:00:00 grep mongod

[Email protected] conf]#

[[Email protected] conf]# service Mongod stop

Stopping MongoDB: [OK]


3, Db.shutdownserver () mode

# MONGO localhost:27000

> Use admin

> Db.shutdownserver ()


4. Use the command line mode to close (supplemental @20160901)

# mongod-f/etc/mongo-m.conf--shutdown


5. Forced closure of Mongod

# kill-9 5675

Disadvantages:

Database is closed directly

Data loss

Data files are easily damaged (need to be repaired)


MongoDB Start and stop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.