准备
1、JDK8
2、MongoDB安装包
3、在华为云上申请三个ECS节点,内外IP分别是192.168.0.19、192.168.0.6、192.168.0.37
image.png
1、安装JDK
分别在三台机器上安装JDK
在/home目录下面创建mongodb文件夹,并把jdk和mongodb的安装包上传到文件夹下面。解压jdk,然后在/etc/profile文件最后添加环境变量
tar -xzvf jdk-8u281-linux-x64.tar.gz
vi /etc/profile
文件内容:
export JAVA_HOME=/home/mongodb/jdk1.8.0_281
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
检查jdk是否安装成功
source /etc/profile
java -version
安装成功显示如下
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)
2、安装mongodb集群
在/home/mongodb下面解压mongodb压缩包,添加环境变量
tar -xzvf mongodb-linux-x86_64-3.6.22.tgz
echo 'export PATH=$PATH:/home/mongodb/mongodb-linux-x86_64-3.6.22/bin' >> /etc/profile
source /etc/profile
创建data目录和log目录,添加配置文件
mkdir data
mkdir log
vi mongodb.conf
配置文件信息
dbpath=/home/mongodb/data/
logpath=/home/mongodb/log/mongodb.log
pidfilepath=/home/mongodb/mongodb.pid
directoryperdb=true
logappend=true
replSet=mymongodb
bind_ip=0.0.0.0
port=27017
oplogSize=10000
fork=false
参数解释:
dbpath:数据存放目录
logpath:日志存放路径
pidfilepath:进程文件,方便停止mongodb
directoryperdb:为每一个数据库按照数据库名建立文件夹存放
logappend:以追加的方式记录日志
replSet:replica set的名字
bind_ip:mongodb所绑定的ip地址
port:mongodb进程所使用的端口号,默认为27017
oplogSize:mongodb操作日志文件的最大大小。单位为Mb,默认为硬盘剩余空间的5%
fork:以后台方式运行进程
noprealloc:不预先分配存储
启动mongodb,分别在三个节点上执行下面命令
mongod -f /home/mongodb/mongodb.conf
启动成功信息如下所示
about to fork child process, waiting until server is ready for connections.
forked process: 1659
child process started successfully, parent exiting
连接数据库
mongo --host 192.168.0.19 --port 27017
切换数据库:
use admin
初始化复制集:
rs.initiate({_id:"mymongodb",members:[{_id:0,host:"192.168.0.19:27017"},{_id:1,host:"192.168.0.6:27017"}, {_id:2,host:"192.168.0.37:27017"}]})
其中_id:"mymongodb"的mymongodb是上面config.conf配置文件里的复制集名称,把三台服务器的配置服务组成复制集。
过十多秒后查看状态:
rs.status()
执行上面的命令查看状态,三台机器的配置服务就已形成复制集,其中1台为PRIMARY,其他2台为SECONDARY
{
"set" : "mymongodb",
"date" : ISODate("2021-03-18T17:07:19.075Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1616087239, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1616087239, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1616087239, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1616087239, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.0.19:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 43,
"optime" : {
"ts" : Timestamp(1616087229, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1616087229, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-18T17:07:09Z"),
"optimeDurableDate" : ISODate("2021-03-18T17:07:09Z"),
"lastHeartbeat" : ISODate("2021-03-18T17:07:17.254Z"),
"lastHeartbeatRecv" : ISODate("2021-03-18T17:07:18.126Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.0.37:27017",
"syncSourceHost" : "192.168.0.37:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 1,
"name" : "192.168.0.6:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 43,
"optime" : {
"ts" : Timestamp(1616087229, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1616087229, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-18T17:07:09Z"),
"optimeDurableDate" : ISODate("2021-03-18T17:07:09Z"),
"lastHeartbeat" : ISODate("2021-03-18T17:07:17.253Z"),
"lastHeartbeatRecv" : ISODate("2021-03-18T17:07:18.108Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.0.37:27017",
"syncSourceHost" : "192.168.0.37:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.0.37:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 352,
"optime" : {
"ts" : Timestamp(1616087239, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-18T17:07:19Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1616087207, 1),
"electionDate" : ISODate("2021-03-18T17:06:47Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"operationTime" : Timestamp(1616087239, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1616087239, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
网友评论