为了支持事务,使用副本集的方式做单机节点: 副本及带认证详细参考我这篇:https://www.cnblogs.com/lshan/p/12035285.html
副本集如果连接不上可以加上参数:mongodb://192.168.186.122:27017/?directConnection=true
#1run
sudo mkdir -p /opt/docker/mongodb/ sudo chmod -R 777 /opt/docker/mongodb/
添加认证:
sudo openssl rand -base64 741 > ~/mongodb-keyfile cp ~/mongodb-keyfile /opt/docker/mongodb/mongodb-keyfile
# mongodb7.x 仅仅600 即可,否则 error too open sudo chmod 600 /opt/docker/mongodb/mongodb-keyfile sudo chown 999 /opt/docker/mongodb/mongodb-keyfile
可以直接使用这个证书(发现有些服务器生成的会报错):
mongodb-keyfile
yG6fnl/ndSSSKl5O25n1pRUFWgY3wluER9ytcd3keENueZbMj+zXoye2PCXOMtV7 rbB1u7wvAiGuXhDApppbPhbZx01UWez//zOpvmHryCCj0K3vARDRqVzGBgMkXd87 pnc/MF11rsN7l/r/3nqPfA8YTc7ykdmXrOZNgv6C+/pDAsy6hw2+mTiflVqKLeAm YuTc4+SHTFaAe8+73CAfZBFuNR/ANvjKbzWUS0egFlmpvXlJPYMtImzJpaWfmr44 gJqLQBQCbvzfdjOfTn2ASkdE8+6aoThpI0q42eIbtW0cqq2LbyQNxeBtbRgOC8Tu rgllih04fzirpImcZ5PpL5lKvOjEuRptKr4cBG4+CFymS1uRdCtSLhEBpEyZhk7O NEq0BYzw+hs8OUDIFu6+rBYygfsPbIS5/cjkt7UxAD1lvQjgktly1Qw8hEQxepGI pwCPpF20geeuHuYhGs2PopiLZJh5XO/teiG8YzBNkPvahbcgb6NpZwU4Y2XPdwT5 htRtC7NHC9GnYJuDrp30S7yb9jQJ02/Up9k4yKbQe2wwoC7WGGmcv5mDQlIlTLbm d2nGfJsfqDwdiScTl07igjglKCLAnCwhuvjdFpY5nMYxbMOfHRcvZEPgyVEgLjBO qbF7apbbThxgYFCSyfMRNvdQn8BhWAA5O12xKmn5JDiNED0RblBjQen/sEvtJuzU f/xh0mzKHpvRhq4xr8jpGtsmGhx0jd8GD/Ks5O6cURD30WPO/AMwaFMkc5IPa9dQ 3SsKvjiQad9L8U4o6wBq505XnxDj7FjwoAkmC6hb9rZb7G8y1Uy4RpyGEQGKIt0u FdGUKaOOnLc/Dx96rsmBmRyW7E2ArQF4FZ3LFhliqol1GdwSBBX0vdOZnOYo2sE1 WvmziTXb52vucXZhY8mYHyTfFXsQoQG+CA+L4sRyGpNarkcKgGVNDeXxpQGDUEE4 oPGLmDfUTisY0TaE9HcJPp77gk4w
--keyFile=/var/local/mongodb-keyfile
mongo:4.4.1 4.4.27
sudo docker run --name mongodb --cpus 4 -m 4G -itd -p 27017:27017 --restart unless-stopped \ -e MONGO_INITDB_ROOT_USERNAME=root \ -e MONGO_INITDB_ROOT_PASSWORD=root \ -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone \ -v /opt/docker/mongodb/configdb/:/data/configdb/ -v /opt/docker/mongodb/db/:/data/db/ \
-v /opt/docker/mongodb/mongodb-keyfile:/var/local/mongodb-keyfile \ mongo:4.4.1 --keyFile=/var/local/mongodb-keyfile --replSet "rs" \ --wiredTigerCacheSizeGB 4 --setParameter wiredTigerConcurrentWriteTransactions=1500 --bind_ip_all
# 6.0 版本中的更改:wiredTigerConcurrentReadTransactions 参数已被重命名为 storageEngineConcurrentWriteTransactions
# https://www.mongodb.com/zh-cn/docs/v6.0/reference/parameters/
mongo:6.0.2 (容器中mongo 变成了 /bin/mongosh --port 27017 , 完成 rs.initiate({_id:"rs",members:[{_id:1,host:"127.0.0.1:27017",priority:1}]});)
sudo docker run --name sea_mongo_6 --cpus 4 -m 4G -itd -p 27017:27017 --restart unless-stopped \ -e MONGO_INITDB_ROOT_USERNAME=root \ -e MONGO_INITDB_ROOT_PASSWORD=root \ -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone \ -v /opt/docker/mongodb/configdb/:/data/configdb/ \ -v /opt/docker/mongodb/db/:/data/db/:rw \ -v /opt/docker/mongodb/mongodb-keyfile:/var/local/mongodb-keyfile \ mongo:6.0.2 mongod --keyFile=/var/local/mongodb-keyfile --replSet "rs" \ --wiredTigerCacheSizeGB 4 --setParameter storageEngineConcurrentWriteTransactions=1500 --bind_ip_all
mongo:7.0.7
sudo docker run --name sea_mongo_7 -m 20G -itd -p 27017:27017 --restart unless-stopped \ -e MONGO_INITDB_ROOT_USERNAME=root \ -e MONGO_INITDB_ROOT_PASSWORD=root@2024 \ -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone \ -v /var/docker/mongodb/configdb/:/data/configdb/ \ -v /var/docker/mongodb/db/:/data/db/:rw \ -v /var/docker/mongodb/mongodb-keyfile:/var/local/mongodb-keyfile \ mongo:7.0.7 mongod --keyFile=/var/local/mongodb-keyfile --replSet "rs" \ --wiredTigerCacheSizeGB 14 --setParameter storageEngineConcurrentWriteTransactions=1500 --setParameter maxTransactionLockRequestTimeoutMillis=300 --bind_ip_all
/bin/mongosh --host 127.0.0.1 --port 27017 -u "root" --authenticationDatabase "admin" -p "capi@root"
rs.initiate({_id:"rs",members:[{_id:0,host:"192.168.18.203:27017",priority:1}]});
事务锁最大等待事件(毫秒)
mongod --setParameter maxTransactionLockRequestTimeoutMillis=3000
#2login
mongo --host 192.168.18.176 --port 27017 -u"root" --authenticationDatabase "admin" -p"root"
#3init (mogo命令行操作)
rs.initiate({_id:"rs",members:[{_id:0,host:"192.168.18.176:27017",priority:1}]});
删除节点(操作失误可以删除) rs.remove("192.168.18.176:27017")
#4check status
rs.conf();
#5后续如果添加节点:
新增加的机器配置同一个副本集名称(mySet)并启动,在PRIMARY主机进行操作: rs.add({_id:1, host: "192.168.18.199:27017", priority: 1}) 新机器会自动进行数据同步,同步完成后变成SECONDARY
#####################################
配置文件:/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 #副本集名称,同一个副本集,名称必须一致
sudo mkdir -p /opt/docker/mongodb/
sudo chmod -R 777 /opt/docker/mongodb/
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/mongodb/configdb/:/data/configdb/ -v /opt/docker/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
当然,也可以指定cpu
docker run --name mongodb --cpus 1 -m 4G -v /alidata/MongoData:/data/db -p 27017:27017 -d mongo:4.4.1 --wiredTigerCacheSizeGB 2.4 --setParameter wiredTigerConcurrentWriteTransactions=1500
参考: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 connect
mongo --port 27017 -u"root" --authenticationDatabase "admin" -p"root" #mongo --host 192.168.18.176 --port 27017 -u"root" --authenticationDatabase "admin" -p"root"
#step4 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" }] })
只读账户
db.createUser({ user: "readonlyUser", pwd: "readonlyUserPsd", roles: [{ role: "read", db: "Test_Db" }] })
if the user is existed,add the new role according to the command:
db.grantRolesToUser("mongodb",[{role:"dbOwner",db:"Test_Db"}])