美文网首页
CentOS7安装MongoDB4

CentOS7安装MongoDB4

作者: 朱穆朗玛 | 来源:发表于2021-04-22 18:56 被阅读0次

1、下载源码包

企业版
https://www.mongodb.com/try/download/enterprise
社区版
https://www.mongodb.com/download-center/community
下载版本
mongodb-linux-x86_64-enterprise-rhel70-4.4.5.tgz

2、解压

[root@vanhrcloud home]# tar -zxvf mongodb-linux-x86_64-enterprise-rhel70-4.4.5.tgz

3、将解压后的文件夹移动到 /usr/local/ 的 mongodb 目录下

[root@vanhrcloud home]# mv mongodb-linux-x86_64-enterprise-rhel70-4.4.5 /usr/local/mongodb

4、配置系统文件 profile

[root@vanhrcloud home]# vi /etc/profile

文件末尾加上
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin

5、配置文件生效

[root@vanhrcloud home]# source /etc/profile

6、创建用于存放数据和日志文件的文件夹

[root@vanhrcloud home]# cd /usr/local/mongodb
[root@vanhrcloud mongodb]# chmod -R 777 /usr/local/mongodb
[root@vanhrcloud mongodb]# mkdir -p data
[root@vanhrcloud mongodb]# chmod -R 777 data
[root@vanhrcloud mongodb]# mkdir logs
[root@vanhrcloud mongodb]# chmod -R 777 logs
[root@vanhrcloud mongodb]# cd logs
[root@vanhrcloud logs]# touch mongodb.log

7、mongodb 启动配置

// 进入到 bin 目录,增加一个配置文件
[root@vanhrcloud logs]# cd /usr/local/mongodb/bin
[root@vanhrcloud bin]# vi mongodb.conf

// 插入下列内容
# 数据文件存放目录
dbpath = /usr/local/mongodb/data
# 日志文件存放目录
logpath = /usr/local/mongodb/logs/mongodb.log
# 端口
port = 27017
# 以守护程序的方式启用,即在后台运行
fork = true
# 关闭http接口,默认关闭27018端口访问
# nohttpinterface = true
# 如果只是本机访问数据库,请注释以下配置,如果开启以下配置,务必设置账号密码登录
bind_ip = 0.0.0.0

8、启动 mongodb 数据库服务,以配置文件的方式启动

[root@vanhrcloud bin]# cd /usr/local/mongodb/bin
[root@vanhrcloud bin]# ./mongod -f mongodb.conf

9、连接 mongodb 数据库

[root@vanhrcloud bin]# ./mongo

10、设置 mongodb.service 启动服务,设置开机启动

[root@vanhrcloud bin]# cd /lib/systemd/system
[root@vanhrcloud system]# vi mongodb.service

// 编辑其内容为
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target

[Service]
# (open files)
LimitNOFILE=64000

Type=forking
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/bin/mongodb.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

11、设置 mongodb.service 权限

[root@vanhrcloud system]# chmod 754 mongodb.service

12、mongodb.service 操作命令

#启动服务
systemctl start mongodb.service
#关闭服务
systemctl stop mongodb.service
#开机启动
systemctl enable mongodb.service

问题记录

1、启动报错

Error parsing INI config file: unrecognised option 'nohttpinterface'
try './mongod --help' for more information

解决方案

将配置文件中该句删除,成功启动
nohttpinterface = true
[root@iZdpcpw9gnl8rdZ bin]# ./mongod -f mongodb.conf
./mongod: error while loading shared libraries: libnetsnmpmibs.so.31: cannot open shared object file: No such file or directory
[root@iZdpcpw9gnl8rdZ bin]# yum install net-snmp

2、性能优化

The server generated these startup warnings when booting: 
        2021-04-22T17:41:57.347+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-04-22T17:41:58.210+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-04-22T17:41:58.211+08:00: You are running this process as the root user, which is not recommended
        2021-04-22T17:41:58.211+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2021-04-22T17:41:58.211+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
        2021-04-22T17:41:58.211+08:00: Soft rlimits too low
        2021-04-22T17:41:58.211+08:00:         currentValue: 1024
        2021-04-22T17:41:58.211+08:00:         recommendedMinimum: 64000

解决方案

// Soft rlimits too low警告处理方案,在mongodb.service中设置
# (open files)
LimitNOFILE=64000

// hugepage警告处理方案
[root@vanhrcloud bin]# vim /etc/rc.d/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

[root@vanhrcloud bin]# chmod +x /etc/rc.d/rc.local
[root@vanhrcloud bin]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
[root@vanhrcloud bin]# cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]

配置帐号密码

1.开启认证

[root@vanhrcloud bin]# ./mongod --auth

2.创建管理员用户

> use admin
switched to db admin
> db.createUser({user:"admin",pwd:"Vanhr1306",roles:["root"]})
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }

3.认证登录

> db.auth("admin", "123456")

相关文章

网友评论

      本文标题:CentOS7安装MongoDB4

      本文链接:https://www.haomeiwen.com/subject/eanxrltx.html