赞
踩
docker镜像查找
docker search mongo
docker下载mongo镜像
docker pull mongo:[版本]
查看本地安装镜像
docker images
docker运行mongo镜像
docker run -itd --name mongo -p 27017:27017 mongo --auth
参数说明:
- p 27017:27017 :映射容器服务的 27017 端口到宿主机的 27017 端口。外部可以直接通过 宿主机 ip:27017 访问到 mongo 的服务。
- –auth:需要密码才能访问容器服务。
- itd
查看正在运行的容器
docker ps
查看本地的容器
docker ps -a
[root@localhost ~]# docker run -itd --name mongo -p 27017:27017 mongo --auth
8c569b057f36fb98af94f0e9e24d37d49f026532a45fafdad1bb9744ecf8c317
[root@localhost ~]# docker run -itd --name mongo2 -p 27018:27017 mongo
236756ec50f475e94f86e4e56895a75942d4685d495acf25e9eebc7031e04265
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS P
236756ec50f4 mongo "docker-entrypoint.s…" 8 seconds ago Up 7 seconds 0
8c569b057f36 mongo "docker-entrypoint.s…" 2 minutes ago Up About a minute
docker 进入mongo
docker exec -it mongo bash
.
- mongo为容器名
docker 运行mongo
docker exec -it 容器名 mongo admin
.
- 以 admin 用户身份进入mongo :
创建用户密码
db.createUser({ user:‘admin’,pwd:‘123456’,roles:[ { role:‘userAdminAnyDatabase’, db: ‘admin’},“readWriteAnyDatabase”]});
账号密码登录
db.auth(‘admin’,‘123456’)
[root@localhost ~]# docker exec -it mongo mongo admin MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("e0d5a7c6-0a89-4daa-9312-59ba0a711d8a") } MongoDB server version: 4.4.1 > db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]}); Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" }, "readWriteAnyDatabase" ] } > db.auth('admin','123456') #登录mongo 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB > # 在上面根据镜像创建mongo容器的时候,第一个末尾加了 --auth,第二个未加因此第一个容器mongo连接mongo的时候需要用户认证, #第二个连接时可直接登录,show dbs时可以看到所有数据库,而第一个容器mongo在未登录时 看不到。 [root@localhost ~]# docker exec -it mongo2 #此时未加mongo admin进不了mongo "docker exec" requires at least 2 arguments. See 'docker exec --help'. Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 236756ec50f4 mongo "docker-entrypoint.s…" 18 hours ago Up 18 hours 0.0.0.0:27018->27017/tcp mongo2 8c569b057f36 mongo "docker-entrypoint.s…" 18 hours ago Up 18 hours 0.0.0.0:27017->27017/tcp mongo [root@localhost ~]# docker exec -it mongo2 mongo admin MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("bd147da7-13d5-47a0-9757-61807eab2709") } MongoDB server version: 4.4.1 --- The server generated these startup warnings when booting: 2020-11-20T07:27:52.988+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted 2020-11-20T07:27:52.988+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' 2020-11-20T07:27:52.988+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' --- > show dbs #此时进的是mongo2,不需要登录认证也能查看本地数据库 admin 0.000GB config 0.000GB local 0.000GB > db admin > exit bye
docker cp 宿主机文件路径 容器名:容器路径
- 举例
docker cp /home/mongo/DevOps mongo:/
- /home/mongo/DevOps 是我本地文件路径,导入到mongo容器的根目录下
docker cp 容器名:容器里文件路径 宿主机文件路径
- 举例
docker cp mongo:/home/data/user.json /home/lin
- docker cp mongo:/home/data/user.json /home/lin
1)、进入mongo数据库
docker exec -it mongo mongo admin
2)、登录mongo(前面已经为admin创建过用户了,此处直接登录)
db.auth(‘admin’,‘123456’)
3)、创建数据库且为该数据库创建用户和分配权限
use mydbs #创建数据库,如果存在则使用该库
db.createUser({ user:‘test’,pwd:‘123456’,roles:[ { role:‘dbOwner’, db: ‘mydbs’}]})
4)、进入到mongo容器,将所需要的数据文件导入mongo数据库
mongorestore -d mydbs -u test -p 123456 devops
5)、接下来就是查看是否导入成功
命令代码
#登录mongo 进入admin数据库 [root@localhost mongo]# docker exec -it mongo mongo admin MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("9579fe5b-f36d-43b0-ab58-d58bf0935038") } MongoDB server version: 4.4.1 > show dbs #未登录看不到数据库 > db #查看当前数据库 admin > db.auth('admin','123456') #用户登录 1 > show dbs #此时看得到 admin 0.000GB config 0.000GB local 0.000GB mydbs 0.018GB > use mydb2 switched to db mydb2 > db mydbs2 #为mydbs数据库创建用户和权限 > db.createUser({ user:'test',pwd:'123456',roles:[ { role:'dbOwner', db: 'mydbs'} ]}) Successfully added user: { "user" : "test", "roles" : [ { "role" : "dbOwner", "db" : "mydbs" } ] } > exit bye # 退出mongo数据库 ,进入mongo容器 [root@localhost ~]# docker exec -it mongo bash root@8c569b057f36:/# ls mydata boot dev etc js-yaml.js lib64 mnt proc run srv tmp var bin data docker-entrypoint-initdb.d home lib media opt root sbin sys usr # 将mydata目录里的文件通过test,123456 用户密码导入到mydbs数据库 root@8c569b057f36:/# mongorestore -d mydbs --username test --password 123456 mydata 2020-11-23T01:37:15.464+0000 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION} 2020-11-23T01:37:15.465+0000 building a list of collections to restore from DevOps dir 2020-11-23T01:37:15.467+0000 reading metadata for mydbs2.P_TestServiceLogEntry from DevOps/P_TestServiceLogEntry.metadata.json 2020-11-23T01:37:15.468+0000 reading metadata for mydbs2.P_DataSharePublish_Entry from DevOps/P_DataSharePublish_Entry.metadata.json
大功告成,接下来可以在进入到mongo数据库或者 用可视化工具查看导入是否成功
.
踩坑记录
在导入数据文件的时候,导入的哪个数据库就要为哪个数据库创建相应的用户和权限,否则会报权限错误异常。在不同的数据库中 用户是不共同的,不能用admin数据库下的用户去导到mydbs数据库
[root@localhost ~]# docker exec -it mongo bash
root@8c569b057f36:/# mongorestore -d mydbs -u admin -p 123456 DevOps/
2020-11-23T02:17:49.885+0000 error connecting to host: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
# 进入mongo admin数据库,用admin数据库下的 admin 123456 登录,切换到mydbs数据库,创建数据库文件 [root@localhost mongo]# docker exec -it mongo mongo admin MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("7e400441-6523-4c2f-9464-9ac39462eba9") } MongoDB server version: 4.4.1 > show dbs > db admin > db.auth('admin','123456') 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB mydbs 0.000GB > use mydbs switched to db mydbs > db mydbs > db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'dbOwner', db: 'mydbs'},“readWriteAnyDatabase”]}) uncaught exception: SyntaxError: illegal character : @(shell):1:81 > db.createUser({ user:'demo',pwd:'123456',roles:[ { role:'dbOwner', db: 'mydbs'}]}) Successfully added user: { "user" : "demo", "roles" : [ { "role" : "dbOwner", "db" : "mydbs" } ] } > > > > exit [root@localhost mongo]# docker exec -it mongo mongo admin MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("869f14cb-01d1-4e38-951c-96817ce2837d") } MongoDB server version: 4.4.1 > db.auth('demo','123456') #需要使用mydbs数据库 Error: Authentication failed. 0 > exit bye [root@localhost mongo]# docker exec -it mongo mongo mydbs MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/mydbs?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("34a54753-8202-4d13-8bb8-f70396524a02") } MongoDB server version: 4.4.1 > db.auth('demo','123456') 1 > show dbs mydbs 0.000GB > db mydbs > exit bye # 进入mongo容器 [root@localhost mongo]# docker exec -it mongo bash #进入mongo容器 root@8c569b057f36:/# mongorestore -d admin --username admin --password 123456 prod_data # admin数据库里有admin这个用户及权限 改命令可以导入数据库 2020-11-21T08:34:03.129+0000 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION} 2020-11-21T08:34:03.129+0000 Failed: mongorestore target 'prod_data' invalid: stat prod_data: no such file or directory 2020-11-21T08:34:03.130+0000 0 document(s) restored successfully. 0 document(s) failed to restore. # mongo中导入数据文件 root@8c569b057f36:/# mongorestore -d mydbs --username demo --password 123456 DevOps # 为mydbs库下建立了demo账号,可以导入成功 2020-11-21T08:35:43.404+0000 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION} 2020-11-21T08:35:43.404+0000 building a list of collections to restore from DevOps dir 2020-11-21T08:35:43.407+0000 reading metadata for mydbs.P_TestServiceLogEntry from DevOps/P_TestServiceLogEntry.metadata.json 2020-11-21T08:35:43.407+0000 reading metadata for mydbs.P_DataSharePublish_Entry from DevOps/P_DataSharePublish_Entry.metadata.json 2020-11-21T08:35:43.408+0000 reading metadata for mydbs.P_CodeCommitHistory from DevOps/P_CodeCommitHistory.metadata.json 2020-11-21T08:35:43.408+0000 reading metadata for mydbs.P_WarningMessageEntry from DevOps/P_WarningMessageEntry.metadata.json 2020-11-21T08:35:43.415+0000 restoring mydbs.P_TestServiceLogEntry from DevOps/P_TestServiceLogEntry.bson 2020-11-21T08:35:43.416+0000 restoring mydbs.P_CodeCommitHistory from DevOps/P_CodeCommitHistory.bson 2020-11-21T08:35:43.427+0000 restoring mydbs.P_DataSharePublish_Entry from DevOps/P_DataSharePublish_Entry.bson 2020-11-21T08:35:43.428+0000 restoring mydbs.P_WarningMessageEntry from DevOps/P_WarningMessageEntry.bson 2020-11-21T08:35:43.574+0000 no indexes to restore [root@localhost mongo]# docker exec -it mongo mongo mydb2 MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/mydb2?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("057cc379-b6fc-4d50-82a3-517c0021ee64") } MongoDB server version: 4.4.1 > show dbs > db mydb2 #错误的创建用户 > db.createUser({ user:‘admin’,pwd:‘123456’,roles:[ { role:‘userAdminAnyDatabase’, db: ‘mydb2’},“readWriteAnyDatabase”]}); uncaught exception: SyntaxError: illegal character : @(shell):1:21 #成功的创建权限 > db.createUser({ user:'mydb2',pwd:'123456',roles:[ { role:'dbOwner', db: 'mydb2'} ]}) uncaught exception: Error: couldn't add user: command createUser requires authentication : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.createUser@src/mongo/shell/db.js:1361:11 @(shell):1:1 > show dbs > db mydb2 > db.createUser({ user:'mydb2',pwd:'123456',roles:[ { role:'dbOwner', db: 'mydb2'} ]}) uncaught exception: Error: couldn't add user: command createUser requires authentication : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.createUser@src/mongo/shell/db.js:1361:11 @(shell):1:1 > exit bye [root@localhost mongo]# docker exec -it mongo mongo admin MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("4439c7c0-4701-4ebf-90e6-eef48697e8f2") } MongoDB server version: 4.4.1 > dbs uncaught exception: ReferenceError: dbs is not defined : @(shell):1:1 > show dbs > db admin > use mydb2 switched to db mydb2 > db.createUser({ user:'mydb2',pwd:'123456',roles:[ { role:'dbOwner', db: 'mydb2'} ]}) uncaught exception: Error: couldn't add user: command createUser requires authentication : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.createUser@src/mongo/shell/db.js:1361:11 @(shell):1:1 #创建数据库 > use mydbs2 switched to db mydbs2 > show dbs DevOps 0.005GB DevOps-Log 0.001GB admin 0.018GB config 0.000GB local 0.000GB mydbs 0.018GB > db mydbs2 #为mydbs2数据库创建用户和权限 > db.createUser({ user:'test',pwd:'123456',roles:[ { role:'dbOwner', db: 'mydbs2'} ]}) Successfully added user: { "user" : "test", "roles" : [ { "role" : "dbOwner", "db" : "mydbs2" } ] } > exit bye # 退出mongo数据库 ,进入mongo容器 [root@localhost ~]# docker exec -it mongo bash root@8c569b057f36:/# ls DevOps boot dev etc js-yaml.js lib64 mnt proc run srv tmp var bin data docker-entrypoint-initdb.d home lib media opt root sbin sys usr # 将Devops目录里的文件通过test,123456 用户密码导入到mydbs2数据库 root@8c569b057f36:/# mongorestore -d mydbs2 --username test --password 123456 DevOps 2020-11-23T01:37:15.464+0000 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION} 2020-11-23T01:37:15.465+0000 building a list of collections to restore from DevOps dir 2020-11-23T01:37:15.467+0000 reading metadata for mydbs2.P_TestServiceLogEntry from DevOps/P_TestServiceLogEntry.metadata.json 2020-11-23T01:37:15.468+0000 reading metadata for mydbs2.P_DataSharePublish_Entry from DevOps/P_DataSharePublish_Entry.metadata.json >
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。