• Docker mongo:5.0


     1、拉取

    1. [root@Tseng-HW ~]# docker pull mongo:5.0
    2. 5.0: Pulling from library/mongo
    3. 7b1a6ab2e44d: Pull complete
    4. 90eb44ebc60b: Pull complete
    5. 5085b59f2efb: Pull complete
    6. c7499923d022: Pull complete
    7. 019496b6c44a: Pull complete
    8. c0df4f407f69: Pull complete
    9. 351daa315b6c: Pull complete
    10. a233e6240acc: Pull complete
    11. a3f57d6be64f: Pull complete
    12. dd1b5b345323: Pull complete
    13. Digest: sha256:5be752bc5f2ac4182252d0f15d74df080923aba39700905cb26d9f70f39e9702
    14. Status: Downloaded newer image for mongo:5.0
    15. docker.io/library/mongo:5.0
    16. [root@Tseng-HW ~]# docker images
    17. REPOSITORY TAG IMAGE ID CREATED SIZE
    18. mongo 5.0 dfda7a2cf273 7 months ago 693MB
    19. [root@Tseng-HW ~]#

    2、创建数据目录

    1. [root@Tseng-HW docker]# mkdir -p /opt/docker/mongdb/data
    2. [root@Tseng-HW docker]#

    3、运行容器命令

    1. [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
    2. 9df69ade730bea3b73fba28e364da448f9f5d186150f99c2b3b69e14d6538bc5
    3. [root@Tseng-HW docker]# docker ps
    4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    5. 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、进入容器操作

    1. [root@Tseng-HW docker]# docker exec -it 9df6 mongo
    2. MongoDB shell version v5.0.5
    3. connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    4. Implicit session: session { "id" : UUID("ddc933b5-34c8-47bf-a517-0b27661f6b26") }
    5. MongoDB server version: 5.0.5
    6. ================
    7. Warning: the "mongo" shell has been superseded by "mongosh",
    8. which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
    9. an upcoming release.
    10. For installation instructions, see
    11. https://docs.mongodb.com/mongodb-shell/install/
    12. ================
    13. Welcome to the MongoDB shell.
    14. For interactive help, type "help".
    15. For more comprehensive documentation, see
    16. https://docs.mongodb.com/
    17. Questions? Try the MongoDB Developer Community Forums
    18. https://community.mongodb.com
    19. > use admin
    20. switched to db admin
    21. > db.auth("tsengadmin","tseng#2022");
    22. 1
    23. > show users;
    24. {
    25. "_id" : "admin.tsengadmin",
    26. "userId" : UUID("ae877221-06e7-43a0-b4dd-7eddeaeafb75"),
    27. "user" : "tsengadmin",
    28. "db" : "admin",
    29. "roles" : [
    30. {
    31. "role" : "root",
    32. "db" : "admin"
    33. }
    34. ],
    35. "mechanisms" : [
    36. "SCRAM-SHA-1",
    37. "SCRAM-SHA-256"
    38. ]
    39. }
    40. > show dbs;
    41. admin 0.000GB
    42. config 0.000GB
    43. local 0.000GB
    44. >

    5、创建新的库/表(先退出当前操作,重新进入)

    1. > exit
    2. bye
    3. [root@Tseng-HW docker]# docker exec -it 9df6 mongo
    4. MongoDB shell version v5.0.5
    5. connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    6. Implicit session: session { "id" : UUID("92b55e22-d634-4570-a663-fa524b4d00bc") }
    7. MongoDB server version: 5.0.5
    8. ================
    9. Warning: the "mongo" shell has been superseded by "mongosh",
    10. which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
    11. an upcoming release.
    12. For installation instructions, see
    13. https://docs.mongodb.com/mongodb-shell/install/
    14. ================
    15. > use admin
    16. switched to db admin
    17. > db.auth("tsengadmin","tseng#2022");
    18. 1
    19. > use tseng
    20. switched to db tseng
    21. > db.createUser({user:"tsengmongo",pwd:"Tseng@2022",roles:[{role: 'dbAdmin', db:'tseng'}]})
    22. Successfully added user: {
    23. "user" : "tsengmongo",
    24. "roles" : [
    25. {
    26. "role" : "dbAdmin",
    27. "db" : "tseng"
    28. }
    29. ]
    30. }
    31. > show users;
    32. {
    33. "_id" : "tseng.tsengmongo",
    34. "userId" : UUID("ed69eb0f-66c6-4e33-95d2-b68ab3a28b25"),
    35. "user" : "tsengmongo",
    36. "db" : "tseng",
    37. "roles" : [
    38. {
    39. "role" : "dbAdmin",
    40. "db" : "tseng"
    41. }
    42. ],
    43. "mechanisms" : [
    44. "SCRAM-SHA-1",
    45. "SCRAM-SHA-256"
    46. ]
    47. }
    48. > db.createCollection("tseng1");
    49. { "ok" : 1 }
    50. > db.getCollectionNames();
    51. [ "tseng1" ]
    52. > db
    53. tseng
    54. > db.getName();
    55. tseng
    56. >

    6、开放端口、工具连接

    7、修改子库/表 连接密码(使用管理员权限用户登录修改密码之后,退出容器,重新进入查库)

    1. > use admin
    2. switched to db admin
    3. > db.auth("tsengadmin","tseng#2022");
    4. 1
    5. > use tseng
    6. switched to db tseng
    7. > db.changeUserPassword("tsengmongo", "tseng*2022")
    8. > use tseng
    9. switched to db tseng
    10. > db.auth("tsengmongo","tseng*2022");
    11. 1
    12. > db.getCollectionNames();
    13. uncaught exception: Error: listCollections failed: {
    14. "ok" : 0,
    15. "errmsg" : "logical sessions can't have multiple authenticated users (for more details see: https://docs.mongodb.com/manual/core/authentication/#authentication-methods)",
    16. "code" : 13,
    17. "codeName" : "Unauthorized"
    18. } :
    19. _getErrorWithCode@src/mongo/shell/utils.js:25:13
    20. DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:718:15
    21. DB.prototype._getCollectionNamesInternal@src/mongo/shell/db.js:790:12
    22. DB.prototype.getCollectionNames@src/mongo/shell/db.js:799:12
    23. @(shell):1:1
    24. > exit
    25. bye
    26. [root@Tseng-HW docker]# docker exec -it 9df6 mongo
    27. MongoDB shell version v5.0.5
    28. connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    29. Implicit session: session { "id" : UUID("cb916f25-5584-4e97-b432-14740e74d193") }
    30. MongoDB server version: 5.0.5
    31. ================
    32. Warning: the "mongo" shell has been superseded by "mongosh",
    33. which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
    34. an upcoming release.
    35. For installation instructions, see
    36. https://docs.mongodb.com/mongodb-shell/install/
    37. ================
    38. > use tseng
    39. switched to db tseng
    40. > db.auth("tsengmongo","tseng*2022");
    41. 1
    42. > db.getCollectionNames();
    43. [ "tseng1" ]
    44. >

    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权限
  • 相关阅读:
    springboot:集成Kaptcha实现图片验证码
    观察者模式的运用——消息队列
    汽车发电机
    检测文件目录及其子文件到底的代码-实现可展开的目录列表和文件浏览功能的HTML代码
    《C Primer Plus》第6章复习题与编程练习
    力扣日记11.7-【二叉树篇】二叉树的层序遍历
    Shell-02变量
    OpenCV学习-P44 角点检测
    数据结构相关知识点(一)
    竞赛选题 深度学习疲劳驾驶检测 opencv python
  • 原文地址:https://blog.csdn.net/piaomiao_/article/details/125992026