运行mongo DB 官方镜像,以及基本操作

配置文件:/etc/mongod.conf.orig

dbpath=D:/mongodb/rs/data   #数据存放目录  
logpath=D:/mongodb/rs/logs/mongod.log  #日志文件目录  
port=12345   #mongodb端口  
logappend=true   #追加方式写日志文件  
fork=true        #后台运行  
journal=true     #启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里  
oplogSize=2048   #同步操作记录文件大小(MB)  
smallfiles=true  #使用较小的默认文件  
replSet=dbset    #副本集名称,同一个副本集,名称必须一致
docker run -it -d --restart unless-stopped --name mongodb4.1_server -p 27017:27017  
-v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone
-v /opt/docker/logs/mongodb/:/data/configdb/ -v /opt/mongodb/db/:/data/db/
-e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=root mongo --wiredTigerCacheSizeGB 8

--wiredTigerCacheSizeGB 8
查看mongod -h发现mongod提供了额外的可选参数来控制WiredTiger存储引擎所占用的cache size。需要注意的是,cache size设置较低,同时mongodb复杂查询很频繁的话,会有延迟发生。
cacheSizeGB 指的就是Cache size,包括数据和索引。Mongod本身使用内存如连接池堆栈以及sorting buffer等都是额外的,不会被统计到这个数字里面。如果索引在内存,查询冷数据取决于你的IO能力。

如果IO latency很低,系统也没有是高负载,那响应时间应该是毫秒级的区别。但是如果查询很频繁,又涉及到很多范围、批量查询,IOPS又跟不上,那就有可能到几百几千毫秒都有可能。
原文链接:https://blog.csdn.net/luyaoying001/java/article/details/75576820

sudo docker  run -itd --restart unless-stopped    -p27017:27017   mongo

参考:https://hub.docker.com/_/mongo?tab=description

Using a custom MongoDB configuration file

For a more complicated configuration setup, you can still use the MongoDB configuration file. mongod does not read a configuration file by default, so the --config option with the path to the configuration file needs to be specified. Create a custom configuration file and put it in the container by either creating a custom Dockerfile FROM mongo or mounting it from the host machine to the container. See the MongoDB manual for a full list of configuration file options.

For example, /my/custom/mongod.conf is the path to the custom configuration file. Then start the MongoDB container like the following:

$ docker run --name some-mongo -v /my/custom:/etc/mongo -d mongo --config /etc/mongo/mongod.conf

Environment Variables

When you start the mongo image, you can adjust the initialization of the MongoDB instance by passing one or more environment variables on the docker run command line. Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup.

MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORD

These variables, used in conjunction, create a new user and set that user's password. This user is created in the admin authentication database and given the role of root, which is a "superuser" role.

The following is an example of using these two variables to create a MongoDB instance and then using the mongo cli to connect against the admin authentication database.

$ docker run -d --network some-network --name some-mongo 
    -e MONGO_INITDB_ROOT_USERNAME=mongoadmin 
    -e MONGO_INITDB_ROOT_PASSWORD=secret 
    mongo

$ docker run -it --rm --network some-network mongo 
    mongo --host some-mongo 
        -u mongoadmin 
        -p secret 
        --authenticationDatabase admin 
        some-db
> db.getName();
some-db

Both variables are required for a user to be created. If both are present then MongoDB will start with authentication enabled (mongod --auth).

Authentication in MongoDB is fairly complex, so more complex user setup is explicitly left to the user via /docker-entrypoint-initdb.d/ (see the Initializing a fresh instance and Authentication sections below for more details).

MONGO_INITDB_DATABASE

This variable allows you to specify the name of a database to be used for creation scripts in /docker-entrypoint-initdb.d/*.js (see Initializing a fresh instance below). MongoDB is fundamentally designed for "create on first use", so if you do not insert data with your JavaScript files, then no database is created.

#step 1: create account

1. mongo; 

2. use admin; 

3. db.createUser({ user: "root", pwd: "root", roles: [{ role: "root", db: "admin" }] }); 

4. db.auth("root", "root");

#step2: set authenticate

/etc/mongod.conf 

auth = true  (uncomment it if it's comment)

 

#step3 check connect

mongo --port 27017 -u"root" --authenticationDatabase "admin" -p"root"

 

Create user and set role

 

use Test_Db

 

if user isn’t existed,then create user.

db.createUser({ user: "mongodb", pwd: "root", roles: [{ role: "dbOwner", db: "Test_Db" }] })

if the user is existed,add the new role according to the command:

>db.grantRolesToUser("mongodb",[{role:"dbOwner",db:"Test_Db"}])

 

 
原文地址:https://www.cnblogs.com/lshan/p/11497359.html