当前时间:2019年3月1日
安装方式:源码安装
参考资料
- 在CentOS7上源码安装MongoDB3.2.7:
https://www.jianshu.com/p/132c9b2766e0 - 查看MongoDB官网最新版本:
https://www.mongodb.com/download-center/community - MongoDB下载地址
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.6.tgz
环境准备
当前目录是所有软件安装包的存放目录
[root@sonny ~]# mkdir /root/mongodb #创建MongoDB程序存放目录
[root@sonny ~]# mkdir /data/mongodata -p #创建数据存放目录
[root@sonny ~]# mkdir /data/log/mongolog -p #创建日志存放目录
[root@sonny ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.6.tgz
- 下载地址,先去官网查看最新版本号,然后把下载地址的版本号改为最新版本就可
安装
[root@sonny ~]# tar xf mongodb-linux-x86_64-3.2.7.tgz
[root@sonny ~]# cd mongodb-linux-x86_64-3.2.7/
[root@sonny ~]# cp -r * /root/mongodb # 将当前所有文件复制
- 为了便于命令启动,需要编辑全局变量PATH
[root@sonny ~]# vim /etc/profile.d/mongodb.sh
export PATH=$PATH:/root/mongodb/bin
[root@sonny ~]# source /etc/profile
- ==环境变量的路径前面的系统变量一定要不能写错,否则就会出现,环境变量就只有一个,其他的都消失了,这是因为这样做是没有追加到系统变量下,而是替换了所有系统变量==
创建配置文件
查看mongod的帮助信息
[root@sonny ~]# mongod --help
Options:
General options:
-h [ --help ] show this usage information
--version show version information
......
在mongodb/bin目录下创建配置文件mongodb.conf
[root@sonny ~]# cd mongodb/bin
[root@sonny bin]# vi mongodb.conf
#数据文件存放目录
dbpath = /data/mongodata
#日志文件存放目录
logpath = /data/log/mongolog/mongodb.log
#端口
port = 27017
#以守护程序的方式启用,即在后台运行
fork = true
nohttpinterface = true
启动服务
[root@sonny bin]# mongod --dbpath=/data/mongodata --logpath=/data/log/mongolog/mongodb.log --logappend --fork
[root@sonny bin]# mongod -f /root/mongodb/bin/mongodb.conf #通过配置文件启动
about to fork child process, waiting until server is ready for connections.
forked process: 7779
child process started successfully, parent exiting
测试
[root@sonny bin]# mongo
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:
2019-03-01T11:30:22.390+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-03-01T11:30:22.390+0800 I CONTROL [initandlisten]
2019-03-01T11:30:22.390+0800 I CONTROL [initandlisten]
2019-03-01T11:30:22.390+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-03-01T11:30:22.390+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-03-01T11:30:22.390+0800 I CONTROL [initandlisten]
2019-03-01T11:30:22.390+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-03-01T11:30:22.390+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-03-01T11:30:22.390+0800 I CONTROL [initandlisten]
> show dbs
local 0.000GB
> quit()
消除警告
[root@sonny bin]# vim /etc/rc.local
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
ulimit -u 65535
[root@sonny bin]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@sonny bin]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
- Tip:
如果发现加入到/etc/rc.local中的内容在重启后没有生效,检查下rc.local的权限及当前系统运行等级
[root@sonny ~]# runlevel
[root@sonny ~]# chmod +x /etc/rc.local
文件限制数调整
修改配置文件 /etc/security/limits.conf,添加配置信息:
* soft nofile 65535
* hard nofile 65535
* soft nproc 32000
* hard nproc 32000
停止mongodb
正常停止方法: kill -2 PID
[root@sonny ~]# ps -ef | grep mongo
root 7779 1 0 11:30 ? 00:00:06 mongod -f /root/mongodb/bin/mongodb.conf
root 7989 7496 0 11:41 pts/0 00:00:00 grep --color=auto mongo
[root@sonny ~]# kill -2 7779
在mongodb内部停止:
>use admin
>db.shutdownServer();
再次验证
[root@sonny ~]# mongod -f /root/mongodb/bin/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 8011
child process started successfully, parent exiting
[root@sonny ~]# mongo
MongoDB shell version: 3.2.7
connecting to: test
Server has startup warnings:
2019-03-01T11:42:54.327+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-03-01T11:42:54.327+0800 I CONTROL [initandlisten]
> exit
bye
清理环境
进入安装包存放目录
[root@sonny ~]# rm -rf redis-5.0.3 #删除解压后文件,留下安装包,以备不时之需
服务化
[root@snails data]# cat > /usr/lib/systemd/system/systemd-mongodb.service << EOF
[Unit]
Description=mongodb
After=network.target
[Service]
Type=forking
PIDFile=/data/mongodata/mongod.lock
ExecStart=/root/mongodb/bin/mongod -f /root/mongodb/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
[root@snails data]# systemctl enable systemd-mongodb