安裝并啟動(dòng):
docker pull mongo (拉取鏡像 默認(rèn)最新版本)
docker images (查看鏡像)
docker run -p 27017:27017 -td mongo (啟動(dòng)鏡像)
docker ps (查看啟動(dòng)的鏡像)
docker exec -it 617f4f3a2571 /bin/bash (進(jìn)入容器)? ?//?617f4f3a2571:這個(gè)是鏡像ID
mongo (進(jìn)入mongodb)
創(chuàng)建管理賬戶(hù):
use admin
db.createUser(
{
user: "admin",
pwd: "123456",
roles: [ { role: "root", db: "admin" } ]
}
);
退出容器:
exit;
創(chuàng)建test用戶(hù):
mongo --port?27017 -u admin -p 123456 --authenticationDatabase admin (以剛建立的用戶(hù)登錄數(shù)據(jù)庫(kù) 創(chuàng)建test用戶(hù))
use test
db.createUser(
{
user: "tester",
pwd: "123456",
roles: [
{ role: "readWrite", db: "test" }
]
}
);
退出容器:
exit;
用上面創(chuàng)建的test用戶(hù)登錄:
mongo -u tester -p --authenticationDatabase test
mongodb其他操作命令:
show dbs (顯示數(shù)據(jù)庫(kù))
use? dbname (切換到數(shù)據(jù)庫(kù))
show collections (顯示表)
db.find.表名 (查看表數(shù)據(jù))