数据库生态圈(RDB & NoSQL & Bigdata)——专注于关系库优化(Oracle & Mysql & Postgresql & SQL Server )

https://www.cnblogs.com/lhdz_bj
http://blog.itpub.net/8484829
https://blog.csdn.net/tuning_optmization
https://www.zhihu.com/people/lhdz_bj

导航

Mongodb访问控制

1.限定访问的服务器特定IP
--通过启动mongodb时指定参数bind_ip
# ./mongod --bind_ip=10.10.10.2
--通过配置文件指定bind_ip
# vi /etc/mongodb.cnf
bind_ip=10.10.10.2
--通过配置文件指定多个bind_ip
# vi /etc/mongodb.cnf
bind_ip=localhost,10.10.10.2
--不对访问的服务器IP做限制
# vi /etc/mongodb.cnf
bind_ip=0.0.0.0

bind_ip_all=true
--服务器配置bind_ip后,客户端访问时必须指定该正确ip
# ./mongo 10.10.10.2

2.设置服务器监听端口
--通过启动mongodb时指定参数port
# ./mongod --bind_ip=10.10.10.2 --port=27019
--通过配置文件指定port
# vi /etc/mongodb.cnf
port=27019
--配置port后,客户端访问时必须指定该正确port
# ./mongo 10.10.10.2:27019

3.启用登录验证
--mongodb默认不会启用登录验证,即登录时不要求用户名和密码,登录后就具有root权限.
--通过启动mongodb时指定参数启用登录验证
# ./mongod --auth
--通过配置文件指定auth
# vi /etc/mongodb.cnf
auth=true
--为了启用登录验证,除了指定auth参数外,还需要为admin库添加至少一个用户.
# ./mongo
> use admin
> db.createUser({user:"root",pwd:"password",roles:["root"]});
> db.auth("root","password");
--指定auth参数,并创建用户后,指定用户root和密码才能登录和操作,否则,本机客户端虽然能连接mongodb,但不能执行任何操作.
# ./mongo -u root -p

--为db_test库创建一个读写用户usr_test
# ./mongo - root -p
> use admin
> db.createUser({user:"usr1",pwd:"password1",roles:[{role:"readWrite",db:"db_test"}]});
--通过新建的usr_test用户登录
# ./mongo -u usr1 -p password1
> use db_test
> show collections;

--删除用户
# ./mongo -u root -p
> db.dropUser("usr_test");
> db.system.users.find();

posted on 2020-12-14 18:10  lhdz_bj  阅读(226)  评论(0编辑  收藏  举报