1、拉取
- [root@Tseng-HW ~]# docker pull mongo:5.0
- 5.0: Pulling from library/mongo
- 7b1a6ab2e44d: Pull complete
- 90eb44ebc60b: Pull complete
- 5085b59f2efb: Pull complete
- c7499923d022: Pull complete
- 019496b6c44a: Pull complete
- c0df4f407f69: Pull complete
- 351daa315b6c: Pull complete
- a233e6240acc: Pull complete
- a3f57d6be64f: Pull complete
- dd1b5b345323: Pull complete
- Digest: sha256:5be752bc5f2ac4182252d0f15d74df080923aba39700905cb26d9f70f39e9702
- Status: Downloaded newer image for mongo:5.0
- docker.io/library/mongo:5.0
- [root@Tseng-HW ~]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- mongo 5.0 dfda7a2cf273 7 months ago 693MB
- [root@Tseng-HW ~]#
2、创建数据目录
- [root@Tseng-HW docker]# mkdir -p /opt/docker/mongdb/data
- [root@Tseng-HW docker]#
3、运行容器命令
- [root@Tseng-HW docker]# docker run -d --restart=always -p 20007:27017 --name mongodb -v /opt/docker/mongdb/data:/data/db -e MONGO_INITDB_ROOT_USERNAME=tsengadmin -e MONGO_INITDB_ROOT_PASSWORD=tseng#2022 -d mongo:5.0
- 9df69ade730bea3b73fba28e364da448f9f5d186150f99c2b3b69e14d6538bc5
- [root@Tseng-HW docker]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 9df69ade730b mongo:5.0 "docker-entrypoint.s…" 28 seconds ago Up 27 seconds 0.0.0.0:20007->27017/tcp, :::20007->27017/tcp mongodb
4、进入容器操作
- [root@Tseng-HW docker]# docker exec -it 9df6 mongo
- MongoDB shell version v5.0.5
- connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
- Implicit session: session { "id" : UUID("ddc933b5-34c8-47bf-a517-0b27661f6b26") }
- MongoDB server version: 5.0.5
- ================
- 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
- switched to db admin
- > db.auth("tsengadmin","tseng#2022");
- 1
- > show users;
- {
- "_id" : "admin.tsengadmin",
- "userId" : UUID("ae877221-06e7-43a0-b4dd-7eddeaeafb75"),
- "user" : "tsengadmin",
- "db" : "admin",
- "roles" : [
- {
- "role" : "root",
- "db" : "admin"
- }
- ],
- "mechanisms" : [
- "SCRAM-SHA-1",
- "SCRAM-SHA-256"
- ]
- }
- > show dbs;
- admin 0.000GB
- config 0.000GB
- local 0.000GB
- >
5、创建新的库/表(先退出当前操作,重新进入)
- > exit
- bye
- [root@Tseng-HW docker]# docker exec -it 9df6 mongo
- MongoDB shell version v5.0.5
- connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
- Implicit session: session { "id" : UUID("92b55e22-d634-4570-a663-fa524b4d00bc") }
- MongoDB server version: 5.0.5
- ================
- 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/
- ================
- > use admin
- switched to db admin
- > db.auth("tsengadmin","tseng#2022");
- 1
- > use tseng
- switched to db tseng
- > db.createUser({user:"tsengmongo",pwd:"Tseng@2022",roles:[{role: 'dbAdmin', db:'tseng'}]})
- Successfully added user: {
- "user" : "tsengmongo",
- "roles" : [
- {
- "role" : "dbAdmin",
- "db" : "tseng"
- }
- ]
- }
- > show users;
- {
- "_id" : "tseng.tsengmongo",
- "userId" : UUID("ed69eb0f-66c6-4e33-95d2-b68ab3a28b25"),
- "user" : "tsengmongo",
- "db" : "tseng",
- "roles" : [
- {
- "role" : "dbAdmin",
- "db" : "tseng"
- }
- ],
- "mechanisms" : [
- "SCRAM-SHA-1",
- "SCRAM-SHA-256"
- ]
- }
- > db.createCollection("tseng1");
- { "ok" : 1 }
- > db.getCollectionNames();
- [ "tseng1" ]
- > db
- tseng
- > db.getName();
- tseng
- >
6、开放端口、工具连接
7、修改子库/表 连接密码(使用管理员权限用户登录修改密码之后,退出容器,重新进入查库)
- > use admin
- switched to db admin
- > db.auth("tsengadmin","tseng#2022");
- 1
- > use tseng
- switched to db tseng
- > db.changeUserPassword("tsengmongo", "tseng*2022")
- > use tseng
- switched to db tseng
- > db.auth("tsengmongo","tseng*2022");
- 1
- > db.getCollectionNames();
- uncaught exception: Error: listCollections failed: {
- "ok" : 0,
- "errmsg" : "logical sessions can't have multiple authenticated users (for more details see: https://docs.mongodb.com/manual/core/authentication/#authentication-methods)",
- "code" : 13,
- "codeName" : "Unauthorized"
- } :
- _getErrorWithCode@src/mongo/shell/utils.js:25:13
- DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:718:15
- DB.prototype._getCollectionNamesInternal@src/mongo/shell/db.js:790:12
- DB.prototype.getCollectionNames@src/mongo/shell/db.js:799:12
- @(shell):1:1
- > exit
- bye
- [root@Tseng-HW docker]# docker exec -it 9df6 mongo
- MongoDB shell version v5.0.5
- connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
- Implicit session: session { "id" : UUID("cb916f25-5584-4e97-b432-14740e74d193") }
- MongoDB server version: 5.0.5
- ================
- 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/
- ================
- > use tseng
- switched to db tseng
- > db.auth("tsengmongo","tseng*2022");
- 1
- > db.getCollectionNames();
- [ "tseng1" ]
- >
8、数据库常用命令(操作集合\数据库)
help | 帮助 |
db.help() | 集合所有方法 |
db.stats() | 集合当前状态信息 |
db | 当前数据库 |
db.getName() | 当前数据库 |
show dbs | 所有数据库列表 |
use test | 切换数据库 |
db.createCollection('tseng1') | 数据库创建集合/表 |
db.getCollectionNames() | 获取数据库所有集合/表 |
db.dropDatabase() | 删除当前数据库 |
show tables | 查看当前库中的表/集合 |
show collections | 查看当前库中的表/集合 |
db.tseng1.drop() | 删除mycollection集合 |
9、MongoDB的角色
超级用户角色 | root |
数据库用户角色 | read、readWrite |
数据库管理角色 | dbAdmin、dbOwner、userAdmin |
集群管理角色 | clusterAdmin、clusterManager、4. clusterMonitor、hostManage |
备份恢复角色 | backup、restore |
所有数据库角色 | readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase |
内部角色 | __system |
10、MongoDB角色说明
root | 只在admin数据库中可用。超级账号,超级权限 |
Read | 允许用户读取指定数据库 |
readWrite | 允许用户读写指定数据库 |
dbAdmin | 允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile |
userAdmin | 允许用户向system.users集合写入,可以在指定数据库里创建、删除和管理用户 |
clusterAdmin | 只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限 |
readAnyDatabase | 只在admin数据库中可用,赋予用户所有数据库的读权限 |
readWriteAnyDatabase | 只在admin数据库中可用,赋予用户所有数据库的读写权限 |
userAdminAnyDatabase | 只在admin数据库中可用,赋予用户所有数据库的userAdmin权限 |
dbAdminAnyDatabase | 只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限 |