拉取镜像

  1. docker pull mongo

创建并运行镜像

  1. docker run --name mymongo -p 27017:27017 -v /usr/local/mongodb/db:/data/db -d mongo:latest
  • 加了 auth 验证的命令:
  1. docker run --name mymongo -p 27017:27017 -v /usr/local/mongodb/db:/data/db -d mongo:latest --auth

在上述命令中,只是挂载配置文件并没有用:

  1. -v /usr/local/mongodb/mongod.conf:/etc/mongod.conf

测试,改端口为 27018,名为 testmongo

  1. docker run --name testmongo -p 27018:27018 -v /usr/local/testmongo/db:/data/db -v /usr/local/mongodb/mongod.conf:/etc/mongod.conf -d mongo:latest --auth

创建账号密码

  • 进入容器内部

    1. docker exec -it mymongo /bin/bash
  • 进入 mongo shell 并选择数据库 admin。

    1. mongo
    2. >use admin
  • 创建一个管理员账号

    1. > db.createUser(
    2. ... {
    3. ... user:"root",
    4. ... pwd:"xxxxx",
    5. ... roles:["root"]
    6. ... }
    7. ... );
    8. Successfully added user: { "user" : "root", "roles" : [ "root" ] }
  • ocr mongo

    配置文件

    默认配置文件,只是加了安全验证。 ```python

    mongod.conf

for documentation of all options, see:

http://docs.mongodb.org/manual/reference/configuration-options/

Where and how to store data.

storage: dbPath: /var/lib/mongodb journal: enabled: true

engine:

mmapv1:

wiredTiger:

where to write logging data.

systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log

network interfaces

net: port: 27017 bindIp: 127.0.0.1

how the process runs

processManagement: timeZoneInfo: /usr/share/zoneinfo

security: authorization: enabled

operationProfiling:

replication:

sharding:

Enterprise-Only Options:

auditLog:

snmp:

```