몽고디비 클러스터
/etc/m0d.conf
m0d.conf
m1d.conf
m2d.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /mongodb/s0
directoryPerDB: true
journal:
enabled: true
# engine:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/m0d.log
# network interfaces
net:
port: 27010
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
#operationProfiling:
replication:
replSetName: "mainSet"
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
/lib/systemd/system/m0d.service
m0d.service
m1d.service
m2d.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target
[Service]
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
# Environment="MONGODB_CONFIG_OVERRIDE_NOFORK=1"
ExecStart=/usr/bin/mongod --config /etc/m0d.conf
RuntimeDirectory=mongodb
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings
[Install]
WantedBy=multi-user.target
sudo chown -R mongodb:mongodb /mongodb/s0
sudo systemctl daemon-reload
I fix it by adding parameter suffix "?readPreference=secondary&directConnection=true" to connection string, so the connection string look like,
"mongodb://name:passwd@ip:port?readPreference=secondary&directConnection=true"
https://stackoverflow.com/a/72877483
nodejs or golang client can not access mongodb, however, mongsh can connect and read with same auth setting
I develop an app which working well on reading and writing data to remote mongodb, recently, we change to another remote mongodb instance with master-slave setting, however, encounter the following
stackoverflow.com
not primary and secondaryOk=false
db.getMongo().setReadPref("primaryPreferred")
rs.initiate(
{
_id: "mainSet",
version: 1,
members: [
{ _id: 0, host : "[ip]:27010" },
{ _id: 1, host : "[ip]:27011" },
{ _id: 2, host : "[ip]:27012" },
]
}
)
rs.secondaryOk()