```
# 下载解压
cd /usr/local
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.2.tgz
tar -zxvf mongodb-linux-x86_64-3.6.2.tgz
mv mongodb-linux-x86_64-3.6.2 mongodb
# 配置文件配置
cd mongodb
mkdir data
mkdir data/db
mkdir data/logs
mkdir conf
cd conf
vim mongodb.conf
dbpath=/usr/local/mongodb/data/db
logpath=/usr/local/mongodb/data/logs/mongodb.log#这个要指定文件
fork=true
logappend=true#日志追加
port=27017
auth=true
bind_ip=0.0.0.0#远程连接要指定ip 不然无法连接。0.0.0.0不限制ip访问,并开启对应端口
# 开机启动(centos7 开机启动配置)
vim /lib/systemd/system/mongodb.service
内容如下
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /usr/local/mongodb/conf/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# systemctl 操作参数
# 启动
systemctl start mongodb.service
# 查看状态
systemctl status mongodb.service
# 开机启动
systemctl enable mongodb.service
# 如果mongodb.service配置错误 在更改后记得重新加载文件
systemctl daemon-reload
# mongodb系统进程查看并操作
ps aux |grep mongodb --查看mongodb进程
kill -15 PID --关闭mongodb服务
netstat -lanp | grep 27017 --检查端口是否已被启动
# 开启用户授权校验以及新建测试数据库
mongodb.conf文件中新增auth=true(开启授权验证)
cd /usr/local/mongodb/bin
./mongo //打开mongo客户端
use admin //新建超级用户管理员库
db.createUser(
{user:'admin',
pwd:'123456!&',
customData:{description:"超级管理员admin"},
roles:[
{
role:'userAdminAnyDatabase',
db:'admin'
}]
})
use admin
db.auth('admin','123456!&') //数据库登录授权
db.createUser({
user:'root',
pwd:'123456!&',
customData:{description:"管理员root"},
roles:[{
'role':'root',
'db':'admin'
}]
})
use test //创建test测试数据库
db.createUser({ //创建测试库具有读写能力的普通用户授权登录用户名密码
user:'jzwx',
pwd:'123456!&',
customData:{description:"test普通用户"},
roles:[{
'role':'readWrite',
'db':'test'
}]
})
```
参考博客:
https://segmentfault.com/a/1190000012858527
https://blog.csdn.net/hu_wen/article/details/76690508
https://blog.csdn.net/cengjingcanghai123/article/details/51648110
https://blog.csdn.net/xw505501936/article/details/72877066