李旸 2020-03-04
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.list apt-get update apt-get install mongodb-org apt-get install mongodb-clients
systemctl start mongod.service
# 登录 mongo # 切换到admin数据库 use admin # 创建admin账号,密码::admin111 权限:root权限,授权的数据库名:admin db.createUser({user:"admin", pwd:"admin111", roles:[{role:"root", db:"admin"}]})
编辑MongoDB配置文件
vim /etc/mongod.conf net: 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