MongoDB installation, master-slave configuration, replicaset configuration

Source: Internet
Author: User
Tags mongodb time interval

I. Installation of MongoDB

Environment: Linux 64bit

Version: 2.0.1

Location:/opt/mongodb

Installation steps:

TAR–ZXVF Mongodb-linux-x86_64-2.0.1.tar–c/opt/mongodb

Mkdir/opt/mongodb/data

Touch/opt/mongodb/logs

The installation is very simple, the key is the following boot configuration. Boot configuration has three kinds of conditions, are normal single host boot, master-slave startup, replica set to start three ways.

1. Single host startup mode

Start

Cd/opt/mongodb/bin

/opt/mongodb/bin/mongod–f mongodb.conf

#mongodb. conf is a custom startup configuration file in which parameter auth indicates that authentication is required to log on, as follows:

#start as daemon and need authentication

port=27017

Fork=true

dbpath=/opt/mongodb/data/

Logpath=/opt/mongodb/logs

Auth=true

Set up automatic boot (optional)

echo "/opt/mongodb/bin/mongod–f/opt/mongodb/bin/mongodb.conf" >>/etc/rc.local

Set Password

#连接mongodb

Cd/opt/mongodb/bin

./mongo

#默认进入到mongodb默认的test库

#为数据库设置用户名密码

#首先切换到admin库, create an administrator account first

> Use admin

> Db.adduser ("Root", "root");

#切换到test库

Use test

#为test库添加用户名和密码

> Db.adduser ("TestUser", "123456");

#注: If displayed at this time

Tue may 16:16:29 uncaught exception:error {

"$err": "Unauthorized db:test lock Type:-1 client:127.0.0.1",

"Code": 10057

}

Instructions to set the password has been in effect, at this point to go to the admin library, enter the authentication and then go back to the test library set the username password, as follows:

Use admin

Db.auth ("root", "root");

Use test

#下次再连接mongodb时需要使用如下方式:

./mongo–u testuser–p 123456

#删除用户

Db.system.users.remove ({"User": "TestUser"});

#注: MongoDB username and password are for each library, so different libraries can set different user names and passwords.

2. master-slave mode start

In fact, this method is not recommended now, the recommended use is Replicaset, the following will be mentioned

Suppose you have installed MongoDB on two hosts, IP 192.168.0.1 and 192.168.0.2, and the following is the boot configuration

Start Master (192.168.0.1)
cd/opt/mongodb/
./bin/mongod--dbpath=/opt/mongodb/data--logpath=/opt/mongodb/logs
--master--oplogsize--logappend--port=27017–fork

Start slave (192.168.0.2)
cd/opt/mongodb/
./bin/mongod--dbpath=/opt/mongodb/data--logpath=/opt/mongodb/logs
--slave--source 192.168.0.1--only Test--slavedelay 10
--logappend--port=27017–fork

This configuration is complete, and of course you can write these startup parameters in the configuration file as before, starting with a configuration file at startup.

After the configuration is complete, you can test it as follows:

Add data to the main machine
cd/opt/mongodb/
./bin/mongo
Db.foo.save ({"id": 123, "name": Gongyong});
You can see the data from the server if you succeed:
cd/opt/mongodb/
./bin/mongo
Db.foo.find ({"id": 123})

3. Configuration of Replcaset

We assume that there are three servers, are already installed MongoDB, with the following three sections configuration

#10. Configuration on 127.65.92

Logpath=/opt/mongodb/logs

dbpath=/opt/mongodb/data/

port=27017

Fork=true

Logappend=true

replset=rs1/10.127.65.95:27017

#10. Configuration on 127.65.95

Logpath=/opt/mongodb/logs

dbpath=/opt/mongodb/data/

port=27017

Fork=true

Logappend=true

replset=rs1/10.127.65.92:27017

#10. Configuration on 11.154.91

Logpath=/opt/mongodb/logs

dbpath=/opt/mongodb/data/

port=27017

Fork=true

Logappend=true

replset=rs1/10.127.65.92:27017,10.127.65.95:27017

Each of the above three sections are replicated and placed in a location (recommended bin directory) for each MongoDB, which is started with the above configuration, to be initialized

First, connect the 65.92 MONGO.

./mongo-port 27017

This connects to the default library test for MongoDB, and then you enter the following directive:

> config_rs1={_id: ' rs1 ', members:[

... {_id:0,host: ' 10.127.65.92:27017 ', priority:10},

... {_id:1,host: ' 10.127.65.95:27017 ', priority:9},

... {_id:2,host: ' 10.11.154.91:27017 ', priority:9}]

...}

> rs.initiate (config_rs1);

The following prompts appear after the carriage return:

{

"Info": "Config now saved locallly." Should come online in about a minute. ",

"OK": 1

}

Description of initialization success, note that only need to initialize on any one of them can

Run the following instructions to see if the success

Rs.status ()

Then you exit and reconnect, and you find that the cursor becomes the following:

Rs1:primary

After the configuration is complete, you can set the secret and user name, set the way mentioned above, it should be noted that each MongoDB need to set the user name and secret in the same way.

Here, the installation of MongoDB, master and slave, the configuration of the replica set is basically completed, and then there are mongodb cluster fragmentation, in another article will be detailed description of http://gong1208.iteye.com/blog/1622078.

attachment: Description of each startup parameter

Parameter interpretation:--dbpath database path (data file)
--logpath log file path
--master designated as the main machine
--slave specified as from machine
--SOURCE Specifies the IP address of the primary machine
--pologsize command-line arguments (used with--master) configure the disk space (m) that is used to store the updated information available from the node, and if this parameter is not specified, the default size is 5% of the current available disk space (the 64-bit machine minimum is 1G). 32-bit machine is 50M).
--logappend the end of the log file add
--port Enable port number
--fork running in the background
--only specify which database to copy only
--slavedelay refers to the time interval from replication detection
--auth whether to require authentication permission logins (username and password)
-H [--help] Show this usage information
--version Show version Information
-F [--config] arg configuration file specifying additional options
--port Arg Specify port number
--BIND_IP arg local IP addresses to bind Listener-all local IPs
Bound by default
-V [--verbose] is more verbose (include multiple
verbosity e.g.-VVVVV)
--dbpath Arg (=/data/db/) directory for datafiles specify data storage directory
--quiet Quieter output Silent mode
--logpath arg file to send all output to instead of stdout specify log storage directory
--logappend APPND to LogPath instead of over-writing Specify whether the log is written in an append or overwrite way
--fork Fork Server process to create a subprocess
--CPU periodically show CPU and iowait utilization periodic display CPU and IO usage
--noauth run without security no authentication mode
--auth run with security authentication mode
--objcheck inspect client data for validity on receipt check the validity check of clients ' input data
--quota Enable DB quota management start database quota management
--quotafiles arg number of files Allower per db, requires--quota specify how many files each database allows
--appsrvpath arg root directory for the Babble app server
--nocursors diagnostic/debugging option Debug Diagnostics options
--nohints Ignore query hints ignore hit hits
--nohttpinterface Disable HTTP Interface shutdown HTTP interface, default is 28017
--noscripting Disable scripting engine shutdown script engine
--noprealloc Disable data file preallocation shutdown database File size pre-allocation
--smallfiles use a smaller default file size with a smaller defaults
--nssize Arg (=16). ns file Size (in MB) for new databases The default size of the newly-NS files
--diaglog arg 0=off 1=w 2=r 3=both 7=w+some reads provides a way to read-only, write only, read or write, or mainly write + part of the reading mode
--sysinfo Print Some diagnostic system Information printer diagnostic information
--upgrade upgrade DB If needed update database if needed
--repair Run Repair on all DBS repairs all databases
--notablescan does not allow table scans does not run a sheet scan
--syncdelay Arg (=60) seconds between disk syncs (0 for never) system synchronized Refresh Disk Time, default 60s
Replication options:
--master Master Mode primary replication mode
--slave slave mode from replication
--source Arg when slave:specify master as <server:port> specifies the address and port of the master when from
--only Arg when slave:specify a singleton to replicate specifies a single library that needs to be copied from the primary
--pairwith ARG address of the server to pair with
--arbiter arg address of arbiter server arbiter, used in main and pair
--autoresync automatically resync if slave data is stale automatic synchronization from
--oplogsize arg size limit (in MB) for OP log specifies the size of the action logs
--opidmem arg size limit (in bytes) for in memory storage of OP ids specifies the memory size of the storage action log
Sharding options:
--configsvr Declare this is a config db of a cluster specifies the configuration server in Shard
--shardsvr declare this is a shard db of a cluster specify Shard server

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.