python mongdb 安全登录
博客专区 > hyhlinux 的博客 > 博客详情
python mongdb 安全登录
hyhlinux 发表于11个月前
python mongdb 安全登录
  • 发表于 11个月前
  • 阅读 8
  • 收藏 0
  • 点赞 0
  • 评论 0
摘要: 1.创建的用户管理员

1.创建的用户管理员

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

2.重新启动服务器

ubuntu@VM-103-136-ubuntu:~$ mongod --auth  
2016-12-11T18:56:25.729+0800 I JOURNAL  [initandlisten] journal dir=/data/db/journal
2016-12-11T18:56:25.729+0800 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2016-12-11T18:56:25.778+0800 I JOURNAL  [durability] Durability thread started
2016-12-11T18:56:25.778+0800 I JOURNAL  [journal writer] Journal writer thread started
2016-12-11T18:56:25.799+0800 I CONTROL  [initandlisten] MongoDB starting : pid=31940 port=27017 dbpath=/data/db 64-bit host=VM-103-136-ubuntu
2016-12-11T18:56:25.799+0800 I CONTROL  [initandlisten] 
2016-12-11T18:56:25.799+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-12-11T18:56:25.799+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-12-11T18:56:25.799+0800 I CONTROL  [initandlisten] 
2016-12-11T18:56:25.799+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-12-11T18:56:25.799+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-12-11T18:56:25.799+0800 I CONTROL  [initandlisten] 
2016-12-11T18:56:25.800+0800 I CONTROL  [initandlisten] db version v3.0.6
2016-12-11T18:56:25.800+0800 I CONTROL  [initandlisten] git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2
2016-12-11T18:56:25.800+0800 I CONTROL  [initandlisten] build info: Linux build6.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2016-12-11T18:56:25.800+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2016-12-11T18:56:25.800+0800 I CONTROL  [initandlisten] options: { security: { authorization: "enabled" } }
2016-12-11T18:56:25.819+0800 I NETWORK  [initandlisten] waiting for connections on port 27017
2016-12-11T18:56:30.077+0800 I NETWORK  [initandlisten] connection accepted from 123.207.174.233:51496 #1 (1 connection now open)

 

3.创建用户,用户都跟着数据库走

use test
db.createUser(  
 {  
    user: "xxx",  
    pwd: "xxx",  
       roles: [  
          { role: "readWrite", db: "test" }
   ]
 }
)

 

4.查看刚刚创建的用户 /查看整个mongoDB全部的用户

show users
use admin  
db.system.users.find() 

 

5. 客户端登录

> use test
switched to db test
> db.auth('test','xxxxxx')
1
> show collections
blog
books
goods
map_reduce_example
order
restaurants
system.indexes

 

6.python 代码登录

client = MongoClient(host='127.0.0.1', port=27017)
db = client.test
db.authenticate(name='test', password='123xx5668')

标签: python mongodb
共有 人打赏支持
粉丝 8
博文 130
码字总数 50509
×
hyhlinux
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: