mongodb 配置单实例与双实例
Posted 刘振川的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb 配置单实例与双实例相关的知识,希望对你有一定的参考价值。
环境: centos6.5 192.168.16.70
配置单实例mongodb:
[[email protected] soft]# tar xf mongodb-linux-x86_64-rhel62-3.2.7.tgz
[[email protected] soft]# ls
mongodb-linux-x86_64-rhel62-3.2.7
mongodb-linux-x86_64-rhel62-3.2.7.tgz
[[email protected] soft]# mv mongodb-linux-x86_64-rhel62-3.2.7 /usr/local/mongodb
[[email protected] soft]# cd /usr/local/mongodb/
[[email protected] mongodb]# ls
bin GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES
[[email protected] mongodb]# ulimit -n
1024
[[email protected] mongodb]# ulimit -n 25000 //指定同一时间最多可开启的文件数
[[email protected] mongodb]# ulimit -u
3648
[[email protected] mongodb]# ulimit -u 25000 //用户最多可开启的程序数目
[[email protected] mongodb]# ulimit -n
25000
[[email protected] mongodb]# ulimit -u
25000
[[email protected] mongodb]# mkdir -p /data/mongodb1
[[email protected] mongodb]# mkdir -p /data/logs/mongodb
[[email protected] mongodb]# touch /data/logs/mongodb/mongodb1.log
[[email protected] mongodb]# cd /usr/local/mongodb/
[[email protected] mongodb]# ls
bin GNU-AGPL-3.0 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
[[email protected] mongodb]# pwd
/usr/local/mongodb
[[email protected] mongodb]# ls
bin conf GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES
[[email protected] mongodb]# bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf //启动mongodb数据库 -f 指定配置文件
about to fork child process, waiting until server is ready for connections.
forked process: 1652
child process started successfully, parent exiting
[[email protected] mongodb]# ps -ef | grep mongod
root 1652 1 2 04:53 ? 00:00:00 bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
root 1666 1607 0 04:53 pts/0 00:00:00 grep mongod
[[email protected] mongodb]# netstat -lnpt | grep 27017
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1652/bin/mongod
[[email protected] mongodb]# lsof -i:27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 1652 root 6u IPv4 12709 0t0 TCP *:27017 (LISTEN)
[[email protected] conf]# vim /etc/rc.local //设置开机自启动
rm -rf /data/mongodb1/mongod.lock
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
[[email protected] mongodb]# bin/mongo //登陆mongodb数据库
MongoDB shell version: 3.2.7
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2017-03-28T04:53:32.052+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-03-28T04:53:32.052+0800 I CONTROL [initandlisten]
> show dbs
local 0.078GB
> exit
bye
去除报错问题:
[[email protected] mongodb]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[[email protected] mongodb]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
[[email protected] ~]# vim .bash_profile
。。。。。。。。。。。。。。。。
alias mongo=/usr/local/mongodb/bin/mongo
。。。。。。。。。。。。。。。。。。。
[[email protected] ~]# source .bash_profile
关闭服务的三种方法:
方法一:
[[email protected] mongodb]# bin/mongo
MongoDB shell version: 3.2.7
connecting to: test
Server has startup warnings:
2017-03-28T05:16:53.926+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-03-28T05:16:53.926+0800 I CONTROL [initandlisten]
> use admin
switched to db admin
> db.shutdownServer();
server should be down...
2017-03-28T05:38:06.756+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2017-03-28T05:38:06.758+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2017-03-28T05:38:06.758+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed
> exit
bye
方法二:
[[email protected] mongodb]# bin/mongod -f /usr/local/mongodb/conf/mongodb2.conf --shutdown
killing process with pid: 1829
方法三:杀进程号(不建议用杀进程方法,mongodb集群的话节点数据不易保存)
[[email protected] mongodb]# bin/mongod -f /usr/local/mongodb/conf/mongodb2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1882
child process started successfully, parent exiting
[[email protected] mongodb]# ps -ef | grep mongod
root 1882 1 2 05:41 ? 00:00:00 bin/mongod -f /usr/local/mongodb/conf/mongodb2.conf
root 1896 1717 0 05:41 pts/1 00:00:00 grep mongod
[[email protected] mongodb]# kill 1882
[[email protected] mongodb]# ps -ef | grep mongod
root 1899 1717 0 05:41 pts/1 00:00:00 grep mongod
开启两个实例,配置双实例mongodb:
[[email protected] conf]# pwd
/usr/local/mongodb/conf
[[email protected] conf]# vim mongodb2.conf
port=27018
dbpath=/data/mongodb2
logpath=/data/logs/mongodb/mongodb2.log
logappend=true //日志追加
fork=true
maxConns=5000
storageEngine=mmapv1 //存储引擎
[[email protected] conf]# ls /data/ //建立mongodb2数据文件目录
logs mongodb1 mongodb2
[[email protected] conf]# ls /data/logs/mongodb/ //建立mongodb2.log 日志文件
mongodb1.log mongodb2.log
[[email protected] mongodb2]# chmod 777 /data/logs/mongodb/mongodb2.log //给mongodb2.log添加权限
编写启停脚本:
[[email protected] mongodb2]# 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
;;
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] init.d]# /etc/init.d/mongodb mongodb1 start
about to fork child process, waiting until server is ready for connections.
forked process: 1957
child process started successfully, parent exiting
[[email protected] init.d]# /etc/init.d/mongodb mongodb2 start
about to fork child process, waiting until server is ready for connections.
forked process: 1973
child process started successfully, parent exiting
[[email protected] init.d]# ps -ef | grep mongodb
root 1957 1 1 05:56 ? 00:00:00 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
root 1973 1 1 05:56 ? 00:00:00 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb2.conf
root 1987 1902 0 05:56 pts/2 00:00:00 grep mongodb
以上是关于mongodb 配置单实例与双实例的主要内容,如果未能解决你的问题,请参考以下文章