zoukankan      html  css  js  c++  java
  • Docker搭建MongoDB

    前置条件:

    64-bit 系统
    kernel 3.10+
    1.检查内核版本,返回的值大于3.10即可。官方文档要求Linux kernel至少3.8以上,且docker只能运行在64位的系统中。

    [root@Slave ~]# uname -r
    3.10.0-327.el7.x86_64
    [root@Slave ~]# getconf LONG_BIT
    64

    查看内核是否支持aufs
    [root@Slave ~]# grep aufs /proc/filesystems
    [root@Slave ~]# yum install https://get.docker.com/rpm/1.7.1/centos-6/RPMS/x86_64/docker-engine-1.7.1-1.el6.x86_64.rpm
    [root@Slave ~]# service docker start

    1、搜索mongo
    [root@Slave tmp]# cd /tools/
    [root@Slave tools]# docker search mongo
    INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
    docker.io docker.io/mongo MongoDB document databases provide high av... 7175 [OK]
    docker.io docker.io/mongo-express Web-based MongoDB admin interface, written... 773 [OK]
    docker.io docker.io/tutum/mongodb MongoDB Docker image – listens in port 270... 229 [OK]
    docker.io docker.io/bitnami/mongodb Bitnami MongoDB Docker Image 127 [OK]
    docker.io docker.io/mongoclient/mongoclient Official docker image for Mongoclient, fea... 89 [OK]
    docker.io docker.io/mongooseim/mongooseim Small docker image for MongooseIM - robust... 19
    docker.io docker.io/cvallance/mongo-k8s-sidecar Kubernetes side car to setup and maintain ... 18 [OK]
    docker.io docker.io/frodenas/mongodb A Docker Image for MongoDB 18 [OK]
    docker.io docker.io/arm64v8/mongo MongoDB document databases provide high av... 10
    docker.io docker.io/centos/mongodb-32-centos7 MongoDB NoSQL database server 8
    docker.io docker.io/circleci/mongo CircleCI images for MongoDB 8 [OK]
    docker.io docker.io/webhippie/mongodb Docker images for MongoDB 7 [OK]
    docker.io docker.io/centos/mongodb-36-centos7 MongoDB NoSQL database server 6
    docker.io docker.io/istepanov/mongodump Docker image with mongodump running as a c... 6 [OK]
    docker.io docker.io/centos/mongodb-26-centos7 MongoDB NoSQL database server 5
    docker.io docker.io/eses/mongodb_exporter mongodb exporter for prometheus 4 [OK]
    docker.io docker.io/neowaylabs/mongodb-mms-agent This Docker image with MongoDB Monitoring ... 4 [OK]
    docker.io docker.io/centos/mongodb-34-centos7 MongoDB NoSQL database server 3
    docker.io docker.io/andreasleicher/mongo-azure-backup a docker container to backup a mongodb usi... 2 [OK]
    docker.io docker.io/ansibleplaybookbundle/mongodb-apb An APB to deploy MongoDB. 1 [OK]
    docker.io docker.io/ekesken/mongo docker image for mongo that is configurabl... 1 [OK]
    docker.io docker.io/openshift/mongodb-24-centos7 DEPRECATED: A Centos7 based MongoDB v2.4 i... 1
    docker.io docker.io/ccitest/mongo CircleCI test images for Mongo 0 [OK]
    docker.io docker.io/martel/mongo-replica-ctrl A dockerized controller for a Mongo db rep... 0 [OK]
    docker.io docker.io/phenompeople/mongodb MongoDB is an open-source, document datab... 0 [OK]
    [root@Slave tools]#

    2、拉取mongo【这里默认为latest】
    [root@Slave tools]# docker pull mongo
    Using default tag: latest
    Trying to pull repository docker.io/library/mongo ...
    latest: Pulling from docker.io/library/mongo
    5d9821c94847: Pull complete
    a610eae58dfc: Pull complete
    a40e0eb9f140: Pull complete
    3242ba6cef1f: Pull complete
    8ade7416f0cf: Pull complete
    cd8d2aab224e: Pull complete
    9d8c2ff7f392: Pull complete
    d29b99c4ab4c: Pull complete
    5d1ed7c1266e: Pull complete
    77f31d60b382: Pull complete
    e328c48077a2: Pull complete
    31085c577c4a: Pull complete
    Digest: sha256:ebcdb042054d9974c8c3160d761b0bdb39b55115448242de1a5161c124ddb0af
    Status: Downloaded newer image for docker.io/mongo:latest
    [root@Slave tools]#

    3、查看本地镜像
    [root@Slave tools]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    busybox v3 08dfcb299a86 20 hours ago 1.23 MB
    docker.io/mongo latest 50e17a9fdd96 5 days ago 492 MB
    docker.io/busybox latest 6858809bf669 13 days ago 1.23 MB
    docker.io/tomcat 8.0 ef6a7c98d192 2 years ago 356 MB
    [root@Slave tools]#

    4、启动容器【就是安装,-v后面的参数表示把数据文件挂载到宿主机的路径,-p把mongo端口映射到宿主机的指定端口,--auth表示连接mongodb需要授权】
    [root@Slave tools]# docker run -p 27017:27017 -v /tmp/db:/data/db -d mongo
    896f0b4d59959d0cc6ec1ab196008c4fcc0320da0f2acf76b0c9aa5ed32f0e3b
    [root@Slave tools]#

    5、查看运行中的容器
    [root@Slave tools]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    896f0b4d5995 mongo "docker-entrypoint..." 15 seconds ago Up 14 seconds 0.0.0.0:27017->27017/tcp sad_swartz
    930fbd523c13 tomcat:8.0 "catalina.sh run" 22 hours ago Up 21 hours 0.0.0.0:8888->8080/tcp tomcat
    [root@Slave tools]#

    6、进入mongo【以 admin 用户身份进入mongo,相当于oracle的用户也就是库,mysql的库】,896f0b4d5995 镜像仓库id
    [root@Slave tools]# docker exec -it 896f0b4d5995 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("b7bcaa79-f6e3-449e-b455-b7459c926878") }
    MongoDB server version: 4.4.1
    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
    ---
    The server generated these startup warnings when booting:
    2020-09-22T03:22:49.272+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
    2020-09-22T03:22:49.273+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
    2020-09-22T03:22:49.273+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
    ---
    ---
    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()
    ---
    >

    7、创建一个 admin 管理员账号
    > db.createUser({ user: 'admin', pwd: '123789654', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
    Successfully added user: {
    "user" : "admin",
    "roles" : [
    {
    "role" : "userAdminAnyDatabase",
    "db" : "admin"
    }
    ]
    }
    >

    8、对 admin 进行身份认证
    > db.auth("admin","123789654");
    1
    > show databases;
    admin 0.000GB
    config 0.000GB
    local 0.000GB
    >

    MongoDB 创建数据库
    语法
    MongoDB 创建数据库的语法格式如下:

    use DATABASE_NAME
    如果数据库不存在,则创建数据库,否则切换到指定数据库。

    实例
    以下实例我们创建了数据库 runoob:
    > use runoob
    switched to db runoob
    > db
    runoob
    >

  • 相关阅读:
    Atitit ..Net Framework sdk 3.0 3.5 4.04.5 4.6 4.7  .net core版本新特性 v2 s22 1. 新特性来源于 down ms 官方网站 1
    Atitit 工业体系的分类 目录 1. 有多少个灰色 黑色行业?? 1 2. 30个制造业行业细分 制造业下面还有更精细的划分,见下表1.2: 2 3. 艾提拉需要掌握的体系 3 4. 艾提拉需要
    Atitit 提升开发效率 通道化驱动化数据操作与查询 目录 1. xiaoguo 1 1.1. Atitit mybatis 简化开发与提升开发效率法 1 2. Code 1 3. ref 4
    Atitit 常见聚合运算与算法 目录 1.1. 单行函数,vs 那就是聚合函数 1 1.2. 聚合分类 哈希聚合 标量聚合 流聚合 1 1.3. 常见聚合函数 mysql oracle等 1 r
    Atitit git 使用法v2 目录 1. Git客户端工具 1 1.1. Tortoisegit git gui 图形化工具。。 1 1.2. Ide中的git插件(eclipse idea
    Atitit 提升开发效率的方法 提升语言级别 目录 1. 提升语言级别到4gl 1 1.1. 语言的代际关系 sql 》script 》java 1 1.2. 使用4gl dsl语言与api 1
    Atitit mybatis返回多个数据集总结 目录 1.1. 配置handleResult接受,但是只有第一个select语句的结果 1 2. 配置resultMap ok 1 2.1. 调
    Atitit pdf转文本 pdf2txt v4 t83.docx Atitit pdf转文本 pdfutil 目录 1.1. Pdfbox cmd 模式 TextToPDF 1 1.2. Pdf
    Atitit mybatis 简化开发与提升开发效率法 目录 1.1. 使用注解模式代替xml模式 1 1.2. 使用通用mapper代替专用mapper 1 1.3. 使用js等脚本语言来输出sq
    Atitit mq的AMQP 协议 STOMP2 、MQTT3 等协议  MQTT,XMPP,STOMP,AMQP,WAMP 目录 1. AMQP in a Nutshell 1 2. MQTT概述
  • 原文地址:https://www.cnblogs.com/zhangkaimin/p/13791385.html
Copyright © 2011-2022 走看看