• 01-Docker部署MongoDB


    命令一键部署

    拉取镜像

    # docker pull mongdo:4.0.3
    
    • 1

    方式一:

    数据为持久化:不挂载容器卷

    # docker run -itd --name mongo -p 27017:27017 mongo --auth
    
    • 1
    • -itd 交互后台运行容器
    • –name 自定义容器名称

    创建mongoDB账号

    1)进入容器创建数据库访问用户账号密码,并且尝试连接

    [root@nhk ~]# docker exec -it mongo mongo admin
    MongoDB shell version v5.0.5
    connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("2c914490-5b3d-4c44-b85f-abc0963cf2c2") }
    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
    > 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2)执行mongoDB 语句, 创建mongoDB账号

    > db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
    Successfully added user: {
    	"user" : "admin",
    	"roles" : [
    		{
    			"role" : "userAdminAnyDatabase",
    			"db" : "admin"
    		},
    		"readWriteAnyDatabase"
    	]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3)使用创建账号进行连接

    > db.auth("admin","123456")
    1
    
    • 1
    • 2

    方式二:

    数据持久化:挂载容器卷

    # docker run --name my_mongo -v /opt/data/mongodb:/data/db --rm -d -p 27017:27017 mongo:4.0.3
    
    • 1

    参数说明:

    • -itd 交互后台运行容器
    • –name 自定义容器名称
    • -p 27017:27017 容器服务的 27017 端口映射到宿主机的 端口 27017,那么外部可以通过 宿主机 IP:27017 访问容器内 mongo。
    • 镜像名
    • –auth 需要密码才能访问容器服务

    进入容器

    # docker exec -it mongo /bin/bash
    
    • 1

    使用mongo客户端

    mongo
    
    • 1
  • 相关阅读:
    开箱即用 yyg-cli(脚手架工具):快速创建 vue3 组件库和vue3 全家桶项目
    TRex学习之旅八
    python(牛客)试题解析1 - 简单
    金蝶云星空——单据附件上传
    等差求解一个网络的权重无法适配到别的数据集上
    Linux下yum源配置实战
    Nginx+keepalived 高可用高性能
    WCF 请求答复模式
    单链表的定义(数据结构与算法)
    六自由度电动并联机器人结构设计(说明书+任务书+cad图纸+proe三维图)
  • 原文地址:https://blog.csdn.net/weixin_56058578/article/details/133792255