(这是读大三时的一篇过程文档 发在博客上留个念想) 

前期准备

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>

  1. 添加两个副本节点和投票 以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>

出现的问题:

  1. 在bin文件夹里用启user_mongo用户启动服务(如果配置了环境变量就可以在任意位置启动服务)
  2. 在启动副本集的服务的时候主机名不要写错(因为虚拟机之间是可以互相启动的)
  3. nmtui 更改IP地址的状态(可能会出现无法建立以太网连接的问题,解决方法是在虚拟网络编辑器里添加网络
  4. 可能会出现报错提示权限不足的问题,一种方法是用su 切换身份,另一种方法是在图形界面选择文件右键属性更改权限
  5. 解决防火墙的问题:

暂时关闭防火墙命令:systemctl stop firewalld

永久关闭防火墙:systemctl disable firewalld

查看防火墙状态:

firewall-cmd --state(简略);systemctl status firewalld(详细)

  1. 投票命令:

rs.add({_id:3,host:"node3:27017",priority:1,vote:1})(要改序号)

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐