顯示具有 mongo 標籤的文章。 顯示所有文章
顯示具有 mongo 標籤的文章。 顯示所有文章

2015年2月27日 星期五

[mongo]如何開啟使用者認證功能使用者認證功能(Authentication)

webber0928
第一步,建立一個最高權限帳號,請用在 MongoDB Shell 執行下面這二行的指令
use admin
db.addUser("root","12345678")
第二步,請把 MongoDB 給 Shutdown 後,再用下面的指令來啟動 MongoDB,如果沒有加上 "—auth” 這個參數
※這是啟動驗證功能之意,如果沒有此參數就啟動Mongod,那即使設定了帳戶,也沒有效果
$ sudo stop mongodb
$ mongod --auth
$ sudo start mongodb
MongoDB 啟動完成之後,再用 Robomongo 做查詢和修改。
P.S.沒有裝的朋友請用$ sudo apt-get install robomongo
Robomongo Create 設定:
Connection>
Address: locahost : 27017

Authentication>
Perform authentication 打勾
User Name: root
Password: 12345678

綁定ip讓外部呼叫

$ vim /etc/mongodb.conf
將 bind_ip = 127.0.0.1 改成 bind_ip = 0.0.0.0
最後記得重啟
$ sudo restart mongodb
nodejs如果要連mongo的url寫法:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

出處

[mongo]如何開啟使用者認證功能使用者認證功能(Authentication)

webber0928
第一步,建立一個最高權限帳號,請用在 MongoDB Shell 執行下面這二行的指令
use admin
db.addUser("root","12345678")

[mongo]couldn’t connect to server 12...錯誤訊息解決方法

webber0928

Error: couldn’t connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js: exception: connect failed,mongo,error

這個問題可以通過下面4個步驟來解決

1) 刪除 .lock 檔案

$ sudo rm /var/lib/mongodb/mongod.lock 

2) 修復 mongodb

$ mongod –repair

3) 啟動 mongodb

sudo start mongodb

4) 檢查 mongodb 狀態

sudo service mongodb status 
(or)
sudo status mongodb

5) 啟動 mongo client

$ mongo

MongoDB 指令的使用

webber0928

顯示出目前有哪些資料庫
show dbs
新增一個資料庫friends
use friends
刪除現在正在使用的資料庫
db.dropDatabase()
顯示目前使用資料庫的名字
db.getName()
新增一個名字叫best的collection到friend這個資料庫,並寫入資料{name:”simon”,age:”25”}
db.best.insert({name:”simon”,age:”25”})
刪除best這個collection
db.best.drop()
顯示friend這個資料庫內的所有collections
show collections
db.getCollectionNames()
讀取best這個collection內的所有資料
db.best.find()
制定要搜尋best這個collection的範圍或是條件,以age來舉例,搜尋age值為25:
db.best.find({age:25})
在best這個collection內的name:simon後加入一個新的欄位,內容為phone:123
db.best.update({name:'simon'},{'$set':{phone:123}})
在best這個collection內刪除一個內容為phone:123的欄位
db.best.update({name:'simon'},{'$unset':{phone:123}})
將best這個collection內{name:’simon’}修改成{name:’yes’}
db.best.update({name:'simon'},{name:'yes'})
清空best這個collection 中的所有資料
db.best.remove()

MongoDB 指令的使用

webber0928
顯示出目前有哪些資料庫
show dbs
新增一個資料庫friends
use friends

Coprights @ 2016, Blogger Templates Designed By Templateism | Distributed By Gooyaabi Templates