MongoDB副本集replica set (二)--副本集环境搭建

(一)主机信息

操作系统版本:centos7 64-bit

数据库版本   :MongoDB 4.2 社区版

ip hostname
192.168.10.41 mongoserver1
192.168.10.42 mongoserver2
192.168.10.43 mongoserver3


(二)副本集搭建过程

首先需要在3台服务器上安装MongoDB软件,安装过程见:https://www.cnblogs.com/lijiaman/p/12983589.html。安装完成之后,即可进行后续的配置,具体操作如下:

(1)在一台机器上创建keyfile

openssl rand -base64 756 > /mongo/mongo-keyfile
chmod 400 /mongo/mongo-keyfile


(2)拷贝feyfile到所有节点

scp /mongo/mongo-keyfile root@192.168.10.42:/mongo/
scp /mongo/mongo-keyfile root@192.168.10.43:/mongo/


(3)以启用身份验证的方式开启所有节点
这里将所有参数设置到配置文件里面,方便管理,配置文件如下:

[root@mongodbserver1 mongo]# cat /etc/mongod.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: /mongo/mongod.log

# Where and how to store data.
storage:
dbPath: /mongo/data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:

# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /mongo/mongod.pid # location of pidfile

# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.

security:
authorization: enabled                 # 启用身份验证
keyFile: /mongo/mongo-keyfile          # 配置keyfile文件
 
replication:
replSetName: rstest                    # 设置副本集名称

然后启动所有节点,以节点1为例:

[root@mongodbserver1 mongo]# mongod -f /etc/mongod.conf


(4)初始化副本集
在其中一个节点执行以下脚本初始化副本集,只需在一个节点上执行即可。

rs.initiate(
{
_id : "rstest",
members: [
{ _id : 0, host : "192.168.10.41:27017" },
{ _id : 1, host : "192.168.10.42:27017" },
{ _id : 2, host : "192.168.10.43:27017" }
]
}
)

参数含义:
_id          :副本集的名称
members :副本集的成员信息


在初始化时,会触发投票选举一个主节点,可以使用rs.status()来确定主节点成员

rstest:SECONDARY> rs.status()
...
"members" : [
{
"_id" : 0,
"name" : "192.168.10.41:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 280,
"optime" : {
"ts" : Timestamp(1592897767, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-06-23T07:36:07Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1592897607, 1),
"electionDate" : ISODate("2020-06-23T07:33:27Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
...


(5)创建管理员用户
第一个用户必须要有创建其它用户的权限,例如需要有userAdminAnyDatabase权限,并且需要创建在admin数据库中。
因为是在副本集上创建用户,故要在主节点上执行。如创建root用户

use admin;

db.createUser(
{
user:"root",
pwd:"123456",
roles:[{role:"userAdminAnyDatabase",db:"admin"}]
}
)


(6)以管理员身份登录数据库
通过以下方式以管理员身份登录到数据库

mongo -u root -p 123456 --authenticationDatabase admin

(7)创建一个集群管理员账户
clusterAdmin角色被授予副本集操作的权限,如配置副本集。在admin数据库中创建一个集群管理员并授予clusterAdmin角色。

use admin

db.createUser(
{
"user" : "replica",
"pwd" : "replica",
roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
}
)


(8)要启用身份验证,需要重启数据库
重启完成后,就需要以用户密码方式登录数据库了,假如不使用用户名密码,可以登录数据库,但是无法访问数据

[root@mongodbserver2 mongo]# mongo
MongoDB shell version v4.2.7
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d49b410b-a7af-4550-a455-faa82885517b") }
MongoDB server version: 4.2.7
rstest:PRIMARY> show dbs
rstest:PRIMARY> 
rstest:PRIMARY> db
test

只有使用了用户名密码,才能查到数据:

[root@mongodbserver2 mongo]# mongo -u root -p 123456 --authenticationDatabase admin 
MongoDB shell version v4.2.7
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a1f0da48-1266-4766-a9e4-32b97a46c3ec") }
MongoDB server version: 4.2.7
rstest:PRIMARY> 
rstest:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB



【完】

posted @ 2020-06-23 20:39  gegeman  阅读(778)  评论(0编辑  收藏  举报