MongoDB installation and master-slave configuration

Source: Internet
Author: User
Tags mongo shell

One, MongoDB installation

1. Download Mongodblinux version https://www.mongodb.com/download-center#community

2. Unzip

Tar xvf mongodb-linux-x86_64-2.0.7.tgz

Mkdir/usr/local/mongodb

MV mongodb-linux-x86_64-2.0.7/*/usr/local/mongodb


3. Create a database file directory

Mkdir-p/mongo/db


Create a MongoDB log file

Mkdir/var/log/mongodb

Touch/var/log/mongodb/mongodb.log


4. Create a soft connection to the MONGO command

The script file in the MONGO Bin directory can be used directly

Ln-s/usr/local/mongodb/bin/*/usr/sbin


5. Configure Startup Parameters

Mkdir/usr/local/mongodb/conf

Cd/usr/local/mongodb/conf

VI mongod.conf

port=27017 #端口号

Fork=true #以守护进程的方式运行, creating a server process

Logpath=/var/log/mongodb/mongodb.log #日志输出文件路径

Logappend=true #日志输出方式

dbpath=/mongo/db #数据库路径

maxconns=600 #数据库的最大连接数


Start: Mongod-f/usr/local/mongodb/conf/mongod.conf



6. Verification

Whether Port 27017 is open

Netstat-nultp


MONGO command into MONGO Shell


7, set the Master and slave:

Assume:

master:10.20.115.185

slave:10.20.115.44


A. Apply the above installation process to MongoDB two hosts, the configuration file changes slightly

On Mongodb-master, the configuration file is incremented

Master=true

oplogsize=2048 #类似于mysql的日志滚动, Unit m


B. On Mongodb-slave, the configuration file is incremented:

Slave=true

source=10.48.100.1:27017 #指定主mongodb Server

slavedelay=10 #延迟复制, unit of seconds

Autoresync=true #当发现从服务器的数据不是最新时, request synchronization of data to the primary server


Third, test master-Slave usability

Start the Mongodb:mongod-f/usr/local/mongodb/conf/mongod.conf on both hosts


1. Below we create the database in our Lord and insert the collection document to see if it is synchronized

On the primary server:

# MONGO

MongoDB Shell version:2.0.7

Connecting To:test

> Show DBS

Local4.201171875gb

> Use Xin

Switched to DB Xin

> Db.test.save ({title: "Just Test"})

> Db.test.find ()

{"_id": ObjectId ("502D3643C5664CA66103A7CF"), "title": "Just Test"}

> Show DBS

Local4.201171875gb

Xin0.203125gb

>

Note: Xin is the database name, test is the collection name, {title: "Just Test"} is a document

MongoDB using use to create a database of course, you can switch the database, and MySQL is a big difference.


2. Execute Rs.slaveok () on the slave node;

3. We enter the MongoDB shell from the server and confirm

# MONGO

MongoDB Shell version:2.0.7

Connecting To:test

> Show DBS

Local0.203125gb

Xin0.203125gb

>

> Use Xin

Switched to DB Xin

> Db.test.find ()

{"_id": ObjectId ("502D3643C5664CA66103A7CF"), "title": "Just Test"}


MongoDB installation and master-slave configuration

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.