mongodb3.6.8集群搭建

MongoDB3.6新增:

  • 配置文件采用yaml方式来配置
  • 生产中取消了仲裁者的角色,因为仲裁者也不会存储数据,只是起到选举的作用,线上为了保证数据安全,每份数据都会配置两个副本集,也就是每份数据存储了三份。
  • 优化配置,采用五台集群
  • 使用非root账户搭建mongodb集群。

环境准备

系统系统 centos7.5 

主机
hostname:node1 192.168.231.132
hostname:node2 192.168.231.133
hostname:node3 192.168.231.134

 

 

 

 

安装包:mongodb-linux-x86_64-rhel70-3.6.8.tgz

服务器规划

服务器132 服务器133 服务器134
mongos server mongos server mongos server
config server config server config server
shard1 server shard1 server shard1 server
shard2 server shard2 server shard2 server
shard3 server shard3 server shard3 server

端口分配:

mongos:20000
config:21000
shard1:27001
shard2:27002
shard3:27003

权限分配:【这里也可以用root用户】

登录root账户,将安装目录和数据目录权限分配给日常操作(mongoUser)账户

chown -R mongoUser:mongoUser /usr/local/
chown -R mongoUser:mongoUser /data

 

mongodb安装

 

1、下载

下载 mongodb 3.4.6 安装包

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.8.tgz
#解压
tar -xzvf mongodb-linux-x86_64-rhel70-3.6.8.tgz -C /usr/local/
#改名
mv mongodb-linux-x86_64-rhel70-3.6.8 mongodb

 

2、创建相关目录

根据服务器的规范,分别在对应的服务器上建立conf、mongos、config、shard1、shard2、shard3、shard4、shard5等目录,因为mongos不存储数据,只需要建立日志文件目录即可。

mkdir -p /usr/local/mongodb/conf
mkdir -p /data/mongos/log
mkdir -p /data/config/data
mkdir -p /data/config/log
mkdir -p /data/shard1/data
mkdir -p /data/shard1/log
mkdir -p /data/shard2/data
mkdir -p /data/shard2/log
mkdir -p /data/shard3/data
mkdir -p /data/shard3/log

 

3、环境变量

为了后续方便操作,配置mongodb的环境变量,需要切到root用户下面

vim /etc/profile
# 内容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
# 使立即生效,在安装用户下执行
source /etc/profile

查看mongodb版本信息mongod -v 输出版本信息表明配置环境变量成功

 

集群配置

 

1、config server配置服务器

在服务器1.2.3上配置以下内容:

添加配置文件:

vi /usr/local/mongodb/conf/config.conf

## content
systemLog:
  destination: file
  logAppend: true
  path: /data/config/log/config.log
 
# Where and how to store data.
storage:
  dbPath: /data/config/data
  journal:
    enabled: true
# how the process runs
processManagement:
  fork: true
  pidFilePath: /data/config/log/configsrv.pid
 
# network interfaces
net:
  port: 21000
  bindIp: 192.168.231.132
 
#operationProfiling:
replication:
    replSetName: config        

sharding:
    clusterRole: configsvr

启动三台服务器的config server

yum install -y numactl
numactl --interleave=all mongod --config /usr/local/mongodb/conf/config.conf

登录任意一台配置服务器,初始化配置副本集

#连接
mongo 192.168.231.132:21000
#config变量
config = {
    _id : "config",
 	members : [
     	{_id : 0, host : "192.168.231.132:21000" },
    	{_id : 1, host : "192.168.231.133:21000" },
     	{_id : 2, host : "192.168.231.134:21000" }
 	]
}

#初始化副本集
rs.initiate(config)

#查看分区状态
rs.status();

其中,"_id" : "configs"应与配置文件中配置的 replicaction.replSetName 一致,"members" 中的 "host" 为三个节点的ip和port

这样配置服务器就配置好了

2、配置分片、副本集

配置第一个分片副本集

在服务器 1.2.3上面做以下配置

配置文件

vi /usr/local/mongodb/conf/shard1.conf

#配置文件内容
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/shard1/log/shard1.log
 
# Where and how to store data.
storage:
  dbPath: /data/shard1/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20

# how the process runs
processManagement:
  fork: true 
  pidFilePath: /data/shard1/log/shard1.pid
 
# network interfaces
net:
  port: 27001
  bindIp: 192.168.231.132

#operationProfiling:
replication:
    replSetName: shard1
sharding:
    clusterRole: shardsvr

启动三台服务器的shard1 server

numactl --interleave=all mongod  --config  /usr/local/mongodb/conf/shard1.conf

登陆任意一台服务器,初始化副本集

mongo 192.168.231.132:27001
#使用admin数据库
use admin
#定义副本集配置
config = {
    _id : "shard1",
     members : [
         {_id : 0, host : "192.168.231.132:27001" },
         {_id : 1, host : "192.168.231.133:27001" },
         {_id : 2, host : "192.168.231.134:27001" }
     ]
 }


#初始化副本集配置
rs.initiate(config);


#查看分区状态
rs.status();

 

配置第二个分片副本集

在服务器1.2.3上面做以下配置

配置文件

vi /usr/local/mongodb/conf/shard2.conf

#配置文件内容
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/shard2/log/shard2.log
 
# Where and how to store data.
storage:
  dbPath: /data/shard2/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20
 
# how the process runs
processManagement:
  fork: true 
  pidFilePath: /data/shard2/log/shard2.pid
 
# network interfaces
net:
  port: 27002
  bindIp: 192.168.231.132

 
#operationProfiling:
replication:
    replSetName: shard2
sharding:
    clusterRole: shardsvr

启动三台服务器的shard2 server

numactl --interleave=all mongod  --config /usr/local/mongodb/conf/shard2.conf

登陆任意一台服务器,初始化副本集

mongo 192.168.231.132:27002
#使用admin数据库
use admin
#定义副本集配置
config = {
	_id : "shard2",
 	members : [
     	{_id : 0, host : "192.168.231.132:27002"},
     	{_id : 1, host : "192.168.231.133:27002"},
     	{_id : 2, host : "192.168.231.134:27002"}
 	]	
}


#初始化副本集配置
rs.initiate(config);

#查看分区状态
rs.status();

配置第三个分片副本集

在服务器1.2.3上面做以下配置

配置文件

vi /usr/local/mongodb/conf/shard3.conf

#配置文件内容
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/shard3/log/shard3.log
 
# Where and how to store data.
storage:
  dbPath: /data/shard3/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20
# how the process runs
processManagement:
  fork: true 
  pidFilePath: /data/shard3/log/shard3.pid
 
# network interfaces
net:
  port: 27003
  bindIp: 192.168.231.132

 
#operationProfiling:
replication:
    replSetName: shard3
sharding:
    clusterRole: shardsvr

启动三台服务器的shard3 server

numactl --interleave=all mongod  --config  /usr/local/mongodb/conf/shard3.conf

登陆任意一台服务器,初始化副本集

mongo 192.168.231.132:27003
#使用admin数据库
use admin
#定义副本集配置
config = {
    _id : "shard3",
     members : [
         {_id : 0, host : "192.168.231.132:27003"},
         {_id : 1, host : "192.168.231.133:27003"},
         {_id : 2, host : "192.168.231.134:27003"}
     ]
 }


#初始化副本集配置
rs.initiate(config);

#查看分区状态
rs.status();

 

3、配置路由服务器 mongos

以下配置在服务器1.2.3上执行

注意:先启动配置服务器和分片服务器,后启动路由实例

vi /usr/local/mongodb/conf/mongos.conf

systemLog:
  destination: file
  logAppend: true
  path: /data/mongos/log/mongos.log
processManagement:
  fork: true
#  pidFilePath: /usr/local/mongodb/mongos.pid
 
# network interfaces
net:
  port: 20000
  bindIp: 192.168.231.132
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
sharding:
   configDB: config/192.168.231.132:21000,192.168.231.133:21000,192.168.231.134:21000

启动二台服务器的mongos server

mongos  --config  /usr/local/mongodb/conf/mongos.conf

 

4、启用分片

目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。

登陆任意一台mongos

mongo 192.168.231.132:20000
#使用admin数据库
use  admin
#串联路由服务器与分配副本集
sh.addShard("shard1/192.168.231.132:27001,192.168.231.133:27001,192.168.231.134:27001")
sh.addShard("shard2/192.168.231.132:27002,192.168.231.133:27002,192.168.231.134:27002")
sh.addShard("shard3/192.168.231.132:27003,192.168.231.133:27003,192.168.231.134:27003")
#查看集群状态
sh.status()

这样mongodb的三台集群搭建就已经完成了。

5、启动顺序

numactl --interleave=all mongod --config /usr/local/mongodb/conf/config.conf
numactl --interleave=all mongod  --config  /usr/local/mongodb/conf/shard1.conf
numactl --interleave=all mongod  --config  /usr/local/mongodb/conf/shard2.conf
numactl --interleave=all mongod  --config  /usr/local/mongodb/conf/shard3.conf
mongos  --config  /usr/local/mongodb/conf/mongos.conf

6、测试

目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。
 

mongo 192.168.231.132:20000
#设置分片chunk大小
use config
db.settings.save({ "_id" : "chunksize", "value" : 1 })
#设置1M是为了测试,否则要插入大量数据才能分片。
#指定test分片生效
sh.enableSharding("test")
#指定数据库里需要分片的集合和片键
use test
db.users.createIndex({user_id : 1})

use admin
sh.shardCollection("test.users", {user_id: 1})
#我们设置testdb的 table1 表需要分片,根据 user_id 自动分片到 shard1 ,shard2,shard3 上面去。要#这样设置是因为不是所有mongodb 的数据库和表 都需要分片!
#测试分片配置结果
mongo 192.168.231.132:20000

use test;

for (var i = 1; i <=1000000; i++){
db.users.save({user_id: i, username: "user"+i});
}

#接下来查看表状态就可以了

 

你可能感兴趣的:(数据库)