MongoDB分布式集群搭建 (副本集 + 分片集群)

副本集+分片集群方式部署MongoDB分布式集群,实现生产环境上的高可用。

MongoDB分布式集群搭建 (副本集+分片集群)

在三台Linux服务器上搭建MongoDB分布式集群,采用副本集+分片集群方式进行部署,并设置密码认证,以实现生产环境上的高可用。

一、部署环境

二、集群规划

node1实例 node2实例 node3实例 端口 副本集
mongos mongos mongos 20000
config config config 21000 config
shard1主 shard1副 shard1仲裁 27001 shard1
shard2仲裁 shard2主 shard2副 27002 shard2
shard3副 shard3仲裁 shard3主 27003 shard3

三、安装部署

==以下安装部署在3台机器上都需进行。==

1. 软件安装

​ 下载mongodb安装包,将mongodb安装包解压至/opt/mongodb目录,将/opt/mongodb/bin目录设置到系统环境变量PATH中,完成软件安装。

2. 建立数据目录

mkdir -p /var/run/mongodb
mkdir -p /tmp/mongodb_config
mkdir -p /tmp/mongodb_shard1
mkdir -p /tmp/mongodb_shard2
mkdir -p /tmp/mongodb_shard3
mkdir -p /tmp/mongodb_mongos
mkdir -p /data/mongodb/keyfile
mkdir -p /data/mongodb/config/log
mkdir -p /data/mongodb/config/data
mkdir -p /data/mongodb/shard1/log/
mkdir -p /data/mongodb/shard1/data
mkdir -p /data/mongodb/shard2/log/
mkdir -p /data/mongodb/shard2/data
mkdir -p /data/mongodb/shard3/log/
mkdir -p /data/mongodb/shard3/data
mkdir -p /data/mongodb/mongos/log

​ 目录说明:

  • /var/run/mongodb

mongodb运行pid存放路径

  • /tmp/mongodb_xxx

unix Socket存放路径

  • /data/mongodb/keyfile

安装认证文件存放路径

  • /data/mongodb/xxx/log

日志文件存放路径

  • /data/mongodb/xxx/data

数据文件存放路径

3.生成认证文件

cd  /data/mongodb/keyfile
openssl rand -base64 741 > mongodb-keyfile
chmod 400 mongodb-keyfile

4.配置并启动config server

  • 编辑生成config server配置文件config.conf至/opt/mongodb目录

    systemLog:
    destination: file
    logAppend: true
    path: /data/mongodb/config/log/config.log
    
    storage:
    dbPath: /data/mongodb/config/data
    journal:
    enabled: true
    wiredTiger:
    engineConfig:
      directoryForIndexes: true
    
    processManagement:
    fork: true
    pidFilePath: /var/run/mongodb/config.pid
    timeZoneInfo: /usr/share/zoneinfo
    
    net:
    port: 21000
    bindIpAll: true
    maxIncomingConnections: 65536
    unixDomainSocket:
    enabled: true
    pathPrefix: /tmp/mongodb_config
    filePermissions: 0700
    
    security:
    keyFile: /data/mongodb/keyfile/mongodb-keyfile
    clusterAuthMode: keyFile
    authorization: enabled
    javascriptEnabled: true
    
    replication:
    replSetName: config
    secondaryIndexPrefetch: all
    sharding:
    clusterRole: configsvr
    
  • 启动config实例

    mongod -f /opt/mongodb/config.conf
    

5.配置并启动shard1 server

  • 编辑生成shard1 server配置文件shard1.conf至/opt/mongodb目录

    systemLog:
    destination: file
    logAppend: true
    path: /data/mongodb/shard1/log/mongodb.log
    
    storage:
    dbPath: /data/mongodb/shard1/data
    journal:
    enabled: true
    wiredTiger:
    engineConfig:
      directoryForIndexes: true     
    
    processManagement:
    fork: true
    pidFilePath: /var/run/mongodb/shard1.pid
    timeZoneInfo: /usr/share/zoneinfo
    
    net:
    port: 27001
    bindIpAll: true
    maxIncomingConnections: 65536
    unixDomainSocket:
    enabled: true
    pathPrefix: /tmp/mongodb_shard1
    filePermissions: 0700
    
    security:
    keyFile: /data/mongodb/keyfile/mongodb-keyfile
    clusterAuthMode: keyFile
    authorization: enabled
    javascriptEnabled: true
    
    replication:
    replSetName: shard1
    secondaryIndexPrefetch: all
    sharding:
    clusterRole: shardsvr
    
  • 启动shard1实例

    mongod -f /opt/mongodb/shard1.conf
    

6.配置并启动shard2 server

  • 编辑生成shard2 server配置文件shard2.conf至/opt/mongodb目录

    systemLog:
    destination: file
    logAppend: true
    path: /data/mongodb/shard2/log/mongodb.log
    
    storage:
    dbPath: /data/mongodb/shard2/data
    journal:
    enabled: true
    wiredTiger:
    engineConfig:
      directoryForIndexes: true     
    
    processManagement:
    fork: true
    pidFilePath: /var/run/mongodb/shard2.pid
    timeZoneInfo: /usr/share/zoneinfo
    
    net:
    port: 27002
    bindIpAll: true
    maxIncomingConnections: 65536
    unixDomainSocket:
    enabled: true
    pathPrefix: /tmp/mongodb_shard2
    filePermissions: 0700
    
    security:
    keyFile: /data/mongodb/keyfile/mongodb-keyfile
    clusterAuthMode: keyFile
    authorization: enabled
    javascriptEnabled: true
    
    replication:
    replSetName: shard2
    secondaryIndexPrefetch: all
    sharding:
    clusterRole: shardsvr
    
  • 启动shard2实例

    mongod -f /opt/mongodb/shard2.conf
    

7.配置并启动shard3 server

  • 编辑生成shard3 server配置文件shard3.conf至/opt/mongodb目录

    systemLog:
    destination: file
    logAppend: true
    path: /data/mongodb/shard3/log/mongodb.log
    
    storage:
    dbPath: /data/mongodb/shard3/data
    journal:
    enabled: true
    wiredTiger:
    engineConfig:
      directoryForIndexes: true     
    
    processManagement:
    fork: true
    pidFilePath: /var/run/mongodb/shard3.pid
    timeZoneInfo: /usr/share/zoneinfo
    
    net:
    port: 27003
    bindIpAll: true
    maxIncomingConnections: 65536
    unixDomainSocket:
    enabled: true
    pathPrefix: /tmp/mongodb_shard3
    filePermissions: 0700
    
    security:
    keyFile: /data/mongodb/keyfile/mongodb-keyfile
    clusterAuthMode: keyFile
    authorization: enabled
    javascriptEnabled: true
    
    replication:
    replSetName: shard3
    secondaryIndexPrefetch: all
    sharding:
    clusterRole: shardsvr
    
  • 启动shard3实例

    mongod -f /opt/mongodb/shard3.conf
    

8.配置并启动mongos server

  • 编辑生成mongos server配置文件mongos.conf至/opt/mongodb目录

    systemLog:
    destination: file
    logAppend: true
    path: /data/mongodb/mongos/log/mongodb.log
    
    processManagement:
    fork: true
    pidFilePath: /var/run/mongodb/mongos.pid
    timeZoneInfo: /usr/share/zoneinfo
    
    net:
    port: 20000
    bindIpAll: true
    maxIncomingConnections: 65536
    unixDomainSocket:
    enabled: true
    pathPrefix: /tmp/mongodb_mongos
    filePermissions: 0700
    
    security:
    keyFile: /data/mongodb/keyfile/mongodb-keyfile
    clusterAuthMode: keyFile
    
    replication:
    localPingThresholdMs: 15
    
    sharding:
    configDB: config/node1:21000,node2:21000,node3:21000
    
  • 启动mongos实例

    mongos -f /opt/mongodb/mongos.conf
    

四、配置分片集

1.配置config分片

  • 登录任一机器的config实例

    mongo --port 21000
    
  • 设置分片信息

    use admin
      
    config = {
    _id : "config",
    members : [
       {_id : 0, host : "node1:21000" },
       {_id : 1, host : "node2:21000" },
       {_id : 2, host : "node3:21000" }
      ]
    }
      
    rs.initiate(config)
      
    rs.status()
    

2.配置shard1分片

  • 登录任一机器的shard1实例

    mongo --port 27001
    
  • 设置分片信息

    use admin
      
    config = {
    _id : "shard1",
     members : [
         {_id : 0, host : "node1:27001" },
         {_id : 1, host : "node2:27001" },
         {_id : 2, host : "node3:27001" , arbiterOnly: true}
     ]
    }  
      
    rs.initiate(config)
      
    rs.status()
    

3.配置shard2分片

  • 登录任一机器的shard2实例

    mongo --port 27002
    
  • 设置分片信息

    use admin
      
    config = {
    _id : "shard2",
     members : [
         {_id : 0, host : "node1:27002", arbiterOnly: true},
         {_id : 1, host : "node2:27002" },
         {_id : 2, host : "node3:27002"}
     ]
    }  
      
    rs.initiate(config)
      
    rs.status()
    

4.配置shard3分片

  • 登录任一机器的shard3实例

    mongo --port 27003
    
  • 设置分片信息

    use admin
      
    config = {
    _id : "shard3",
     members : [
         {_id : 0, host : "node1:27003"},
         {_id : 1, host : "node2:27003", arbiterOnly: true},
         {_id : 2, host : "node3:27003"}
     ]
    }  
      
    rs.initiate(config)
      
    rs.status()
    

5.在mongos添加分片信息

  • 登录任一机器的mongos实例

    mongo --port 20000
    
  • 添加分片信息

    use admin
      
    sh.addShard("shard1/node1:27001,node2:27001,node3:27001")
    sh.addShard("shard2/node1:27002,node2:27002,node3:27002")
    sh.addShard("shard3/node1:27003,node2:27003,node3:27003")
    

6.创建数据库和集合,设置数据分片

// 创建数据库
use db1
// 创建集合
db.createCollection("collection1")
// 设置数据分片
sh.enableSharding("db1")
sh.shardCollection("db1.collection1", {"field1": "hashed" })

// 查看设置数据分片状态
db.printShardingStatus()

五、创建用户及设置密码

1. 创建管理账户

use admin

db.createUser({ 
    user: 'admin', 
    pwd: 'admin', 
    roles: [ { role: "root", db: "admin" } ] });
    
    
    db.auth("admin","admin") 
    
    
    db.createUser({ 
    user: 'dbadmin', 
    pwd: 'dbadmin', 
    roles: [ { role: "dbAdminAnyDatabase", db: "admin" } ] });

2.创建数据库用户账户并设置密码

use db1

db.createUser({ 
    user: 'db1owner', 
    pwd: 'db1owner', 
    roles: [ { role: "dbOwner", db: "db1" } ] });
    
db.createUser({ 
    user: 'db1', 
    pwd: 'db1', 
    roles: [ { role: "readWrite", db: "db1" } ] });