1.新建用戶
db.createUser({user: 'test', pwd: 'myPassword', roles: [{role: 'readWrite', db: 'test_db'}]})
給 test_db 庫新建一個 test 用戶,該用戶具有讀寫權限
如果沒有庫,先新建庫:
mongo // 進入mongo shell > use test // 使用 test 庫,如果沒有則新建 > db.createCollection('user') // 在 test 庫中新建一個叫 user 的 collection
2.修改mongoDB配置
sudo vim /etc/mongod.conf
注釋下面的 bindIp 那一行:
# network interfaces net: port: 27017 # bindIp: 127.0.0.1 <- comment out this line
取消注釋 security, 下面添加一行如下:
security: authorization: 'enabled'
重啟 mongod
sudo service mongod restart
3. 防火牆放行 27017 端口
sudo iptables -A INPUT -p tcp --dport 27017 -j ACCEPT
4. 最后用遠程client連接即可,如 Robomongo
參考: