用户工具

站点工具


docs:mongodb:mongodb_troubleshooting

写在前面

mongod作为数据实例,本身已经集成编译所需的依赖。

  1. mongod服务进程无法手动启动时,多是由于配置文件配置不正确
  2. 如果可以手动执行核心命令启动服务,但init.d或systemd服务无法启动,则是要进一排查这此服务启动配置脚本了。
  3. 主程序目录、数据库目录要添加用户权限。
  4. 数据库目录在初次启动后将自动进行初始化。(对比mysql需要单独初始操作)

FAQ

重中之重

  1. init.d启动脚本中的变量(特别是核心启动命令参数对应的变量是否已经正常定义和识别,或者是复制时丢失了。)

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.

附录

mongod.service配置文件参考

(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
  

systemd示例2

官方参考: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

init.d启动脚本参考

请优先参考官方提供: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

docs/mongodb/mongodb_troubleshooting.txt · 最后更改: 2020/09/25 21:49 (modify by Amos)