1.文件准备
mkdir -p /data/mongodb
cd /data/mongodb
--下载一个二进制目录。
mongodb-linux-x86_64-rhel70-5.0.9.tgz
dockerfile-mongodb.yml
mongodb.conf
2.创建镜像
chmod u+x docker-build-mongodb.sh
./docker-build-mongodb.sh
........................
Removing intermediate container 81853da7408d
---> 8b0373bbbea0
Successfully built 8b0373bbbea0
Successfully tagged mongodb:5.0.9
--创建卷。
docker volume create mongodata
ls /data/docker/data/volumes/mongodata/_data/
3.创建容器
--后台窗口一个容器。
docker run -it -d --name mongodb27017 -h mongodb27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-p 27017:27017 mongodb:5.0.9
--上面这些参数是docker里面针对mongodb定义的参数。
4.登陆并使用容器
docker exec -it mongodb27017 bash
--使用本地IP创建后才可以远程登陆。
mongo --host 127.0.0.1:27017
[root@mongodb27017 local]]# mongo --host 127.0.0.1:27017
MongoDB shell version v5.0.9
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9a8035df-e8d9-4ab5-950c-892201f541ea") }
MongoDB server version: 5.0.9
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
>
--创建管理员。
use admin
db.createUser(
{
user: "admin",
pwd: "admin123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
--创建集群管理员
use admin
db.auth("admin","admin123")
> use admin
switched to db admin
> db.auth("admin","admin123")
1
> db.createUser(
... {
... user: "root",
... pwd: "root123",
... roles: [ { role: "clusterAdmin", db: "admin" } ]
... }
... )
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "clusterAdmin",
"db" : "admin"
}
]
}
--回显
> use sspudb
switched to db sspudb
> db.createUser(
... {
... user: "sspu",
... pwd: "sspu123",
... roles: [{ role: "readWrite", db: "sspudb" }]
... }
... )
Successfully added user: {
"user" : "sspu",
"roles" : [
{
"role" : "readWrite",
"db" : "sspudb"
}
]
}
--宿主机登陆使用
宿主机:PATH 路径添加:/data/mongodb/mongodb-linux-x86_64-rhel70-5.0.9/bin
mongo --host 192.168.1.7:27017
[root@es1 bin]# mongo --host 192.168.1.7:27017
MongoDB shell version v5.0.9
connecting to: mongodb://192.168.1.7:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a2169aa3-27c0-464c-a100-5df259ea013d") }
MongoDB server version: 5.0.9
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
>
> db.serverStatus().connections
{
"current" : 1,
"available" : 499,
"totalCreated" : 2,
"active" : 1,
"threaded" : 1,
"exhaustIsMaster" : 0,
"exhaustHello" : 0,
"awaitingTopologyChanges" : 0
}
--向SSPUDB数据库插入数据
use sspudb
db.auth("sspu","sspu123")
db.ssputab1.insert({"sspu01":"student1"})
for(var i =0; i <11; i ++){db.ssputab1.insert({userName:'sspu'+i,age:i})}
show collections
--回显。
> use sspudb
switched to db sspudb
> db.auth("sspu","sspu123")
1
> db.ssputab1.insert({"sspu01":"student1"})
WriteResult({ "nInserted" : 1 })
> for(var i =0; i <11; i ++){db.ssputab1.insert({userName:'sspu'+i,age:i})}
WriteResult({ "nInserted" : 1 })
> show collections;
ssputab1
db.ssputab1.find()
> db.ssputab1.find()
{ "_id" : ObjectId("6688a7c162946c3d05351f88"), "sspu01" : "student1" }
{ "_id" : ObjectId("6688a7cc62946c3d05351f89"), "userName" : "sspu0", "age" : 0 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8a"), "userName" : "sspu1", "age" : 1 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8b"), "userName" : "sspu2", "age" : 2 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8c"), "userName" : "sspu3", "age" : 3 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8d"), "userName" : "sspu4", "age" : 4 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8e"), "userName" : "sspu5", "age" : 5 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8f"), "userName" : "sspu6", "age" : 6 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f90"), "userName" : "sspu7", "age" : 7 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f91"), "userName" : "sspu8", "age" : 8 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f92"), "userName" : "sspu9", "age" : 9 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f93"), "userName" : "sspu10", "age" : 10 }
db.ssputab1.insert({"userName":"sspu11"})
> db.ssputab1.insert({"userName":"sspu11"})
WriteResult({ "nInserted" : 1 })
> db.ssputab1.find()
{ "_id" : ObjectId("6688a7c162946c3d05351f88"), "sspu01" : "student1" }
{ "_id" : ObjectId("6688a7cc62946c3d05351f89"), "userName" : "sspu0", "age" : 0 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8a"), "userName" : "sspu1", "age" : 1 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8b"), "userName" : "sspu2", "age" : 2 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8c"), "userName" : "sspu3", "age" : 3 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8d"), "userName" : "sspu4", "age" : 4 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8e"), "userName" : "sspu5", "age" : 5 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f8f"), "userName" : "sspu6", "age" : 6 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f90"), "userName" : "sspu7", "age" : 7 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f91"), "userName" : "sspu8", "age" : 8 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f92"), "userName" : "sspu9", "age" : 9 }
{ "_id" : ObjectId("6688a7cc62946c3d05351f93"), "userName" : "sspu10", "age" : 10 }
{ "_id" : ObjectId("6688a81e62946c3d05351f94"), "userName" : "sspu11" }
5.将容器上传到harbor镜像
docker rm -f mongodb27017
docker tag mongodb:5.0.9 192.168.1.11:443/myharbor/mongodb:5.0.9
docker push 192.168.1.11:443/myharbor/mongodb:5.0.9
[root@es1 bin]# docker rm -f mongodb27017
mongodb27017
[root@es1 bin]# docker tag mongodb:5.0.9 192.168.1.11:443/myharbor/mongodb:5.0.9
[root@es1 bin]# docker push 192.168.1.11:443/myharbor/mongodb:5.0.9
The push refers to repository [192.168.1.11:443/myharbor/mongodb]
38051ae93ab2: Pushed
4722af617cd4: Pushed
f1e5014e52bd: Pushed
34fc55c434bf: Pushed
604ddaddc589: Pushed
53c61a520c85: Mounted from myharbor/mysql
88765da43086: Mounted from myharbor/mysql
2ca40431ab7d: Mounted from myharbor/mysql
caceb42f09c2: Mounted from myharbor/mysql
5b1d42135d46: Mounted from myharbor/mysql
174f56854903: Mounted from myharbor/mysql
5.0.9: digest: sha256:c65dff2739c645447590cc204bb6cc9a667f0a44e6ff55a5c6146baf6aced54d size: 2617