Home Database Mysql Tutorial [置顶] Mongodb的主从模式搭建实例

[置顶] Mongodb的主从模式搭建实例

Jun 07, 2016 pm 02:51 PM
mongodb Example build database model pin to top

对数据库来说,为了保证数据的安全性和高可用性,往往会采用主从架构模式,对于Mongodb也是如此,这篇文章将讲述一下如何搭建一个简单的Mongodb主从架构模型。 在下面的实例中,因为没有多个服务器的原因,在一台服务器上进行了实验,采用不同的端口号进行区

对数据库来说,为了保证数据的安全性和高可用性,往往会采用主从架构模式,对于Mongodb也是如此,这篇文章将讲述一下如何搭建一个简单的Mongodb主从架构模型。

在下面的实例中,因为没有多个服务器的原因,在一台服务器上进行了实验,采用不同的端口号进行区分主从。

首先我们需要的是下载Mongodb的官方版本,这里我下载的是2.4.1的Linux版本,并在Linux下进行解压,然后创建两个文件夹master和slave,分别存放主Mongodb实例和从mongodb实例。


然后我们首先启动master中的主Mongodb实例,如下所示:

我们看到了,Mongodb已经在27017端口等待客户端连接了,并且从“master:true”看到当前Mongodb实例是以master身份启动的。

接下来,我们启动从Mongodb实例,并且指定该从Mongodb是那个主Mongodb的从实例,

mongod --port 27018 --dbpath=/lingxi/local/mongodb/slave/mongodb/mongodbdata/ -slave -source 172.31.4.15:27017

这里我们将从Mongodb实例在27018端口启动,并且作为原来的27017端口上的Mongodb实例的从实例。启动代码如下:


从上面标红可以看出,Mongodb从实例已经启动起来。接下来就是验证主从模式的时候了。这里,我们只做简单的测试,我们往主Mongodb上写入一条数据,看看Mongodb从实例上是否会有数据。


在上面,我们首先创建了一个新的集合,然后插入了一条新建了test集合,并且插入了一条文档{"caller":"18326149379"},然后我们再次插入一条文档,我们看看在插入过程中Mongodb从实例能否及时拿到新的数据,如下所示:

从结果来看,从Mongodb实例能够及时获知主Mongodb实例的数据变化并进行同步,至此一个简单的Mongodb主从模式便搭建起来了。

需要说明的是,因为主从模式也存在着一些问题,针对这些问题,Mongodb有其他方法能够较好的解决,所以Mongodb官方推荐以后尽量不要使用Mongodb主从来提供高可用方案,取而代之的是采用副本集和分片技术,针对这个问题的说明,回头整理一篇文章进行说明,谢谢。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

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:

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

How to choose a database for GitLab on CentOS How to choose a database for GitLab on CentOS Apr 14, 2025 pm 04:48 PM

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

Real-World MySQL: Examples and Use Cases Real-World MySQL: Examples and Use Cases Apr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

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.

How to set up users in mongodb How to set up users in mongodb Apr 12, 2025 am 08:51 AM

To set up a MongoDB user, follow these steps: 1. Connect to the server and create an administrator user. 2. Create a database to grant users access. 3. Use the createUser command to create a user and specify their role and database access rights. 4. Use the getUsers command to check the created user. 5. Optionally set other permissions or grant users permissions to a specific collection.

See all articles