系统:
Ubuntu 18.04
MongoDB: 4.2,3
# 添加MongoDB GPG密钥apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 4B7C549A058F8B6B# 创建MongoDB存储库echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.listapt-get updateapt-get install mongodb-orgapt-get install mongodb-clients
systemctl start mongod.service
# 登录mongo# 切换到admin数据库use admin# 创建admin账号,密码::admin111 权限:root权限,授权的数据库名:admindb.createUser({user:"admin", pwd:"admin111", roles:[{role:"root", db:"admin"}]})
编辑MongoDB配置文件
vim /etc/mongod.confnet: port: 27017 # 端口 bindIp: 0.0.0.0 # 允许访问的地址,0.0.0.0表示所有security: authorization: enabled
重启MongoDB服务
systemctl restart mongod.service
测试登录
mongo -u admin -p admin111 --authenticationDatabase admin