MongoDB
MongoDB 安装篇
本文档使用 MrDoc 发布
-
+
首页
MongoDB 安装篇
> 1、基础环境 1. CentOS 6.8 x64 2. libcurl-7.19.7-53.el6_9.x86_64 3. openssl-1.0.1e-57.el6.x86_64 > 2、依赖安装 libcurl openssl ``` yum install libcurl openssl ``` > 3、下载Mongodb源码包、解压 ``` wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.12.tgz mkdir /usr/local/mongodb tar -zxvf mongodb-linux-x86_64-3.6.12.tgz -C /usr/local/ cd /usr/local/ mv mongodb-linux-x86_64-3.6.12/ mongodb ``` > 4、添加环境变量 ``` vim /etc/profile #下面代码加入文件末尾 export PATH=$PATH:/usr/local/mongodb/bin #立刻生效 source /etc/profile #查看版本 mongod --version db version v3.6.12 git version: c2b9acad0248ca06b14ef1640734b5d0595b55f1 allocator: tcmalloc modules: none build environment: distarch: x86_64 target_arch: x86_64 ``` > 5、创建目录 ``` 分别创建 数据/日志/配置 运行目录 mkdir -p /home/mongodb/{data,log,conf} mkdir -p /var/run/mongodb ``` > 配置 mongodb - [x] 一般咱们用 mongod -f xx.conf 启动 ``` vi /home/mongodb/conf/mongod.conf ``` ``` # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: /home/mongodb/mongod.log # Where and how to store data. storage: dbPath: /home/mongodb/data journal: enabled: true # engine: # mmapv1: # wiredTiger: # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfo # network interfaces net: port: 27017 bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces. #security: #operationProfiling: #replication: #sharding: ## Enterprise-Only Options #auditLog: #snmp: setParameter: failIndexKeyTooLong: false ``` > 6、创建 mongodb 用户 ``` useradd mongodb -M -s /sbin/nologin chown -R mongodb.mongodb /usr/local/mongodb chown -R mongodb.mongodb /var/run/mongodb ``` > 7、启动脚本 ``` vi /etc/init.d/mongod ``` ``` #!/bin/sh # # mongodb init file for starting up the MongoDB server # # chkconfig: - 90 10 # description: Starts and stops the MongDB daemon that handles all \ # database requests. # Source function library. . /etc/rc.d/init.d/functions exec="/usr/local/mongodb/bin/mongod" prog="mongod" logfile="/home/mongodb/log/mongodb.log" [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog OPTIONS="--quiet -f /home/mongodb/conf/mongod.conf" pidfile=${PIDFILE-/var/run/mongodb/mongod.pid} options="$MONGODB_OPTIONS $OPTIONS" lockfile="/var/lock/subsys/mongod" # Nicer version of killproc that does not kill mongodb when it takes # a long time to shut down and does not hang for a long time when mongo # shuts down quickly killproc_nice() { local RC base pid pid_file= delay i RC=0; delay=3 # Test syntax. if [ "$#" -eq 0 ]; then echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]" return 1 fi if [ "$1" = "-p" ]; then pid_file=$2 shift 2 fi if [ "$1" = "-d" ]; then delay=$2 shift 2 fi # Save basename. base=${1##*/} # Find pid. __pids_var_run "$1" "$pid_file" RC=$? if [ -z "$pid" ]; then if [ -z "$pid_file" ]; then pid="$(__pids_pidof "$1")" else [ "$RC" = "4" ] && { failure $"$base shutdown" ; return $RC ;} fi fi # Kill it. if [ -n "$pid" ] ; then [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base " if checkpid $pid 2>&1; then # TERM first, then KILL if not dead kill -TERM $pid >/dev/null 2>&1 usleep 100000 # Check every one second if the program is stopped. # Do so for a maximum of $delay seconds for ((i = 0 ; i < $delay; i++)) do if checkpid $pid; then sleep 1 else break fi done # If the program is not stopped, kill it if checkpid $pid ; then kill -KILL $pid >/dev/null 2>&1 usleep 100000 fi fi checkpid $pid RC=$? [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown" RC=$((! $RC)) else failure $"$base shutdown" RC=0 fi # Remove pid file if any. rm -f "${pid_file:-/var/run/$base.pid}" return $RC } start() { [ -x $exec ] || exit 5 echo -n $"Starting $prog: " daemon --pidfile=${pidfile} --user mongodb "$exec $options run >> $logfile 2>&1" retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc_nice -p ${pidfile} -d 300 $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { restart } force_reload() { restart } rh_status() { # run checks to determine if the service is running or use generic status status -p ${pidfile} $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 restart ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac exit $? ``` ``` # 给予可执行权限 chmod a+x /etc/init.d/mongod #注意权限问题: 1. mongodb 的 data log pid 存放 确保有权限 2. 比如登陆用户为root,此时手动启动了mongodb 生成的 sock 则属于root ,你使用 etc/init.d/mongod start 的时候则会则没有操作权限,删除即可 3. 查看详细的错误 OPTIONS="--quiet -f /home/mongodb/conf/mongod.conf --fork --logpath=/home/mongodb/log/mongodb.log" ,进一步排查 ``` > 开机启动(centos6.8) ``` chkconfig --add mongod chkconfig mongod on ```
admin
2022年12月16日 16:04
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码