mongodb主从复制小结
在生产环境中单台数据库一般不能满足业务稳定性的需求,所以数据库主从复制架构在生产环境中很常见,用于主从复制也是Mongodb最常用的一种架构方式。这种方式非
在生产环境中单台数据库一般不能满足业务稳定性的需求,所以数据库主从复制架构在生产环境中很常见,用于主从复制也是Mongodb最常用的一种架构方式。这种方式非常灵活,可用于备份、故障恢复、读扩展等,从而提高数据处理性能和冗余,常用架构模式是一主一从、一主多从、双主。今天我们主要对MONGODB的主从架构做一小结。
环境说明:
系统:CentOS6.4_x64
主(Master):192.168.2.2
从(Slave):192.168.2.3
一、安装与基础配置
#cd /usr/local
#tar -zxvf mongodb-linux-x86_64-2.2.6.tgz
#mv mongodb-linux-x86_64-2.2.6.tgz mongodb
mongodb配置文件在 /usr/local/mongodb/conf
主从数据库目录都在 /usr/local/mongodb/data
日志目录都在 /usr/local/mongodb/logs
#mkdir -p /usr/local/mongodb/conf
#mkdir -p /usr/local/mongodb/data
#mkdir -p /usr/local/mongodb/logs
二、一主一从配置
关键点:
1)、在数据库集群中要明确的知道谁是主服务器,主服务器只有一台。
2)、从服务器需要知道自己的数据源,也就是对于自己来说主服务器是谁。
3)、master用来确定主服务器,slave来控制从服务器, source确定从服务的数据源。
192.168.2.2 主
conf下新建一文件mongodb.conf
添加如下:
port=27017
fork=true
logpath=/usr/local/mongodb/logs/mongodb.log
logappend=true
dbpath=/usr/local/mongodb/data
maxConns=1024
master=true
oplogSize=2048
192.168.2.3 从
port=27017
fork=true
logpath=/usr/local/mongodb/logs/mongodb.log
logappend=true
dbpath=/usr/local/mongodb/data
maxConns=1024
slave=true
source=192.168.2.2:27017
autoresync=true
三、启动MONGODB
[root@localhost ~]# echo "PATH=$PATH:/usr/local/mongodb/bin" >> /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# mongod -f /usr/local/mongodb/conf/mongod.conf
先启主再启从
观察一下主的日志mongodb.log
五、思考与补充:
1、如何指定要同步的库
默认是同步所有的库,如果需要指定只同步某个库,可以在从库的配置文件中进行指定
方法:only=test
意思就是只同步TEST库
2、从库上还可设置的其他参数
slavedelay 从节点设置主数据库同步数据的延迟(单位是秒)
fastsync 从节点以主数据库的节点快照为节点启动从数据库
autoresync 如果不同步则自动从新同步数据库
3、从服务器上查看同步状态
>db.printReplicationInfo()
this is a slave, printing slave replication info.
source: 192.168.2.2:27017
syncedTo: Thu Apr 17 2014 01:38:02 GMT-0700 (PDT)
= 10 secs ago (0.04hrs)
4、备份与恢复
任何数据库都需要备份与恢复,这个你懂的。
备份:
>mongodump -h dbhost -d dbname -o dbdirectory
-h:MongDB所在服务器地址,例如:192.168.2.2,当然也可以指定端口号:192.168.2.2:27017
-d:需要备份的数据库实例,例如:test
-o:备份的数据存放位置,例如:/data/dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。
恢复:
>mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
-h:MongoDB所在服务器地址
-d:需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
--directoryperdb:备份数据所在位置,例如:/data/dump/test,这里为什么要多加一个test,而不是备份时候的dump,自己查看提示吧!
--drop:恢复的时候,先删除当前数据,,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,谨慎使用!
5、监控
可以使用mongodb自带28017端口进行图形化监控
本文出自 “滴水穿石孙杰” 博客,请务必保留此出处

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

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

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

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).

MongoDB and relational database: In-depth comparison This article will explore in-depth the differences between NoSQL database MongoDB and traditional relational databases (such as MySQL and SQLServer). Relational databases use table structures of rows and columns to organize data, while MongoDB uses flexible document-oriented models to better suit the needs of modern applications. Mainly differentiates data structures: Relational databases use predefined schema tables to store data, and relationships between tables are established through primary keys and foreign keys; MongoDB uses JSON-like BSON documents to store them in a collection, and each document structure can be independently changed to achieve pattern-free design. Architectural design: Relational databases need to pre-defined fixed schema; MongoDB supports

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.

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

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
