mongodb多实例部署

Posted zc1741845455

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb多实例部署相关的知识,希望对你有一定的参考价值。

安装与管理MongoDB

1.安装解压源码包

[[email protected] ~]# tar xf mongodb-linux-x86_64-rhel70-4.0.6.tgz

[[email protected] ~]# mv mongodb-linux-x86_64-rhel70-4.0.6/ /usr/local/mongodb

 

2.指定一个进程同一时间最多可开启的文件数

[[email protected] ~]# ulimit -n

1024

[[email protected] ~]# ulimit -n 65535

[[email protected] ~]# ulimit -n

65535

 

3.用户最多可开启的程序数目

[[email protected] ~]# ulimit -u

11690

[[email protected] ~]# ulimit -u 65535

[[email protected] ~]# ulimit -u

65535

 

4.创建数据目录,日志文件及目录并创建相应的配置文件

[[email protected] ~]# mkdir -p /data/mongodb1

[[email protected] ~]# mkdir -p /data/logs/mongodb

[[email protected] ~]# touch /data/logs/mongodb/mongodb1.log

[[email protected] ~]# cd /usr/local/mongodb/

[[email protected] mongodb]# ls

bin  LICENSE-Community.txt  MPL-2  README  THIRD-PARTY-NOTICES

[[email protected] mongodb]# mkdir conf

[[email protected] mongodb]# vim conf/mongodb1.conf

port=27017

dbpath=/data/mongodb1

logpath=/data/logs/mongodb/mongodb1.log

logappend=true

fork=true

maxConns=5000

storageEngine=mmapv1

 

5.启动MongoDB数据库,-f指定配置文件

[[email protected] mongodb]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf

about to fork child process, waiting until server is ready for connections.

forked process: 2324

child process started successfully, parent exiting

[[email protected] mongodb]# netstat -anpt |grep mongod

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      2324/mongod

   

6.设置开机自动启动

[[email protected] mongodb]# vim /etc/rc.local

rm -f /data/mongodb1/mongod.lock

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf

 

7.连接数据库

[[email protected] ~]# /usr/local/mongodb/bin/mongo

> show dbs

admin   0.078GB

config  0.078GB

local   0.078GB

> exit

bye

 

  1. 开启服务与关闭服务

 

[[email protected]~]#/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf --shutdown

killing process with pid: 2806

[[email protected] ~]# netstat -anpt |grep mongod

[[email protected]~]#/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf

about to fork child process, waiting until server is ready for connections.

forked process: 2885

child process started successfully, parent exiting

[[email protected] ~]# netstat -anpt |grep mongod

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      2885/mongod   

 

  1. 开启多个实例(两个)

[[email protected] ~]# cd /usr/local/mongodb/

[[email protected] mongodb]# cd conf/

[[email protected] conf]# cp mongodb1.conf mongodb2.conf

[[email protected] conf]# vim mongodb2.conf

[[email protected] conf]# mkdir /data/mongodb2

[[email protected] conf]# touch /data/logs/mongodb/mongodb2.log

[[email protected] conf]# chmod 777 /data/logs/mongodb/mongodb2.log

 

  1. 编写启动停止脚本管理多个实例

[[email protected] ~]# cd /etc/init.d

[[email protected] init.d]# vim mongodb

#!/bin/bash

INSTANCE=$1

ACTION=$2

case "$ACTION" in

‘start‘)

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf;;

‘stop‘)

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf --shutdown;;

‘restart‘)

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf --shutdown

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf;;

esac

[[email protected] init.d]# chmod +x mongodb

[[email protected] ~]# /etc/init.d/mongodb mongodb1 start

[[email protected] ~]# /etc/init.d/mongodb mongodb2 start

[[email protected] ~]# netstat -anpt |grep mongod

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      2885/mongod         

tcp        0      0 127.0.0.1:27018         0.0.0.0:*               LISTEN      3204/mongod

 

以上是关于mongodb多实例部署的主要内容,如果未能解决你的问题,请参考以下文章

Shell脚本一键部署MongoDB

MongoDB多实例

MongoDB安装及多实例启动

MongoDB安装及多实例启动

tomcat单实例多应用多实例多应用部署

MongoDB安装以及MongoDB开启多实例