• Docker安装MongoDB


    1、创建主机挂载配置目录

    mkdir -p /docker/mongodb/data && cd /docker/mongodb

    data目录存放mongodb数据库文件,删除重启容器不会丢失

    2、生成启动文件

    2.1 无账户密码,不需要认证

    1. cat <<EOF> start.sh
    2. #!/bin/bash
    3. MONGODB_DIR=`pwd`
    4. docker stop mongodb
    5. docker rm mongodb
    6. docker run -d \\
    7. --name mongodb \\
    8. --restart always \\
    9. --privileged \\
    10. -p 27017:27017 \\
    11. -v \${MONGODB_DIR}/data:/data/db \\
    12. mongo:4.2.2
    13. EOF

    2.2 有账户密码,需要认证(推荐)

    1. cat <<EOF> start.sh
    2. #!/bin/bash
    3. MONGODB_DIR=`pwd`
    4. docker stop mongodb
    5. docker rm mongodb
    6. docker run -d \\
    7. --name mongodb \\
    8. --restart always \\
    9. --privileged \\
    10. -p 27017:27017 \\
    11. -v \${MONGODB_DIR}/data:/data/db \\
    12. -e MONGO_INITDB_ROOT_USERNAME=admin \\
    13. -e MONGO_INITDB_ROOT_PASSWORD=123456 \\
    14. mongo:4.2.2 mongod --auth
    15. EOF

    说明:

    -d: 后台运行容器;
    --name: 指定容器名;
    -p: 指定服务运行的端口;
    -v: 映射目录或文件;
    --privileged 拥有真正的root权限
    --restart=always Docker服务重启容器也启动
    -e MONGO_INITDB_ROOT_USERNAME=admin 指定用户名
    -e MONGO_INITDB_ROOT_PASSWORD=123456 指定密码
    mongod --auth :容器默认启动命令是mongod,我们认证需要修改启动命为mongod --auth开启认证

    3、运行start.sh

    sh start.sh

    停止和删除容器

    docker stop mongodb && docker rm mongodb
    

    4. 使用Robo连接

    image

    image

    image

    启动脚本配置不同,账号密码输入也不一样

    • 无账户密码用户名密码为空就能登录
    • 有账户密码:账号密码是上面设置的admin/123456

    5、进入容器

    1. [root@localhost mongodb]# docker exec -it mongodb bash
    2. root@ce90018683a8:/# mongo --version
    3. MongoDB shell version v4.2.2
    4. git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
    5. OpenSSL version: OpenSSL 1.1.1 11 Sep 2018
    6. allocator: tcmalloc
    7. modules: none
    8. build environment:
    9. distmod: ubuntu1804
    10. distarch: x86_64
    11. target_arch: x86_64
    12. root@ce90018683a8:/#

    6、进入mongodb

    6.1 无密码进入数据库

    root@ce90018683a8:/# mongo
    MongoDB shell version v4.2.2
    connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("e35fba4a-fddf-4e87-b5d4-cce3d6769d63") }
    MongoDB server version: 4.2.2
    Welcome to the MongoDB shell.
    For interactive help, type "help".
    For more comprehensive documentation, seehttp://docs.mongodb.org/
    Questions? Try the support grouphttp://groups.google.com/group/mongodb-user
    # 查看命令

    > help
         db.help()                    help on db methods
         db.mycoll.help()             help on collection methods
         sh.help()                    sharding helpers
         rs.help()                    replica set helpers
         help admin                   administrative help
         help connect                 connecting to a db help
         help keys                    key shortcuts
         help misc                    misc things to know
         help mr                      mapreduce

        show dbs                     show database names
         show collections             show collections in current database
         show users                   show users in current database
         show profile                 show most recent system.profile entries with time >= 1ms
         show logs                    show the accessible logger names
         show log [name]              prints out the last segment of log in memory, 'global' is default
         use                 set current database
         db.foo.find()                list objects in collection foo
         db.foo.find( { a : 1 } )     list objects in foo where a == 1
         it                           result of the last line evaluated; use to further iterate
         DBQuery.shellBatchSize = x   set default number of items to display on shell
         exit                         quit the mongo shell
    >

    6.2 需要密码认证

    在连接期间进行身份验证,使用-u ,-p --authenticationDatabase 命令行选项启动一个mongo shell

    root@ce90018683a8:/# mongo --port 27017 -u "admin" -p "123456" --authenticationDatabase "admin"
    MongoDB shell version v4.2.2
    connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("275dac83-d84c-4147-bb4c-9516cedc534a") }
    MongoDB server version: 4.2.2
    Server has startup warnings:
    2022-08-29T03:39:16.657+0000 I  CONTROL  [initandlisten]
    2022-08-29T03:39:16.657+0000 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
    2022-08-29T03:39:16.657+0000 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2022-08-29T03:39:16.657+0000 I  CONTROL  [initandlisten]
    2022-08-29T03:39:16.657+0000 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2022-08-29T03:39:16.657+0000 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2022-08-29T03:39:16.657+0000 I  CONTROL  [initandlisten]
    ---
    Enable MongoDB's free cloud-based monitoring service, which will then receive and display
    metrics about your deployment (disk utilization, CPU, operation statistics, etc).

    The monitoring data will be available on a MongoDB website with a unique URL accessible to you
    and anyone you share the URL with. MongoDB may use this information to make product
    improvements and to suggest MongoDB products and deployment options to you.

    To enable free monitoring, run the following command: db.enableFreeMonitoring()
    To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
    ---

    > show dbs
    admin   0.000GB
    config  0.000GB
    local   0.000GB
    >

    6.3 以 admin 用户身份进入,先连接后验证

    mongo shell 连接到 mongodb,也就是先连接,后验证用户身份

    1. root@ce90018683a8:/# mongo
    2. MongoDB shell version v4.2.2
    3. connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    4. Implicit session: session { "id" : UUID("6e560085-2f03-4b6a-81a6-b42cd5b03f96") }
    5. MongoDB server version: 4.2.2
    6. > use admin
    7. switched to db admin
    8. > db.auth("admin","123456")
    9. 1
    10. > show dbs
    11. admin 0.000GB
    12. config 0.000GB
    13. local 0.000GB
    14. >

    7、使用命令创建数据库

    1. # 进入admin数据库
    2. [root@localhost mongodb]# docker exec -it mongodb mongo admin
    3. MongoDB shell version v4.2.2
    4. connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
    5. Implicit session: session { "id" : UUID("91508ef9-195d-4a87-a8ae-c36ee3dfcd12") }
    6. MongoDB server version: 4.2.2
    7. # 输入账号密码认证,返回1说明认证成功
    8. > db.auth("admin", "123456")
    9. 1
    10. # 查看所有数据库
    11. > show dbs
    12. admin 0.000GB
    13. config 0.000GB
    14. local 0.000GB
    15. # 创建新数据库
    16. > use oyz
    17. switched to db oyz
    18. # 创建 和新创建的数据库 绑定的用户
    19. > db.createUser({ user: 'haolb', pwd: 'haolb123456', roles: [ { role: "readWrite", db: "oyz" } ] });
    20. Successfully added user: {
    21. "user" : "haolb",
    22. "roles" : [
    23. {
    24. "role" : "readWrite",
    25. "db" : "oyz"
    26. }
    27. ]
    28. }
    29. # exit退出当前用户,否则继续认证新的用户会报错 too many users are authenticated
    30. > exit
    31. bye
    32. # 进入 oyz 数据库
    33. [root@localhost mongodb]# docker exec -it mongodb mongo oyz
    34. MongoDB shell version v4.2.2
    35. connecting to: mongodb://127.0.0.1:27017/oyz?compressors=disabled&gssapiServiceName=mongodb
    36. Implicit session: session { "id" : UUID("b9a9ea61-a15f-43d8-9e4a-dc2bfa0bdb05") }
    37. MongoDB server version: 4.2.2
    38. # 重新认证新的用户
    39. > db.auth("haolb","haolb123456")
    40. 1
    41. # 随便添加一条信息才算创建成功
    42. > db.oyz.insert({"name":"abc1111"});
    43. WriteResult({ "nInserted" : 1 })
    44. > show dbs
    45. oyz 0.000GB
    46. >

    8、使用命令删除用户

    1. # 切换 admin 库
    2. > use admin
    3. switched to db admin
    4. > db.auth("admin","123456")
    5. 1
    6. # 创建一个 myuser 用户
    7. > db.createUser({user: "myuser",pwd: "my123",roles: [ { role: "root", db: "admin" } ]})
    8. Successfully added user: {
    9. "user" : "myuser",
    10. "roles" : [
    11. {
    12. "role" : "root",
    13. "db" : "admin"
    14. }
    15. ]
    16. }
    17. > db.auth("myuser","my123")
    18. 1
    19. > show users
    20. {
    21. "_id" : "admin.admin",
    22. "userId" : UUID("c19fdb6d-efe1-4398-b32a-77ef8c12bac3"),
    23. "user" : "admin",
    24. "db" : "admin",
    25. "roles" : [
    26. {
    27. "role" : "root",
    28. "db" : "admin"
    29. }
    30. ],
    31. "mechanisms" : [
    32. "SCRAM-SHA-1",
    33. "SCRAM-SHA-256"
    34. ]
    35. }
    36. {
    37. "_id" : "admin.myuser",
    38. "userId" : UUID("ec82e9f7-9f81-4a2a-b10b-8368d4750e6c"),
    39. "user" : "myuser",
    40. "db" : "admin",
    41. "roles" : [
    42. {
    43. "role" : "root",
    44. "db" : "admin"
    45. }
    46. ],
    47. "mechanisms" : [
    48. "SCRAM-SHA-1",
    49. "SCRAM-SHA-256"
    50. ]
    51. }
    52. # 删除单个用户:myuser
    53. > db.system.users.remove({user:"myuser"})
    54. WriteResult({ "nRemoved" : 1 })
    55. # 切换 admin 用户登录
    56. > db.auth("admin","123456")
    57. 1
    58. # 显示用户信息
    59. > show users
    60. {
    61. "_id" : "admin.admin",
    62. "userId" : UUID("c19fdb6d-efe1-4398-b32a-77ef8c12bac3"),
    63. "user" : "admin",
    64. "db" : "admin",
    65. "roles" : [
    66. {
    67. "role" : "root",
    68. "db" : "admin"
    69. }
    70. ],
    71. "mechanisms" : [
    72. "SCRAM-SHA-1",
    73. "SCRAM-SHA-256"
    74. ]
    75. }
    76. >
  • 相关阅读:
    新风机小助手-风压变速器
    Analysis of Xiaomi Kernel(Updating)
    Linux进程概念(一)
    asp毕业设计——基于asp+access的文学网站设计与实现(毕业论文+程序源码)——文学网站
    第十一章 函数提高
    RKMPP库快速上手--(一)RKMPP功能及使用详解
    java操作adb查看apk安装包包名【搬代码】
    VSCode自定义闪烁光标
    大数据编程技术基础实验八:Flume实验——文件数据Flume至HDFS
    每日一题:探究响应式本质,以最简单的方式理解响应式
  • 原文地址:https://blog.csdn.net/qq_30665009/article/details/126586070