MongoDB master-slave copy and replica set (iv)

Source: Internet
Author: User
Tags mongodb

Simple Master-slave replication
    • Using a master-slave or a master-slave mode, you can separate read and write, improve the availability of the database, but MongoDB's master-slave mode is not able to replace the master node after the failure of the master node, it can generally be used in the development phase.
Implementation steps
    • Setting up the configuration file
# mongodb.conf1 # 主节点dbpath=/var/lib/mongodb1 # 设置数据文件#where to loglogpath=/var/log/mongodb/mongodb1.log # 设置日志文件logappend=truebind_ip = 127.0.0.1,192.168.1.21 # 设置ipport = 27018 # 设置portjournal=truemaster = true # 设置主节点# mongodb.conf2 # 从节点dbpath=/var/lib/mongodb2 # 设置数据文件#where to loglogpath=/var/log/mongodb/mongodb2.log # 设置日志文件logappend=truebind_ip = 127.0.0.1,192.168.1.21 # 设置ipport = 27019 # 设置portjournal=trueslave = true # 设置从节点source = 192.168.1.21:27018 # 指定主节点
    • Starting with a configuration file
# 启动主节点mongod -f /etc/mongodb.conf1 &# 启动从节点mongod -f /etc/mongodb.conf2 &
    • The test found that from the node can be read and not writable, the primary node after killing, from the node can not replace the master node.
Replica set
    • Definition: Replication provides redundant backups of data, stores copies of data on multiple servers, improves data availability, and guarantees data security.

    • Characteristics:

    1. The primary and standby node stores data, and the quorum node does not store data. The client connects the primary node to the standby node at the same time and does not connect the quorum node.

    2. All nodes are divided into 3 classes, master nodes, slave nodes and quorum nodes.

    3. By default, the master node provides all the additions and deletions, and the standby node does not provide any services. However, you can provide a query service by setting up the standby node;

    4. Auto Fail-Over

    5. Automatic recovery

Implementation steps
    • Set up three configuration files, configured for primary node, slave node, quorum node, respectively
# mongodb.confdbpath=/var/lib/mongodb # 数据文件logpath=/var/log/mongodb/mongodb.log  # 日志文件pidfilepath=/var/lib/mongodb/mongodb.pid  # 进程文件logappend=true  # 添加方式replSet=name # 设置副本集名称bind_ip=192.168.1.21  # 绑定ipport=27017  # portfork=true  # 后台运行noprealloc=true# mongodb.conf1dbpath=/var/lib/mongodb1 # 数据文件logpath=/var/log/mongodb/mongodb1.log  # 日志文件pidfilepath=/var/lib/mongodb/mongodb1.pid  # 进程文件logappend=true  # 添加方式replSet=name # 设置副本集名称bind_ip=192.168.1.21  # 绑定ipport=27018  # portfork=true  # 后台运行noprealloc=true# mongodb.conf2dbpath=/var/lib/mongodb2 # 数据文件logpath=/var/log/mongodb/mongodb2.log  # 日志文件pidfilepath=/var/lib/mongodb/mongodb2.pid  # 进程文件logappend=true  # 添加方式replSet=name # 设置副本集名称bind_ip=192.168.1.21  # 绑定ipport=27019  # portfork=true  # 后台运行noprealloc=true
    • Start node
mongo -f /etc/mongodb.confmongo -f /etc/mongodb1.confmongo -f /etc/mongodb2.conf
    • Configuration node
mongo ip:portuse admin  fuben={ _id:"name", members:[ {_id:0,host:‘192.168.1.21:27017‘,priority:2}, {_id:1,host:‘192.168.1.21:27018‘,priority:1},   {_id:2,host:‘192.168.1.21:27019‘,arbiterOnly:true}] };  rs.initiate(fuben)   

Parameters:
Priority: Precedence
Arbiteronly: Quorum node

    • Sometimes error
"errmsg" : "couldn‘t initiate : member 192.168.1.21:27018 has data already, cannot initiate set.  All members except initiator must be empty."# 需要清空数据库,将数据库文件都删除
    • After the entry into force, the slave node is currently not allowed to read and write, set:
# 登录从节点rs.slaveOk();
    • Kill the Master node, you can see from the node to the primary node, after restarting the master node, the master node or the master node, will not become from the node (eldest brother is always the eldest);
Reference:
    • Https://www.cnblogs.com/hadoop-dev/p/6069780.html

    • https://docs.mongodb.com/manual/introduction/

MongoDB master-slave copy and replica set (iv)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.