绝好的MongoDB学习资料之八. Replication (2)
2. Master/Slave Master/Slave 是一种典型的备份方案,MongoDB 支持 One Master Multi Salver 和 Multi Master One Slave 等多种部署方式。 先从简单的 镜像备份 开始。 $ sudo mkdir -p /var/mongodb/0$ sudo mkdir -p /var/mongodb/1$ sudo ./mongod --for
2. Master/SlaveMaster/Slave 是一种典型的备份方案,MongoDB 支持 "One Master Multi Salver" 和 "Multi Master One Slave" 等多种部署方式。
先从简单的 "镜像备份" 开始。
$ sudo mkdir -p /var/mongodb/0 $ sudo mkdir -p /var/mongodb/1 $ sudo ./mongod --fork --logpath /dev/null --dbpath /var/mongodb/0 --master forked process: 1388 all output going to: /dev/null $ sudo ./mongod --fork --logpath /dev/null --dbpath /var/mongodb/1 --port 27018 --slave --source localhost:27017 --autoresync forked process: 1401 all output going to: /dev/null
autoresync 参数会在系统发生意外情况造成主从数据不同步时,自动启动复制操作 (同步复制 10 分钟内仅执行一次)。除此之外,还可以用 --slavedelay 设定更新频率(秒)。
使用 isMaster、printReplicationInfo、printSlaveReplicationInfo 等命令获取相关状态信息。
$ ./mongo > db.isMaster() { "ismaster" : true, "ok" : 1 } > db.printReplicationInfo() configured oplog size: 990MB log length start to end: 1164secs (0.32hrs) oplog first event time: Mon Aug 23 2010 12:23:54 GMT+0800 (CST) oplog last event time: Mon Aug 23 2010 12:43:18 GMT+0800 (CST) now: Mon Aug 23 2010 12:43:24 GMT+0800 (CST) $ ./mongo localhost:27018 > db.isMaster() { "ismaster" : false, "ok" : 1 } > db.printSlaveReplicationInfo() source: localhost:27017 syncedTo: Mon Aug 23 2010 12:43:58 GMT+0800 (CST) = 10secs ago (0hrs) > show dbs admin local test > use local switched to db local > show collections me pair.sync sources system.indexes > db.sources.find() { "_id" : ObjectId("4c71f8178d806ad3f54dd89a"), "host" : "localhost:27017", "source" : "main", "syncedTo" : { "t" : 1282538738000, "i" : 1 }, "localLogTs" : { "t" : 0, "i" : 0 } }
Slave 的配置信息保存在 local.sources 中。
通常我们会使用主从方案实现读写分离,但需要设置 Slave_OK。
$ ipython IPython 0.10 -- An enhanced Interactive Python. In [1]: from pymongo import * In [2]: m_conn = Connection() In [3]: s_conn = Connection(host="localhost:27018", slave_okay=True) In [4]: m_db = m_conn.test In [5]: s_db = s_conn.test # ----------------------------------------- # In [6]: m_db.users.insert({"name":"user3"}) Out[6]: ObjectId('4c71feb0499b140632000000') In [7]: s_db.users.find_one({"name":"user3"}) # 数据被复制到 Slave Out[7]: {u'_id': ObjectId('4c71feb0499b140632000000'), u'name': u'user3'} In [8]: m_db.users.remove({"name":"user3"}) # 删除 Master 数据 In [9]: for u in m_db.users.find(): print u ....: {u'_id': ObjectId('4c71fa4d5e01e1ba6d62398f'), u'name': u'user1'} In [10]: for u in s_db.users.find(): print u # Slave 记录被同步删除 ....: {u'_id': ObjectId('4c71fa4d5e01e1ba6d62398f'), u'name': u'user1'} # ----------------------------------------- # In [11]: s_db.users.insert({"name":"userx"}) # 在 Slave 插入数据 Out[11]: ObjectId('4c71ff2c499b140632000001') In [12]: for u in m_db.users.find(): print u # Master 上无该数据 ....: {u'_id': ObjectId('4c71fa4d5e01e1ba6d62398f'), u'name': u'user1'} In [13]: for u in s_db.users.find(): print u # Slave 同样没有 ....: {u'_id': ObjectId('4c71fa4d5e01e1ba6d62398f'), u'name': u'user1'}
可见 Slave 读操作正常,但写无效果。
我们还可以部署 "One Slave Two Master" 方案。
$ sudo mkdir -p /var/mongodb/m1 $ sudo mkdir -p /var/mongodb/m2 $ sudo mkdir -p /var/mongodb/s $ sudo ./mongod --fork --dbpath /var/mongodb/m1 --logpath /dev/null --master forked process: 1616 all output going to: /dev/null $ sudo ./mongod --fork --dbpath /var/mongodb/m2 --logpath /dev/null --port 27018 --master forked process: 1627 all output going to: /dev/null $ sudo ./mongod --fork --dbpath /var/mongodb/s --logpath /dev/null --port 27019 --slave forked process: 1638 all output going to: /dev/null
连接 Slave,添加配置。
$ ./mongo localhost:27019 MongoDB shell version: 1.6.1 connecting to: localhost:27019/test > use local switched to db local > db.sources.insert({host:"localhost:27017"}) > db.sources.insert({host:"localhost:27018"}) > db.printSlaveReplicationInfo() source: localhost:27017 syncedTo: Mon Aug 23 2010 13:06:03 GMT+0800 (CST) = 17secs ago (0hrs) source: localhost:27018 doing initial sync
在两台 Master 上添加数据,查看复制效果。
$ ./mongo localhost:27017 MongoDB shell version: 1.6.1 connecting to: localhost:27017/test > use db1 switched to db db1 > db.users.insert({name:"user1"}) > db.users.insert({name:"user2"}) > db.users.find() { "_id" : ObjectId("4c720211ade34e85c5380eab"), "name" : "user1" } { "_id" : ObjectId("4c720214ade34e85c5380eac"), "name" : "user2" } > exit bye
$ ./mongo localhost:27018 MongoDB shell version: 1.6.1 connecting to: localhost:27018/test > use db2 switched to db db2 > db.blogs.insert({title:"aaa"}) > db.blogs.insert({title:"bbb"}) > db.blogs.insert({title:"ccc"}) > db.blogs.find() { "_id" : ObjectId("4c720236f24118cb2f59218d"), "title" : "aaa" } { "_id" : ObjectId("4c720239f24118cb2f59218e"), "title" : "bbb" } { "_id" : ObjectId("4c72023cf24118cb2f59218f"), "title" : "ccc" } > exit bye
$ ./mongo localhost:27019 MongoDB shell version: 1.6.1 connecting to: localhost:27019/test > show dbs admin db1 db2 local > use db1 switched to db db1 > db.users.find() { "_id" : ObjectId("4c720211ade34e85c5380eab"), "name" : "user1" } { "_id" : ObjectId("4c720214ade34e85c5380eac"), "name" : "user2" } > use db2 switched to db db2 > db.blogs.find() { "_id" : ObjectId("4c720236f24118cb2f59218d"), "title" : "aaa" } { "_id" : ObjectId("4c720239f24118cb2f59218e"), "title" : "bbb" } { "_id" : ObjectId("4c72023cf24118cb2f59218f"), "title" : "ccc" } >
一切正常。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies
