yum/apt-get安装mongodb(3.4)和开启用户认证
yum/apt-get安装mongodb(3.4)和开启用户认证
一、创建文件/etc/yum.repos.d/mongodb-org-3.4.repo:
1 2 3 4 5 6 |
[mongodb-org-3.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc |
二、sudo yum install -y mongodb-org
三、防火墙开放27017端口,然后编辑配置/etc/mongod.conf:
1 |
bindIp: 127.0.0.1 #设置绑定的ip地址或者注释该行,来允许远程访问! |
四、设置开机启动,启动,状态查看:
1 2 3 |
systemctl enable mongod systemctl start mongod systemctl status mongod |
五、使用mongo shell 创建唯一管理员用户:
1 2 3 4 |
#输入mongo,进入mongo shell use admin; db.createUser({user:"xx",pwd:"xxpwd",roles:["dbAdminAnyDatabase","readWriteAnyDatabase","root"]}); exit |
六、启用mongo 的认证机制,编辑/etc/mongod.conf:
1 2 3 4 5 |
#追加上 security: authorization: "enabled" #重启mongo服务 systemctl restart mongod |
七、此时连接mongodb需要用用户名密码登录并验证,然后再为特定的库创建该库的用户登录并登录:
1 2 3 4 5 6 |
#输入mongo,进入mongo shell use admin; db.auth('xx','xxpwd'); use spider; db.createUser({user:"spider",pwd:"spiderpwd",roles:[{role: "userAdmin", db: "spider"},{role: "readWrite", db: "spider"},{role: "dbAdmin",db: "spider"}]}); exit |
八、此时即可用spider/spiderpwd来登录管理spider库。
附:mongodb的更多配置说明:https://docs.mongodb.com/master/reference/configuration-options/#security-options,Copyright:cpp.cloudcpp.com Share、Open- C/C++程序员之家