阿汤博客-承接中小企业服务器维护和网站维护,有意者可以联系博主!

MongoDB安装副本集配置

运维维护 62℃ 0评论

一、yum安装

1、到阿里云源下载最新rpm安装包到服务器安装

#wget https://mirrors.aliyun.com/mongodb/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-server-4.0.1-1.el7.x86_64.rpm

#rpm -ivh mongodb-org-*.rpm

#systemctl start mongod #启动mongod服务

#systemctl enable mongod  #设置开机启动

相关文件路径

MongoDB安装副本集配置

 

二、二进制安装

1、下载二进制安装包解压安装

#wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.1.tgz

#tar -zxvf mongodb-linux-x86_64-rhel70-4.0.1.tgz

#mv mongodb-linux-x86_64-rhel70-4.0.1 /usr/local/mongodb

#cd /usr/local/mongodb

#mkdir -pv {conf,log,data/db1}  #创建配置文件、日志、数据目录

# echo "export PATH=$PATH:/usr/local/mongodb/bin" > /etc/profile.d/mongodb.sh #设置环境变量

#source /etc/profile #让环境变量生效

#useradd mongod  -s /bin/false #新建MongoDB运行账户

#chown -R mongod:mongod /usr/local/mongodb/ #更改mongod目录用户和属组

 

2、新建配置文件mongodb.conf

#vim /usr/local/mongodb/conf/mongodb.conf

 

# mongod.conf

 

# for documentation of all options, see:

#   http://docs.mongodb.org/manual/reference/configuration-options/

 

# where to write logging data.

systemLog:

  destination: file

  logAppend: true

  path: /usr/local/mongodb/log/mongod-log-1.log

 

# Where and how to store data.

storage:

  dbPath: /usr/local/mongodb/data/db1

  journal:

    enabled: true

#  engine:

#  mmapv1:

#  wiredTiger:

 

# how the process runs

processManagement:

  fork: true  # fork and run in background

  pidFilePath: /usr/local/mongodb/mongod.pid  # location of pidfile

  timeZoneInfo: /usr/share/zoneinfo

 

# network interfaces

net:

  port: 27017

  bindIp: master  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

 

 

#security:

 

#operationProfiling:

 

#replication:

 

#sharding:

 

## Enterprise-Only Options

 

#auditLog:

 

#snmp:

 

3、新建service服务文件

#vim /usr/lib/systemd/system/mongod.service

[Unit]

Description=MongoDB Database Server

After=network.target

Documentation=https://docs.mongodb.org/manual

 

[Service]

User=mongod

Group=mongod

Environment="OPTIONS=-f /usr/local/mongodb/conf/mongodb.conf"

ExecStart=/usr/local/mongodb/bin/mongod $OPTIONS

PermissionsStartOnly=true

PIDFile=/usr/local/mongodb/mongod.pid

Type=forking

# 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 for mongod as specified in

# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

 

[Install]

WantedBy=multi-user.target

 

4、设置内核参数

#echo 0 > /proc/sys/vm/zone_reclaim_mode

#sysctl -w vm.zone_reclaim_mode=0    #永久设置

#echo never > /sys/kernel/mm/transparent_hugepage/enabled

#echo never > /sys/kernel/mm/transparent_hugepage/defrag

 

5、启动测试

#systemctl start mongod  #启动服务

#systemctl enable mongod #设置开机启动

 

三、副本集配置

Master 192.168.133.128

Node1 192.168.133.129

Node2 192.168.133.130


1、三节点分别设置hosts

#cat <<EOF >> /etc/hosts

192.168.133.128 master

192.168.133.129 node1

192.168.133.130 node2

EOF


2、编辑配置文件

#Vim /etc/mongod.conf #所有节点配置文件添加如下两行

replication:

  replSetName: rs1

#systemctl restart mongod  #重启服务让配置生效


3、连接master节点MongoDB

# mongo –host=master

>use admin

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

>rs.conf()     #确认设定

>rs.add(node1:27017)  #添加node1rs1副本集

>rs.add({host:node2:27017,priority:5})  #添加node2rs1副本集,并设置priority优先级为5

>rs.conf()   #确认设定

>rs.status()   #查看状态

 

设置priority优先级

>var config = rs.config()

>config.members[0].priority=6

>rs.reconfig(config)  //重新更新配置

MongoDB安装副本集配置

删除节点

>rs.remove(node1:27017)


4、数据同步测试

a. 在主库插入数据

rs1:PRIMARY> db.user.insert({"姓名":"张三","年龄":20,"性别":""})

rs1:PRIMARY> db.user.find()

{ "_id" : ObjectId("5b7bca2a40d8bd576c4e3c54"), "姓名" : "张三", "年龄" : 20, "性别" : "" }

b.登陆从节点查询数据

# mongo –host=node1

rs1:SECONDARY>use admin

rs1:SECONDARY>rs.slaveOk()  #开启从库读取

rs1:SECONDARY> db.user.find()

{ "_id" : ObjectId("5b7bca2a40d8bd576c4e3c54"), "姓名" : "张三", "年龄" : 20, "性别" : "" }


5、故障转移测试

a.关闭master MongoDB服务

# systemctl stop mongod

b.登陆任意从节点数据库,查看状态

# mongo –host=node2

>use admin

>rs.status()

MongoDB安装副本集配置

MongoDB安装副本集配置

可以看到priority优先级为5的节点已经变成了主节点。

c.在新的主节点添加数据。

rs1:PRIMARY> db.user2.insert({"姓名":"李四","年龄":25,"性别":""})

WriteResult({ "nInserted" : 1 })

rs1:PRIMARY> db.user2.find()

{ "_id" : ObjectId("5b7bd44c6129560c71cc22e0"), "姓名" : "李四", "年龄" : 25, "性别" : "" }

d.启动故障节点的MongoD服务

#systemctl start mongod

#mongo –host=master

> use admin

>rs.stauts()

MongoDB安装副本集配置

可以看到故障服务器上线以后,又变成了主库。

> db.user2.find()  #查询刚才添加的数据

{ "_id" : ObjectId("5b7bd44c6129560c71cc22e0"), "姓名" : "李四", "年龄" : 25, "性别" : "" }

转载请注明:阿汤博客 » MongoDB安装副本集配置

喜欢 (0)or分享 (0)