參考下列指令:
# 環境準備
docker rm -f mongodb_mongo_1
rm -r -f $PWD/db
mkdir -p $PWD/db
# 建立mongoDB
docker run \
-d \
--name mongodb_mongo_1 \
-p 27017:27017 \
-v $PWD/db:/data/db \
mongo
sleep 5
# 建立超級管理者
docker exec -it mongodb_mongo_1 bash
mongo admin
db.createUser({ user: "admin" , pwd: "admin1234", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]})
quit()
# 重新登入
mongo admin
db.auth("admin","admin1234")
use octblog
db.createUser({
user: "gevin",
pwd: "gevin1234",
roles: [ { role: "readWrite", db: "octblog" },
{ role: "readWrite", db: "octblog-log" } ]
})
quit()
mongo octblog
db.auth("gevin","gevin1234")
use octblog
db.collection.insert({'test':1234});
quit()
exit
# 重啟mongoDB
docker rm -f mongodb_mongo_1
docker run \
-d \
--name mongodb_mongo_1 \
-p 27017:27017 \
-v $PWD/db:/data/db \
mongo mongod --auth