mongodb集群1主1从环境搭建
vi /home/mongodb_4.0.5/mongodb.conf
path: /home/mongodb_4.0.5/logs/mongod.log
dbPath: /home/mongodb_4.0.5/data
fork: true # 后台启动fork and run in background
pidFilePath: /home/mongodb_4.0.5/logs/mongod.pid # location of pidfile
bindIp: 192.168.x.x # Listen to local interface only, comment to listen on all interfaces.
keyFile: /home/mongodb_4.0.5/keyfile
vi /home/mongodb_rs_4.0.5/mongodb_rs.conf
path: /home/mongodb_rs_4.0.5/logs/mongod.log
dbPath: /home/mongodb_rs_4.0.5/data
fork: true # 后台启动fork and run in background
pidFilePath: /home/mongodb_rs_4.0.5/logs/mongod.pid # location of pidfile
bindIp: 192.168.x.x # Listen to local interface only, comment to listen on all interfaces.
keyFile: /home/mongodb_rs_4.0.5/keyfile
openssl rand -base64 741 >keyfile #注意KEY权限给600
cp mongodb_4.0.5/keyfile mongodb_rs_4.0.5/
nohup /home/mongodb_4.0.5/bin/mongod --config /home/mongodb_4.0.5/mongodb.conf &
nohup /home/mongodb_rs_4.0.5/bin/mongod --config /home/mongodb_rs_4.0.5/mongodb_rs.conf &
/home/mongodb_4.0.5/bin/mongo localhost:27017
{ "_id" : 0, "host" : "192.168.x.x:27017" },
{ "_id" : 1, "host" : "192.168.x.x:27018" }
#新建用户,注意,建立用户后不能再使用localhost连接
admin = db.getSiblingDB("admin")
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
db.getSiblingDB("admin").auth("admin", "admin")
db.getSiblingDB("admin").createUser(
roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
/home/mongodb_4.0.5/bin/mongo 192.168.x.x:27017 -u admin -p admin --authenticationDatabase admin
db.grantRolesToUser ( "admin", [ { role: "__system", db: "admin" } ] )
db.new101.insert({"name":"new"});
/home/mongodb_4.0.5/bin/mongoimport -h 192.168.x.x --port 27017 -u admin -p admin --authenticationDatabase admin -d a003 -c xxx --file /data/xxx/xxx.json
/home/mongodb_4.0.5/bin/mongoimport -h 192.168.x.x --port 27017 -u admin -p admin --authenticationDatabase admin --db test2021 -c xxx --type csv --headerline --ignoreBlanks --file /data/xxx/xxx/xxx.csv
/home/mongodb_4.0.5/bin/mongoexport -h 192.168.x.x --port 27017 -u admin -p admin --authenticationDatabase admin -d a003 -c xxx --csv -f xxx -o /data/xxx/csv/xxx.csv