Master/Slave configurations of MongoDB in Linux

Source: Internet
Author: User
Tags findone scp command

One master and slave configuration method
1. HOST (master)
MASTER: 172.16.101.20.
MongoDB home:/usr/local/Server/MongoDB/
Data File:/usr/local/Server/MongoDB/data/
Log File:/usr/local/Server/MongoDB/logs

2. slave machine (slave)
MASTER: 172.16.101.244
MongoDB home:/usr/local/Server/MongoDB/
Data File:/usr/local/Server/MongoDB/data/
Log File:/usr/local/Server/MongoDB/logs

Note: It is best to use the SCP command to remotely copy all directories and files of MongoDB in the host.
Do not start the host before starting the slave machine. Otherwise, an error will be reported when starting the slave machine (in
The following error is displayed in the log file: MongoDB error: Client ::~ Client _ context shoshould be null but is not; client: replslave)
In this case, you only need to delete all the files in the data directory of the data file and restart it.

3. Start master
CD/usr/local/Server/MongoDB/
./Bin/mongod -- dbpath =/usr/local/Server/MongoDB/data -- logpath =/usr/local/Server/MongoDB/logs
-- Master -- oplogsize 64 -- logappend -- Port = 27017 -- fork

4. Start slave
CD/usr/local/Server/MongoDB/
./Bin/mongod -- dbpath =/usr/local/Server/MongoDB/data -- logpath =/usr/local/Server/MongoDB/logs
-- Slave -- source 172.16.101.20.-- only test -- slavedelay 10
-- Logappend -- Port = 27017 -- fork

5. Use the following command if you need to connect the master and slave nodes:
Insert. New records can be viewed on the other side)
5.1. Start master
CD/usr/local/Server/MongoDB/
./Bin/mongod -- dbpath =/usr/local/Server/MongoDB/data -- logpath =/usr/local/Server/MongoDB/logs
-- Master -- slave -- source 172.16.101.244
-- Logappend -- Port = 27017 -- fork

5.2. Start slave
CD/usr/local/Server/MongoDB/
./Bin/mongod -- dbpath =/usr/local/Server/MongoDB/data -- logpath =/usr/local/Server/MongoDB/logs
-- Master -- slave -- source 172.16.101.small -- only test -- slavedelay 10
-- Logappend -- Port = 27017 -- fork

6. Test
Add data to the host
CD/usr/local/Server/MongoDB/
./Bin/Mongo
DB. Foo. Save ({"ID": 123456, "name": 'liuyuanshijie '});

If the operation succeeds, you can view the data on the server:
CD/usr/local/Server/MongoDB/
./Bin/Mongo
DB. foo.find ({"ID": 123 })

2. How to configure replica Pairs
1. HOST (master)
MASTER: 172.16.101.20.
MongoDB home:/usr/local/Server/MongoDB/
Data File:/usr/local/Server/MongoDB/data/
Log File:/usr/local/Server/MongoDB/logs

2. slave machine (slave)
MASTER: 172.16.101.244
MongoDB home:/usr/local/Server/MongoDB/
Data File:/usr/local/Server/MongoDB/data/
Log File:/usr/local/Server/MongoDB/logs

Note: It is best to use the SCP command to remotely copy all directories and files of MongoDB in the host.
Do not start the host before starting the slave machine. Otherwise, an error will be reported when starting the slave machine (in
The following error is displayed in the log file: MongoDB error: Client ::~ Client _ context shoshould be null but is not; client: replslave)
In this case, you only need to delete all the files in the data directory of the data file and restart it.

3. Start master
CD/usr/local/Server/MongoDB/
./Bin/mongod -- dbpath =/usr/local/Server/MongoDB/data -- logpath =/usr/local/Server/MongoDB/logs \
-- Pairwith 172.16.101.244 -- arbiter <arbiterserver> -- logappend -- Port = 27017 -- fork

4. Start slave
CD/usr/local/Server/MongoDB/
./Bin/mongod -- dbpath =/usr/local/Server/MongoDB/data -- logpath =/usr/local/Server/MongoDB/logs \
-- Pairwith 172.16.101.251 -- arbiter <arbiterserver> -- logappend -- Port = 27017 -- fork
Note: -- arbiter <arbiterserver> this parameter is optional if it is a virtual host under the same vro or under the same host.

5. view the status on 251
$./Mongo
> DB. $ cmd. findone ({ismaster: 1 });
{"Ismaster": 0.0, "remote": "192.168.58.1: 30001", "OK": 1.0}

6. Set the permission to 251.
$./Mongo 172.101.133/admin
> Use local
> DB. adduser ('repl', <replpassword> );
Press Ctrl + C to exit and then log on to the slave server
$./Mongo 172.101.244/admin
> Use local
> DB. adduser ('repl', <replpassword> );
The two accounts and passwords must be consistent

7. Check that the status is on 251
> DB. $ cmd. findone ({replacepeer: 1 });
{
"Info": "adjust local. Sources hostname; DB restart now required ",
"OK": 1.0
}

8. Restart MongoDB
DB. shutdownserver ();

9. Set the slave, which can be set in both, but it is best
DB. getmongo (). setslaveok ()

10. Test, insert data on the master, and then you can view the result from DB. Find ().

Note: In this mode, MongoDB determines who is the master and who is the slave, and when I test
Only documents can be inserted and modified at the master end. If documents are inserted at the slave end, no master is reported.
This question remains to be studied. If you know the answer, please let us know. Thank you!
 
Three-parameter explanation
Parameter description: -- dbpath database path (data file)
-- Logpath: Log File Path
-- Master is specified as the master machine.
-- Slave is specified as the slave machine
-- Source: Specifies the IP address of the host.
The -- pologsize command line parameter (used together with -- master) is used to configure the disk space used for storage of available update information for slave nodes (in MB). If this parameter is not specified, the default size is 5% of the current available disk space (the minimum value of 64-bit machines is 1 GB, and that of 32-bit machines is 50 MB ).
-- Add at the end of the logappend Log File
-- Port enables the port number
-- Fork runs in the background
-- Only specifies which database to copy
-- Slavedelay indicates the interval between slave replication detection.
-- Does auth need to verify the permission to log on (user name 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 address to bind listener-all local IPS
Bound by default
-V [-- verbose] Be more verbose (include multiple times for more
Verbosity e.g.-vvvvv)
-- Dbpath Arg (=/data/DB/) directory for datafiles specifies the data storage directory
-- Quiet quieter output silent mode
-- Logpath Arg file to send all output to instead of stdout specifies the log storage directory
-- Logappend appnd to logpath instead of over-writing specifies whether to append or overwrite a log file.
-- Fork server process is run by creating a sub-process
-- CPU periodically show CPU and iowait utilization periodically displays CPU and IO usage
-- Noauth run without security run in No Authentication Mode
-- Auth run with Security Authentication Mode
-- Objcheck inspect client data for validity on receipt check the validity of client input data
-- Quota enable dB Quota Management
-- Quotafiles Arg number of files allower per dB, requires -- quota specifies the number of files allowed by each database
-- Initialize rvpath Arg root directory for the babble app Server
-- Nocursors diagnostic/debugging option debug diagnostic options
-- Nohints ignore query hints ignore query hit rate
-- Nohttpinterface disable HTTP interface: Disable the HTTP interface. The default value is 28017.
-- Noscripting disable scripting engine shut down the Script Engine
-- Noprealloc disable data file preallocation disable database file size pre-allocation
-- Smallfiles use a smaller default file size
-- Nssize Arg (= 16). Ns file size (in MB) for new databases default size of the NS file in the new database
-- Diaglog Arg 0 = off 1 = W 2 = R 3 = both 7 = W + some reads: Read-only, write-only, or read/write, or the primary write + part Read mode
-- Sysinfo print some diagnostic system information print system diagnostic information
-- Upgrade DB if needed update the database if necessary
-- Repair run repair on all DBS fix all databases
-- Notablescan do not allow table scans does not run table Scan
-- Syncdelay Arg (= 60) seconds between disk syncs (0 for never) the time when the system refreshes the disk synchronously. The default value is 60 s.

Replication options:
-- Master mode master replication Mode
-- Slave mode slave replication Mode
-- Source Arg when slave: Specify master as <server: Port> when it is a slave, specify the Master Address and Port
-- Only Arg when slave: specify a single database to replicate when it is set to slave, specify a single database to be copied from the master
-- Pairwith Arg address of server to pair
-- Arbiter Arg address of arbiter server arbitration server, used in master and pair
-- Autoresync automatically Resync if Slave Data is stale automatically synchronize Slave Data
-- Oplogsize Arg size limit (in MB) for OP log specifies the size of the operation log
-- Opidmem Arg size limit (in bytes) for in memory storage of OP IDs specifies the memory size for storing operation logs.

Sharding options:
-- Configsvr declare this is a Config dB of a cluster specifies the configuration server in the shard.
-- Shardsvr declare this is a shard dB of a cluster specified 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.