nosql | 搭建mongodb副本集

 前期准备

1.三台虚拟机进行环境配置之前查看IP状态,若为动态IP则使用nmtui命令都先将其IP改为静态IP如图所示以nosql01为例

2.将三台虚拟机的防火墙关闭

使用命令systemctl stop firewalld 关闭防火墙

使用命令firewall-cmd --state 查看防火墙的状态

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# firewall-cmd --state
not running

副本集的环境准备

1.克隆完已搭建mongodb的虚拟机之后,把data和logs文件夹都删除在创建新的两个同名文件夹并创建mongologs.log文件(目的是将原来的数据文件和日志文件清除)

2.在每一台hosts文件中的写入三台已经设置完静态的IP地址(vi /etc/hosts)

副本集的启动

1.启动三个节点的服务(注意:在没有添加环境变量的情况下需要在bin文件夹之下启动服务,添加了环境变量可以以user_mongo用户身份在任意位置启动但是每次切换到user_mongo用户身份的时候需要初始化环境变量)

以nosql01为例

[root@nosql01 ~]# su user_mongo

[user_mongo@nosql01 root]$ cd /opt/servers/mongodb_demo/mongodb/bin/

[user_mongo@nosql01 bin]$ mongod --replSet itcast    --dbpath=/opt/servers/mongodb_demo/replicaset/data/    --logpath=/opt/servers/mongodb_demo/replicaset/logs/mongodb.log    --port 27017 --bind_ip nosql01 --logappend --fork

about to fork child process, waiting until server is ready for connections.

forked process: 4226

child process started successfully, parent exiting

副本集的初始化

  1. 进入主节点的客户端
[user_mongo@localhost bin]$ ./mongo --host nosql01 --port 27017

MongoDB shell version v4.2.2

connecting to: mongodb://nosql01:27017/?compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("67d4e455-3c4e-431c-94af-daa5c7e37ffd") }

MongoDB server version: 4.2.2

Server has startup warnings:

2021-10-11T22:24:00.809+0800 I  CONTROL  [initandlisten]

2021-10-11T22:24:00.815+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten]

2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten]

2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'

2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten]

2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'

2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten]

---

Enable MongoDB's free cloud-based monitoring service, which will then receive and display

metrics about your deployment (disk utilization, CPU, operation statistics, etc).



The monitoring data will be available on a MongoDB website with a unique URL accessible to you

and anyone you share the URL with. MongoDB may use this information to make product

improvements and to suggest MongoDB products and deployment options to you.



To enable free monitoring, run the following command: db.enableFreeMonitoring()

To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

---



itcast:PRIMARY>

2.添加两个副本节点和投票 以nosql02为例

itcast:PRIMARY> rs.add("nosql02:27017")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1636005850, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

"keyId" : NumberLong(0)

}
},
"operationTime" : Timestamp(1636005850, 1)
}



rs.add({_id: 3, host: "node3:27017", priority: 0, vote: true})

3.在主节点的客户端查看三个节点的状态

itcast:PRIMARY> rs.status()

{

"set" : "itcast",

"date" : ISODate("2021-10-28T05:33:17.696Z"),

"myState" : 1,

"term" : NumberLong(4),

"syncingTo" : "",

"syncSourceHost" : "",

"syncSourceId" : -1,

"heartbeatIntervalMillis" : NumberLong(2000),

"majorityVoteCount" : 1,

"writeMajorityCount" : 1,

"optimes" : {

"lastCommittedOpTime" : {

"ts" : Timestamp(1635399189, 1),

"t" : NumberLong(4)

},

"lastCommittedWallTime" : ISODate("2021-10-28T05:33:09.852Z"),

"readConcernMajorityOpTime" : {

"ts" : Timestamp(1635399189, 1),

"t" : NumberLong(4)

},

"readConcernMajorityWallTime" : ISODate("2021-10-28T05:33:09.852Z"),

"appliedOpTime" : {

"ts" : Timestamp(1635399189, 1),

"t" : NumberLong(4)

},

"durableOpTime" : {

"ts" : Timestamp(1635399189, 1),

"t" : NumberLong(4)

},

"lastAppliedWallTime" : ISODate("2021-10-28T05:33:09.852Z"),

"lastDurableWallTime" : ISODate("2021-10-28T05:33:09.852Z")

},

"lastStableRecoveryTimestamp" : Timestamp(1635399169, 1),

"lastStableCheckpointTimestamp" : Timestamp(1635399169, 1),

"electionCandidateMetrics" : {

"lastElectionReason" : "electionTimeout",

"lastElectionDate" : ISODate("2021-10-11T14:24:13.122Z"),

"electionTerm" : NumberLong(4),

"lastCommittedOpTimeAtElection" : {

"ts" : Timestamp(0, 0),

"t" : NumberLong(-1)

},

"lastSeenOpTimeAtElection" : {

"ts" : Timestamp(1633961502, 1),

"t" : NumberLong(3)

},

"numVotesNeeded" : 1,

"priorityAtElection" : 1,

"electionTimeoutMillis" : NumberLong(10000),

"numCatchUpOps" : NumberLong(0),

"newTermStartDate" : ISODate("2021-10-11T14:24:13.755Z"),

"wMajorityWriteAvailabilityDate" : ISODate("2021-10-11T14:24:13.766Z")

},

"members" : [

{

"_id" : 0,

"name" : "nosql01:27017",

"ip" : "192.168.161.134",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 1436961,

"optime" : {

"ts" : Timestamp(1635399189, 1),

"t" : NumberLong(4)

},

"optimeDate" : ISODate("2021-10-28T05:33:09Z"),

"syncingTo" : "",

"syncSourceHost" : "",

"syncSourceId" : -1,

"infoMessage" : "",

"electionTime" : Timestamp(1633962253, 1),

"electionDate" : ISODate("2021-10-11T14:24:13Z"),

"configVersion" : 3,

"self" : true,

"lastHeartbeatMessage" : ""

},

{

"_id" : 1,

"name" : "nosql02:27017",

"ip" : "192.168.161.135",

"health" : 0,

"state" : 8,

"stateStr" : "(not reachable/healthy)",

"uptime" : 0,

"optime" : {

"ts" : Timestamp(0, 0),

"t" : NumberLong(-1)

},

"optimeDurable" : {

"ts" : Timestamp(0, 0),

"t" : NumberLong(-1)

},

"optimeDate" : ISODate("1970-01-01T00:00:00Z"),

"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),

"lastHeartbeat" : ISODate("2021-10-28T05:33:09.360Z"),

"lastHeartbeatRecv" : ISODate("2021-10-26T10:34:32.744Z"),

"pingMs" : NumberLong(0),

"lastHeartbeatMessage" : "Error connecting to nosql02:27017 (192.168.161.135:27017) :: caused by :: No route to host",

"syncingTo" : "",

"syncSourceHost" : "",

"syncSourceId" : -1,

"infoMessage" : "",

"configVersion" : -1

},

{

"_id" : 2,

"name" : "nosql03:27017",

"ip" : "192.168.161.136",

"health" : 0,

"state" : 8,

"stateStr" : "(not reachable/healthy)",

"uptime" : 0,

"optime" : {

"ts" : Timestamp(0, 0),

"t" : NumberLong(-1)

},

"optimeDurable" : {

"ts" : Timestamp(0, 0),

"t" : NumberLong(-1)

},

"optimeDate" : ISODate("1970-01-01T00:00:00Z"),

"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),

"lastHeartbeat" : ISODate("2021-10-28T05:33:12.368Z"),

"lastHeartbeatRecv" : ISODate("2021-10-11T14:31:31.766Z"),

"pingMs" : NumberLong(1),

"lastHeartbeatMessage" : "Error connecting to nosql03:27017 (192.168.161.136:27017) :: caused by :: No route to host",

"syncingTo" : "",

"syncSourceHost" : "",

"syncSourceId" : -1,

"infoMessage" : "",

"configVersion" : -1

}

],

"ok" : 1,

"$clusterTime" : {

"clusterTime" : Timestamp(1635399189, 1),

"signature" : {

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

"keyId" : NumberLong(0)

}

},

"operationTime" : Timestamp(1635399189, 1)

}

itcast:PRIMARY>

4.再进入两个节点的客户端,两个副节点出现secondary身份即配置成功

[user_mongo@nosql01 bin]$ ./mongo --host nosql02 --port 27017

MongoDB shell version v4.2.2

connecting to: mongodb://nosql02:27017/?compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("06e9f565-b481-49f1-88ca-9448c3077dea") }

MongoDB server version: 4.2.2

Server has startup warnings:

2021-10-11T22:25:00.195+0800 I  CONTROL  [initandlisten]

2021-10-11T22:25:00.195+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2021-10-11T22:25:00.195+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

2021-10-11T22:25:00.195+0800 I  CONTROL  [initandlisten]

2021-10-11T22:25:00.196+0800 I  CONTROL  [initandlisten]

2021-10-11T22:25:00.196+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

2021-10-11T22:25:00.196+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'

2021-10-11T22:25:00.196+0800 I  CONTROL  [initandlisten]

2021-10-11T22:25:00.197+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

2021-10-11T22:25:00.197+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'

2021-10-11T22:25:00.197+0800 I  CONTROL  [initandlisten]

---

Enable MongoDB's free cloud-based monitoring service, which will then receive and display

metrics about your deployment (disk utilization, CPU, operation statistics, etc).



The monitoring data will be available on a MongoDB website with a unique URL accessible to you

and anyone you share the URL with. MongoDB may use this information to make product

improvements and to suggest MongoDB products and deployment options to you.



To enable free monitoring, run the following command: db.enableFreeMonitoring()

To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

---



itcast:SECONDARY>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值