In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
那么我将从两个方面提高mongo数据库的安全防护系数
1.设置mongodb外网禁止访问
启动数据库时,额外添加--bind_ip 127.0.0.1即可
./mongod --bind_ip 127.0.0.1 --dbpath /data/db --auth
也可以通过修改/etc/mongod.conf文件添加一行代码
#只监听本地接口,多个接口用,隔开
bind_ip = 127.0.0.1
2.为数据库设置账号密码登录权限
为了保证数据库需要账号密码才能连接,那么在启动数据库的时候需要添加auth参数
./mongod --dbpath /data/db --auth
也可以通过修改/etc/mongod.conf文件添加一行代码
auth = true
这样在进行数据库连接的时候需要相应的账号密码才能成功访问。
如果之前数据库未设置账号密码的话,那么需要先添加一个管理员账户,
> use adminswitched to db admin> db.createUser({user:"root",pwd:"123456",roles:["userAdminAnyDatabase"]})Successfully added user: { "user" : "root", "roles" : [ "userAdminAnyDatabase" ] }
此时我们就成功的创建了一个管理员账户 账户名 root 密码 123456 ,此时执行show dbs会出现如下报错
> show dbs2017-12-03T22:14:58.418+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }", "code" : 13, "codeName" : "Unauthorized"} .....
上边提示意为当前admin数据库未经授权无法执行指令,因此需要以管理员身份登录验证,如下操作
> db.auth('dpd','123456')
上边执行登录操作,输出结果 1,说明登录成功。此时再执行 show dbs 则会成功输出结果。此刻我们是以管理员的身份登录数据库,如果切换到test数据库,执行db.blog.insert({name:1})会报错如下,同样意为该数据库未经授权无法操作:
> use testswitched to db test> db.blog.insert({name:1})WriteResult({ "writeError" : { "code" : 13, "errmsg" : "not authorized on test to execute command { insert: \"blog\", documents: [ { _id: ObjectId('5a240d8e2d43081ea4271cc8'), name: 1.0 } ], ordered: true }" }})
所以要为test数据库添加一个用户,并以该用户身份登录才可以执行对该数据的操作。
> db.createUser({user:'use1',pwd:'123456',roles:["readWrite"]})Successfully added user: { "user" : "use1", "roles" : [ "readWrite" ] }> db.auth('use1','123456')1> db.blog.insert({name:1})WriteResult({ "nInserted" : 1 })>
至此,完成了通过账号和密码登录权限对数据库的访问和操作。
那么最终标准的连接test数据库的URI语法如下:
mongodb://use1:123456@localhost:27017/test
总结
以上所述是小编给大家介绍的mongodb禁止外网访问及添加账号的操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.