mongodb集群1主1从环境搭建

准备环境

#192.168.x.x

安装包官网下载

#mongodb_27017配置文件

vi /home/mongodb_4.0.5/mongodb.conf

systemLog:

  destination: file

  logAppend: true

  path: /home/mongodb_4.0.5/logs/mongod.log

storage:

  dbPath: /home/mongodb_4.0.5/data

  journal:

    enabled: true

processManagement:

  fork: true  # 后台启动fork and run in background

  pidFilePath: /home/mongodb_4.0.5/logs/mongod.pid  # location of pidfile

# network interfaces

net:

  port: 27017

  bindIp: 192.168.x.x  # Listen to local interface only, comment to listen on all interfaces.

security:

  authorization: enabled

  clusterAuthMode: keyFile

  keyFile: /home/mongodb_4.0.5/keyfile

replication:

  replSetName: sdp

#mongodb_27018配置文件

vi /home/mongodb_rs_4.0.5/mongodb_rs.conf

systemLog:

  destination: file

  logAppend: true

  path: /home/mongodb_rs_4.0.5/logs/mongod.log

storage:

  dbPath: /home/mongodb_rs_4.0.5/data

  journal:

    enabled: true

processManagement:

  fork: true  # 后台启动fork and run in background

  pidFilePath: /home/mongodb_rs_4.0.5/logs/mongod.pid  # location of pidfile

# network interfaces

net:

  port: 27018

  bindIp: 192.168.x.x  # Listen to local interface only, comment to listen on all interfaces.

security:

  authorization: enabled

  clusterAuthMode: keyFile

  keyFile: /home/mongodb_rs_4.0.5/keyfile

 

replication:

  replSetName: sdp

 

#生成keyfilea**文件

openssl rand -base64 741 >keyfile  #注意KEY权限给600

#2个服务各有一份相同文件

cp mongodb_4.0.5/keyfile mongodb_rs_4.0.5/

#启动服务1

nohup /home/mongodb_4.0.5/bin/mongod --config /home/mongodb_4.0.5/mongodb.conf &

#启动服务2

nohup /home/mongodb_rs_4.0.5/bin/mongod --config /home/mongodb_rs_4.0.5/mongodb_rs.conf &

 

#localhost连接进行配置

/home/mongodb_4.0.5/bin/mongo localhost:27017

#集群配置

conf=

    {

    "_id" : "sdp",

    "members" : [

        { "_id" : 0, "host" : "192.168.x.x:27017" },

        { "_id" : 1, "host" : "192.168.x.x:27018" }

        ]

    }

rs.initiate(conf)

#查看是否成功:

rs.status()

mongodb集群1主1从环境搭建

#新建用户,注意,建立用户后不能再使用localhost连接

admin = db.getSiblingDB("admin")

admin.createUser(

  {

    user: "admin",

    pwd: "admin",

    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

  }

)

#注册用户到数据库

db.getSiblingDB("admin").auth("admin", "admin")

#建立集群用户,可以后续管理集群副本集

db.getSiblingDB("admin").createUser(

  {

    "user" : "cluster",

    "pwd" : "cluster",

    roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]

  }

)

#使用用户连接

/home/mongodb_4.0.5/bin/mongo 192.168.x.x:27017 -u admin -p admin --authenticationDatabase admin

show dbs

#授权

use admin

db.grantRolesToUser ( "admin", [ { role: "__system", db: "admin" } ] )

use a003;

db.new101.insert({"name":"new"});

#正常就完事了

mongodb集群1主1从环境搭建

数据导入导出

导入json

/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

导入csv

/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

导出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