云网牛站
所在位置:首页 > Linux云数据库 > 在Ubuntu 18.04系统中安装MongoDB Replication的方法

在Ubuntu 18.04系统中安装MongoDB Replication的方法

2019-01-29 22:56:32作者:陈自坚稿源:云网牛站

本文介绍在Ubuntu 18.04操作系统中安装MongoDB Replication的方法。在MongoDB中,Replica sets是一组维护相同数据集的mongod进程,它是所有生产部署的基础,因为它们提供数据冗余和高可用性。

 

三节点IP地址

本文将介绍创建工作三节点replica set所需的步骤,而无需配置访问控制来限制访问,本文基于以下系统IP地址:

Node1: 10.10.5.2

Node2: 10.10.5.3

Node3: 10.10.5.4

 

一、设置要求

在配置replica set之前,必须在将成为replica set一部分的每个系统上安装MongoDB,为所有服务器设置正确的主机名,如果你有活动的DNS服务器,请为所有服务器添加A记录,或修改/etc/hosts文件,在所有节点上添加这些:

$ sudo vim /etc/hosts

10.10.5.2 mongodb-01 

10.10.5.3 mongodb-02 

10.10.5.4 mongodb-03

以下仅供参考,我的实验室设置基于KVM上运行的以下Vagrantfile:

# -*- mode: ruby -*-

# vim: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'

# Check required plugins

REQUIRED_PLUGINS_LIBVIRT = %w(vagrant-libvirt)

exit unless REQUIRED_PLUGINS_LIBVIRT.all? do |plugin|

Vagrant.has_plugin?(plugin) || (

puts "The #{plugin} plugin is required. Please install it with:"

puts "$ vagrant plugin install #{plugin}"

false

)

end

Vagrant.configure("2") do |config|

config.vm.define "mongodb-01" do |node|

node.vm.hostname = "mongodb-01"

node.vm.box = "generic/ubuntu1804"

node.vm.box_check_update = false

#node.vm.synced_folder '.', '/vagrant', :disabled => true

node.vm.network "private_network", ip: "10.10.5.2"

node.vm.provider :libvirt do |domain|

domain.memory = 512

domain.storage :file, :size => '10G'

end

end

config.vm.define "mongodb-02" do |node|

node.vm.hostname = "mongodb-02"

node.vm.box = "generic/ubuntu1804"

node.vm.box_check_update = false

#node.vm.synced_folder '.', '/vagrant', :disabled => true

node.vm.network "private_network", ip: "10.10.5.3"

node.vm.provider :libvirt do |domain|

domain.memory = 512

domain.storage :file, :size => '10G'

end

end

config.vm.define "mongodb-03" do |node|

node.vm.hostname = "mongodb-03"

node.vm.box = "generic/ubuntu1804"

node.vm.box_check_update = false

#node.vm.synced_folder '.', '/vagrant', :disabled => true

node.vm.network "private_network", ip: "10.10.5.4"

node.vm.provider :libvirt do |domain|

domain.memory = 512

domain.storage :file, :size => '10G'

end

end

end

 

二、在所有Ubuntu 18.04节点上安装MongoDB

如果还没有安装MongoDB,请参考在Ubuntu 18.04/16.04系统中安装MongoDB的方法

更新系统:

sudo apt-get update

sudo apt-get upgrade

导入MongoDB公共GPG密钥:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

导入密钥后,添加存储库:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

对于我的设置,我有专门用于MongoDB数据的10GB辅助存储,我将它分区并挂载到/data/mongodb:

在Ubuntu 18.04系统中安装MongoDB Replication的方法

为辅助磁盘创建GPT分区表,它可以是多个磁盘:

parted -s -a optimal -- /dev/vda mklabel gpt

parted -s -a optimal -- /dev/vda mkpart primary 0% 100%

parted -s -- /dev/vda align-check optimal 1

然后创建LVM卷,这将使扩展分区变得容易:

# pvcreate  /dev/vda1

Physical volume "/dev/vda1" successfully created.

# vgcreate vg11 /dev/vda1

Volume group "vg11" successfully created

# lvcreate -n data -l 100%FREE vg11

Logical volume "data" created

在创建的逻辑卷上创建ext4文件系统:

# mkfs.ext4 /dev/mapper/vg11-data

mke2fs 1.44.1

Creating filesystem with 2620416 4k blocks and 655360 inodes

Filesystem UUID: b98e07e5-1b04-4282-a9db-fa5b73c74d2f

Superblock backups stored on blocks: 

32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done

Writing inode tables: done

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

创建挂载点并挂载分区:

echo "/dev/mapper/vg11-data /data ext4 defaults 0 0" >> /etc/fstab

mkdir /data

mount -a

确认分区装载成功:

# df -hT | grep /data

/dev/mapper/vg11-data ext4      9.8G   37M  9.3G   1% /data

安装MongoDB包:

sudo apt-get update

sudo apt-get install -y openssl libcurl3 mongodb-org

为MongoDB数据创建一个文件夹:

mkdir /data/mongodb

chown -R mongodb:mongodb /data/mongodb

chmod -R 775 /data/mongodb

 

三、配置MongoDB Replica set

现在我们已经准备好了所有的东西,让我们继续配置MongoDB Replica set。

在所有节点上从localhost IP更改MongoDB侦听地址:

运行:sudo vim /etc/mongod.conf

# node 1

# network interfaces

net:

port: 27017

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

# node 2

# network interfaces

net:

port: 27017

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

# node 3

# network interfaces

net:

port: 27017

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

将存储路径配置为先前在所有节点上创建的存储路径:

# Where and how to store data.

storage:

dbPath: /data/mongodb

journal:

enabled: true

1、配置MongoDB Replica Set

其中一个MongoDB节点作为PRIMARY运行,所有其他节点将作为SECONDARY运行,数据始终是PRIMARY节点,然后数据集将复制到所有其他SECONDARY节点。

编辑MongoDB配置文件mongod.conf并在所有节点上启用Replica Set:

replication:

replSetName: "replica01"

在防火墙上打开端口27017/tcp:

sudo ufw enable

sudo ufw allow ssh

sudo ufw allow 27017/tcp

将MongoDB配置为在操作系统引导期间启动:

sudo systemctl enable mongod.service

sudo systemctl restart mongod.service

检查MongoDB服务的监听地址:

# ss -tunelp | grep -i mongo

tcp LISTEN 0 128 10.10.5.2:27017  0.0.0.0:*  users:(("mongod",pid=15288,fd=11)) uid:111 ino:46927 sk:4 <->

2、启动MongoDB Replica Set

我们的MongoDB Node1(mongodb-01)将是主要的,另外两个将充当SECONDARY。

登录到mongodb-01服务器并启动mongo shell:

$ mongo 10.10.5.2

MongoDB shell version v4.0.1

connecting to: mongodb://10.10.5.2:27017/test

MongoDB server version: 4.0.1

Welcome to the MongoDB shell.

For interactive help, type "help".

...

>

通过运行以下命令在node1上初始化replica set:

> rs.initiate()

{

"info2" : "no configuration specified. Using a default configuration for the set",

"me" : "10.10.5.2:27017",

"ok" : 1,

"operationTime" : Timestamp(1534797235, 1),

"$clusterTime" : {

"clusterTime" : Timestamp(1534797235, 1),

"signature" : {

"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

"keyId" : NumberLong(0)

}

}

}

确保你的状态OK为1。

3、添加辅助节点

replica01:PRIMARY> rs.add("mongodb-02")

{

"ok" : 1,

"operationTime" : Timestamp(1534797580, 1),

"$clusterTime" : {

"clusterTime" : Timestamp(1534797580, 1),

"signature" : {

"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

"keyId" : NumberLong(0)

}

}

}

replica01:PRIMARY> rs.add("mongodb-03")

{

"ok" : 1,

"operationTime" : Timestamp(1534797614, 1),

"$clusterTime" : {

"clusterTime" : Timestamp(1534797614, 1),

"signature" : {

"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

"keyId" : NumberLong(0)

}

}

}

使用以下方法检查replica set状态:

replica01:PRIMARY> rs.status()

检查主站的状态:

replica01:PRIMARY> rs.isMaster()

4、测试MongoDB Replication

在mongodb-01上创建一个测试数据库:

replica01:PRIMARY> use test_db

switched to db test_db

添加一些数据:

> db.test.save(

{

"desc": "My Test Database",

"apps":  ["Test1", "Test2", "Test3", "Test4"],

})

replica01:PRIMARY> show dbs

admin    0.000GB

config   0.000GB

local    0.000GB

test_db  0.000GB

replica01:PRIMARY> use test_db

switched to db test_db

replica01:PRIMARY> show collections

test

 

结语

你现在有一个有效的MongoDB replication,你可以将群集大小从三个节点增加到五个节点,七个节点或更多节点,该数字来自: [Master Eligible Nodes) / 2 + 1],这意味着高可用性需要奇数个节点,最小数量为3。

 

相关主题

Ubuntu 16.04 MongoDB副本集配置和数据迁移实战

精选文章
热门文章