如何安装mongodb,主从,副本集

1,准备虚拟机环境

下载mongodb

cd /opt
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.13.tgz

安装依赖包

yum install -y libcurl openssl

创建所需的目录以及解压到安装目录(因为安装包内未提供配置文件)

mkdir -p /opt/mongo/ 

tar xvf mongodb-linux-x86_64-3.6.13.tgz -C /opt/mongo/

ln -s /opt/mongo/mongodb-linux-x86_64-3.6.13 /opt/mongo/mongodb

mkdir -p /opt/mongo/mongo_27017/{conf,logs,pid} 

编辑配置文件

vim /opt/mongo/mongo_27017/conf/mongodb.conf
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongo/mongo_27017/logs/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /opt/mongo/mongo_27017
  directoryPerDB: true
  wiredTiger:
     engineConfig:
        cacheSizeGB: 1
        directoryForIndexes: true
     collectionConfig:
         blockCompressor: zlib
     indexConfig:
        prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/mongo/mongo_27017/pid/mongod.pid

net:
  port: 27017
  bindIp: 0.0.0.0

将mongodb加入systemd

vim /usr/lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target

[Service]
Type=forking
ExecStart=/opt/mongo/mongodb/bin/mongod --config /opt/mongo/mongo_27017/conf/mongodb.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/opt/mongo/mongodb/bin/mongod --config /opt/mongo/mongo_27017/conf/mongodb.conf --shutdown
PrivateTmp=true

[vInstall]
WantedBy=multi-user.target

写完配置重载一下

systemctl daemon-reload

添加环境变量 以便全局使用

echo "PATH=\$PATH:/opt/mongo/mongodb/bin" >> /etc/profile

不要忘记刷新一下

source /etc/profile 

最后启动即可

systemctl start mongodb
systemctl enable mongodb

如何搭建主从

在上面的基础上 在启动时换成(这是主库的,直接回车就好)

/opt/mongo/mongodb/bin/mongod -f /opt/mongo/mongo_27017/conf/mongodb.conf --master

由于mongodb的特殊性,从节点必须指定源头,命令是(换成主库的id就行)

/opt/mongo/mongodb/bin/mongod -f /opt/mongo/mongo_27017/conf/mongodb.conf --slave --source masterIP

在从库执行

mongo
>rs.slaveOk();  (开启可读)

最后在主库插入几条数据,你来从库查询,就可以看见。

如何搭建Mongodb的副本集

修改一下本机的/etc/hosts文件(mongodb通常根据主机名进行寻找,如果未修改会在本机形成死循环)
你有多少台,就写多少条格式如下

vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.100 mongodb1
192.168.10.101 mongodb2
192.168.10.102 mongodb3

修改配置文件即可

主节点配置
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongo/mongo_27017/logs/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /opt/mongo/mongo_27017
  directoryPerDB: true
  wiredTiger:
     engineConfig:
        cacheSizeGB: 1
        directoryForIndexes: true
     collectionConfig:
         blockCompressor: zlib
     indexConfig:
        prefixCompression: true
replication:
  replSetName: "myapp"
  oplogSizeMB: 1024
processManagement:
  fork: true
  pidFilePath: /opt/mongo/mongo_27017/pid/mongod.pid

net:
  port: 27017
  bindIp: 0.0.0.0


从节点1

systemLog:
  destination: file
  logAppend: true
  path: /opt/mongo/mongo_27017/logs/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /opt/mongo/mongo_27017
  directoryPerDB: true
  wiredTiger:
     engineConfig:
        cacheSizeGB: 1
        directoryForIndexes: true
     collectionConfig:
         blockCompressor: zlib
     indexConfig:
        prefixCompression: true
replication:
  replSetName: "myapp"
  oplogSizeMB: 1024
processManagement:
  fork: true
  pidFilePath: /opt/mongo/mongo_27017/pid/mongod.pid

net:
  port: 27017
  bindIp: 0.0.0.0

从节点2

systemLog:
  destination: file
  logAppend: true
  path: /opt/mongo/mongo_27017/logs/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /opt/mongo/mongo_27017
  directoryPerDB: true
  wiredTiger:
     engineConfig:
        cacheSizeGB: 1
        directoryForIndexes: true
     collectionConfig:
         blockCompressor: zlib
     indexConfig:
        prefixCompression: true
replication:
  replSetName: "myapp"
  oplogSizeMB: 1024
processManagement:
  fork: true
  pidFilePath: /opt/mongo/mongo_27017/pid/mongod.pid

net:
  port: 27017
  bindIp: 0.0.0.0

接下来在三台主机启动

systemctl start mongodb.service

启动完成后选择一台作为主节点(默认第一台)

mongod -port 27017
config = {      _id: "myapp",     members: [     
     {_id: 0, host: "192.168.10.100:27017", "priority": 3},
     {_id: 1, host: "192.168.10.204:27017", "priority": 2},
     {_id: 2, host: "192.168.10.102:27017", "priority": 1}     ]  }

看见如下提示就是成功了

{
“_id” : “myapp”,
“members” : [
{
“_id” : 0,
“host” : “192.168.10.100:27017”,
“priority” : 3
},
{
“_id” : 1,
“host” : “192.168.10.204:27018”,
“priority” : 2
},
{
“_id” : 2,
“host” : “192.168.10.102:27019”,
“priority” : 1
}
] }
初始化一下

myapp> rs.initiate()  //初始化
myapp:PRIMARY> rs.initiate(config)  //初始化
myapp:PRIMARY>  db.isMaster()      //查看是否为master

myapp:PRIMARY> db.isMaster()

{
“hosts” : [
“192.168.10.100:27017”,
“192.168.10.101:27018”,
“192.168.10.102:27019”
],
“setName” : “myapp”,
“setVersion” : 1,
“ismaster” : true,
。。。。。。。。。

如果在hosts看不见三台,可以在主机试一下手动添加

myapp:PRIMARY>  rs.add("mysql2:27017")
myapp:PRIMARY>  rs.add("mysql3:27017")

测试一下在主库加数据

myapp:PRIMARY>use book
myapp:PRIMARY> db.book.insert({name:"test"})
WriteResult({ "nInserted" : 1 })

随便去一个从库查询(报错了)

myapp:SECONDARY> db.book.find() Error: error: {
“operationTime” : Timestamp(1694054475, 3),
“ok” : 0,
“errmsg” : “not master and slaveOk=false”,
“code” : 13435,
“codeName” : “NotMasterNoSlaveOk”,
“$clusterTime” : {
“clusterTime” : Timestamp(1694054491, 1),
“signature” : {
“hash” : BinData(0,“AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
} }

这是因为备份节点可能会落后于主节点,可能没有最新写入的数据,所以备份节点在默认情况下会拒绝读取请求,以防止应用程序意外拿到过期的数据。因此,如果在备份节点上做查询,可能会得到一个错误提示,说当前节点不是主节点。
这是为了保护应用程序,以免意外连接到备份节点,读取到过期数据。如果希望从备份节点读取数据,需要设置“从备份节点读取数据没有问题”标识

所以我们对从节点也进行设置

myapp:SECONDARY>rs.slaveOk()

就可以查询到数据,实验成功

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值