mongod作为数据实例,本身已经集成编译所需的依赖。
CONFIGFILE="/opt/mongodbdata/mongod.conf" OPTIONS=" -f $CONFIGFILE" mongod=${MONGOD-/usr/local/mongodb/bin/mongod} daemon=${MONGOD-/usr/local/mongodb/bin/mongod} MONGO_USER=mongod MONGO_GROUP=mongod start() { ... echo -n $"Starting mongod: " $daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1" #备注:核心启动命令:mongod也被定义也了变量引用。如果$daemon没有定义,则核心命令无法执行,报错日志,也不会有了。 ... }
journalctl -u mongod.service
journalctl -u mongod.service #查看systemd启动日志,init.d暂不讨论。 #如下是日志 -- Logs begin at Fri 2020-02-28 10:55:02 CST, end at Sat 2020-02-29 09:10:01 CST. -- Feb 28 11:13:28 Ecloud systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database.... Feb 28 11:13:28 Ecloud runuser[16279]: pam_unix(runuser:session): session opened for user mongod by (uid=0) Feb 28 11:13:28 Ecloud runuser[16279]: pam_unix(runuser:session): session closed for user mongod Feb 28 11:13:28 Ecloud mongod[16269]: Starting mongod: [FAILED] Feb 28 11:13:28 Ecloud systemd[1]: mongod.service: control process exited, code=exited status=1 Feb 28 11:13:28 Ecloud systemd[1]: Failed to start SYSV: Mongo is a scalable, document-oriented database.. Feb 28 11:13:28 Ecloud systemd[1]: Unit mongod.service entered failed state. Feb 28 11:13:28 Ecloud systemd[1]: mongod.service failed.
[root@ksjk ~]# /etc/init.d/mongod status ● mongod.service - SYSV: Mongo is a scalable, document-oriented database. Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled) Active: failed (Result: exit-code) since 三 2020-03-04 18:56:57 CST; 2s ago Docs: man:systemd-sysv-generator(8) Process: 3411 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=1/FAILURE) 3月 04 18:56:52 ksjk systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database.... 3月 04 18:56:53 ksjk runuser[3422]: pam_unix(runuser:session): session opened for user mongod by (uid=0) 3月 04 18:56:53 ksjk runuser[3422]: pam_unix(runuser:session): session closed for user mongod 3月 04 18:56:53 ksjk mongod[3411]: Starting mongod: [失败] 3月 04 18:56:57 ksjk systemd[1]: mongod.service: control process exited, code=exited status=1 3月 04 18:56:57 ksjk systemd[1]: Failed to start SYSV: Mongo is a scalable, document-oriented database.. 3月 04 18:56:57 ksjk systemd[1]: Unit mongod.service entered failed state. 3月 04 18:56:57 ksjk systemd[1]: mongod.service failed.
(1)创建服务文件:
sudo vim /usr/lib/systemd/system/mongodb.service
(2)写入以下内容:
[Unit] Description=mongodb After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/bin/mongodb.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/bin/mongodb.conf PrivateTmp=true [Install] WantedBy=multi-user.target(3)重载系统服务, 启动
systemctl daemon-reload systemctl start mongodb.service
官方参考:https://github.com/mongodb/mongo/blob/master/rpm/mongod.service 节选修改自yum install mongod 获取的monod.service
[Unit] Description=MongoDB Database Server Documentation=https://docs.mongodb.org/manual After=network.target [Service] #User=mongod #Group=mongod Environment="OPTIONS=-f /etc/mongod.conf" #EnvironmentFile=-/etc/sysconfig/mongod #ExecStartPre=/usr/bin/echo never > /sys/kernel/mm/transparent_hugepage/enabled #ExecStartPre=/usr/bin/echo never > /sys/kernel/mm/transparent_hugepage/defrag #ExecStart=/usr/local/mongodb/bin/mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork #ExecStart=/usr/bin/mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork ExecStart=/usr/bin/numactl --interleave=all /usr/local/mongodb/bin/mongod $OPTIONS --fork #ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb #ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb #ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb #PermissionsStartOnly=true #PIDFile=/var/run/mongodb/mongod.pid Type=forking # file size LimitFSIZE=infinity # cpu time LimitCPU=infinity # virtual memory size LimitAS=infinity # open files LimitNOFILE=64000 # processes/threads LimitNPROC=64000 # locked memory LimitMEMLOCK=infinity # total threads (user+kernel) TasksMax=infinity TasksAccounting=false # Recommended limits for for mongod as specified in # http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings [Install] WantedBy=multi-user.target
请优先参考官方提供:https://github.com/mongodb/mongo/blob/master/rpm/init.d-mongod https://github.com/mongodb/mongo/blob/master/rpm/
#!/bin/bash # mongod - Startup script for mongod # chkconfig: 35 85 15 # description: Mongo is a scalable, document-oriented database. # processname: mongod # config: /etc/mongod.conf . /etc/rc.d/init.d/functions # NOTE: if you change any OPTIONS here, you get what you pay for: # this script assumes all options are in the config file. CONFIGFILE="/etc/mongod.conf" OPTIONS=" -f $CONFIGFILE" mongod=${MONGOD-/usr/bin/mongod} MONGO_USER=mongod MONGO_GROUP=mongod # All variables set before this point can be overridden by users, by # setting them directly in the SYSCONFIG file. Use this to explicitly # override these values, at your own risk. SYSCONFIG="/etc/sysconfig/mongod" if [ -f "$SYSCONFIG" ]; then . "$SYSCONFIG" fi # Handle NUMA access to CPUs (SERVER-3574) # This verifies the existence of numactl as well as testing that the command works NUMACTL_ARGS="--interleave=all" if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null then NUMACTL="numactl $NUMACTL_ARGS" else NUMACTL="" fi # things from mongod.conf get there by mongod reading it PIDFILEPATH="`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' \"$CONFIGFILE\" | tr -d \"[:blank:]\\"'\" | awk -F'#' '{print $1}'`" PIDDIR=`dirname $PIDFILEPATH` start() { # Make sure the default pidfile directory exists if [ ! -d $PIDDIR ]; then install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR fi # Make sure the pidfile does not exist if [ -f "$PIDFILEPATH" ]; then echo "Error starting mongod. $PIDFILEPATH exists." RETVAL=1 return fi # Recommended ulimit values for mongod or mongos # See https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings # ulimit -f unlimited ulimit -t unlimited ulimit -v unlimited ulimit -n 64000 ulimit -m unlimited ulimit -u 64000 ulimit -l unlimited echo -n $"Starting mongod: " daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1" RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod } stop() { echo -n $"Stopping mongod: " mongo_killproc "$PIDFILEPATH" $mongod RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod } restart () { stop start } # Send TERM signal to process and wait up to 300 seconds for process to go away. # If process is still alive after 300 seconds, send KILL signal. # Built-in killproc() (found in /etc/init.d/functions) is on certain versions of Linux # where it sleeps for the full $delay seconds if process does not respond fast enough to # the initial TERM signal. mongo_killproc() { local pid_file=$1 local procname=$2 local -i delay=300 local -i duration=10 local pid=`pidofproc -p "${pid_file}" ${procname}` if [ ! -f "${pid_file}" ]; then echo "No PID file detected, nothing to stop" return 0 fi # Per the man page the process name should always be the second # field. In our case mongod is wrapped in parens hence the parens in # the if condition below. local stat_procname=`cat /proc/$pid/stat | cut -d" " -f2` # $procname is the full path to the mongod binary but the process # name will only match the binary's file name. local binary_name=`basename $procname` if [ "($binary_name)" != "$stat_procname" ]; then echo "PID file may have been tampered with, refusing to kill process" echo "Expected (${binary_name}) but found ${stat_procname}" return 1 fi # This doesn't actually "daemonize" this process. All this function # does (defined in /etc/init.d/function) is run a process as another # user in a way that doesn't require sudo or other packages which # are not guaranteed to exist on any given system. # # The check flag here can be ignored it doesn't do anything except # prevent the daemon function's PID checking from throwing an error. daemon --check "$mongod" --user "$MONGO_USER" "kill -TERM $pid >/dev/null 2>&1" usleep 100000 local -i x=0 while [ $x -le $delay ] && checkpid $pid; do sleep $duration x=$(( $x + $duration)) done daemon --check "$mongod" --user "$MONGO_USER" "kill -KILL $pid >/dev/null 2>&1" usleep 100000 checkpid $pid # returns 0 only if the process exists local RC=$? [ "$RC" -eq 0 ] && failure "${procname} shutdown" || rm -f "${pid_file}"; success "${procname} shutdown" RC=$((! $RC)) # invert return code so we return 0 when process is dead. return $RC } RETVAL=0 case "$1" in start) start ;; stop) stop ;; restart|reload|force-reload) restart ;; condrestart) [ -f /var/lock/subsys/mongod ] && restart || : ;; status) status $mongod RETVAL=$? ;; *) echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}" RETVAL=1 esac exit $RETVAL