# cd /opt
# tar xf mongodb-linux-x86_64-rhel70-4.4.4.tar
# ln -s /opt/mongodb-linux-x86_64-rhel70-4.4.4/ /opt/mongodb
- 添加环境变量vim /etc/profile
在最后添加一行export PATH=$PATH:/opt/mongodb/bin
保存退出执行source /etc/profile
- 创建mongodb配置文件
# cd /opt/mongodb
# mkdir -pv data/db
# mkdir -pv logs
# touch logs/mongodb.log
vim mongodb.conf
#端口号(默认端口27017)
port=27017
#数据目录
dbpath=/opt/mongodb/data/db
#日志目录
logpath=/opt/mongodb/logs/mongodb.log
#以守护程序的方式启用,即在后台运行
fork=true
#开启日志追加添加日志
logappend=true
#本地监听IP,0.0.0.0表示本地所有IP
bind_ip=0.0.0.0
# 最大连接数
maxConns=100
- 启动测试
mongod --config mongodb.conf
(如果启动失败考虑防火墙放行端口问题)
- 创建启动文件
vim /usr/lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
RuntimeDirectory=mongodb
ExecStart=/opt/mongodb/bin/mongod --config /opt/mongodb/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/mongodb/bin/mongod --shutdown --config /opt/mongodb/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod +x /usr/lib/systemd/system/mongodb.service
- 启动mongodb
systemctl daemon-reload
systemctl start mongodb
systemctl enable mongodb
- 测试
1)创建管理用户
mongo
> use admin
> db.createUser({user:"admin",pwd:"xuad830818",roles:[{role:"userAdminAnyDatabase",db: "admin"}]})
> db.auth('admin','xuad830818')
2)创建测试用户
> use test
> db.createUser({user:"xuad",pwd:"123456",roles:[{role:"readWrite",db:"securitydata"}]})
> db.auth('xuad','123456')
> exit