欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

centos7mongoDB启动与停止、环境搭建

程序员文章站 2022-03-08 22:49:34
一、环境搭建 Step 1: 禁用 SELINUX=disabled [root@k8s-master conf]# vi /etc/selinux/config # T...

一、环境搭建

Step 1: 禁用 SELINUX=disabled

[root@k8s-master conf]# vi /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
# SELINUX=disabled
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

Step 2: 下载安装文件

下载地址: https://www.mongodb.org/downloads.
或者直接在命令提示符下使用curl命令下载
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz


Step 3: 解压下载的文件
# pwd
/usr/local/src
# tar -xvf mongodb-linux-x86_64-rhel62-3.0.6.gz ###注,本文直接从网站下载,所以文件为.gz
Step 4: 复制解压文件到运行目录
# mkdir -p /var/lib/mongodb
# cp -R -n /usr/local/src/mongodb-linux-x86_64-rhel62-3.0.6/. /var/lib/mongodb/
Step 5: 设置环境变量
# vi /etc/profile
export PATH=/var/lib/mongodb/bin:$PATH
# source /etc/profile
Step 6: 创建数据目录
# mkdir -p /data/mongodata

# mkdir -pv /data/mongodata/{rs1,rs2,rs3}

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


二,启动方式

1、基于命令行方式启动mongodb
# mongod --dbpath=/data/mongodata/rs1 --logpath=/data/mongodata/rs1/rs1.log &


缺省端口为
[root@node3 rs1]# netstat -nltp|grep mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 5062/mongod

2、基于配置文件的命令行启动
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 &

# netstat -nltp|grep 27000
tcp 0 0 0.0.0.0:27000 0.0.0.0:* LISTEN 5356/mongod

3、以守护进程方式启动mongodb
# 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、使用系统服务的方式启动mogodb

4.1 新建配置文件

#cd /var/lib/mongodb/conf

# vi mongodb.conf
port = 27017
dbpath = /data/mongodata/rs1
logpath = /data/mongodata/rs1/rs1.log
smallfiles = true
fork = true
pidfilepath = /var/run/mongo.pid

4.2 新建脚本

#vi /etc/init.d/mongod

		#!/bin/sh  
		# chkconfig:  

		#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 
			 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 "$1" in
				start)
						start
						;;
				stop)
						stop
						;;
				status)
				status $prog
						;;
				restart)
						restart
						;;
				*)
						echo $"Usage: $0 {start|stop|status|restart}"
		esac

[root@k8s-master conf]# service mongod start
Starting mongod (via systemctl): [ OK ]

三、停止

service mongod stop