1.建立的用戶管理員python
use admin db.createUser( { user: "myUserAdmin", pwd: "abc123", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
2.從新啓動服務器git
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.建立用戶,用戶都跟着數據庫走ubuntu
use test db.createUser( { user: "xxx", pwd: "xxx", roles: [ { role: "readWrite", db: "test" } ] } )
4.查看剛剛建立的用戶 /查看整個mongoDB所有的用戶服務器
show users
use admin db.system.users.find()
5. 客戶端登陸ui
> use test
switched to db test
> db.auth('test','xxxxxx')
1
> show collections
blog
books
goods
map_reduce_example
order
restaurants
system.indexes
> spa
6.python 代碼登陸rest
client = MongoClient(host='127.0.0.1', port=27017)
db = client.test
db.authenticate(name='test', password='123xx5668')code