1, first install MongoDB, see previous blog post
2, execute the following command on the master node
Mongod--dbpath=/var/lib/mongo--master--oplogsize 27017--port
You see the following message that started successfully
2015-05-18t09:51:12.111+0800 I STORAGE [initandlisten] * * Warning:readahead For/var/lib/mongo is set to 4096KB2015-05-1 8t09:51:12.111+0800 I STORAGE [initandlisten] * * We suggest setting it to 256KB (sectors) or less2015-05-18t 09:51:12.111+0800 I STORAGE [initandlisten] * * http://dochub.mongodb.org/core/readahead2015-05-18T09:51:12.112+0 JOURNAL [Initandlisten] JOURNAL dir=/var/lib/mongo/journal2015-05-18t09:51:12.112+0800 i JOURNAL [Initandlisten] Recover:no journal files present, no recovery needed2015-05-18t09:51:12.162+0800 I Journal [durability] Durability thre Ad started2015-05-18t09:51:12.162+0800 I JOURNAL [JOURNAL writer] JOURNAL writer thread started2015-05-18t09:51:12.162+ 0800 I CONTROL [Initandlisten] MongoDB starting:pid=29245 port=27017 dbpath=/var/lib/mongo master=1 64-bit Host=localho st.localdomain2015-05-18t09:51:12.162+0800 I CONTROL [initandlisten] * * Warning:you is running this process as the root User, which is not recommended.2015-05-18t09:51:12.162+0800 I control [Initandlisten] 2015-05-18t09:51:12.162+0800 i control [Initandlisten] 2015-05-18t09:51:12.162+0800 I CONTROL [initandlisten] * * WARNING:/sys/kernel/mm/transparent_hugepage/enabled is ' Always '. 2015-05-18t09:51:12.162+0800 I CONTROL [initandlisten] * * We suggest setting it to ' Never ' 2015-05-18t09:51 : 12.162+0800 i control [Initandlisten] 2015-05-18t09:51:12.162+0800 i control [initandlisten] * * WARNING:/sys/kernel/mm /transparent_hugepage/defrag is ' always '. 2015-05-18t09:51:12.162+0800 I CONTROL [initandlisten] * * We suggest sett ing it to ' never ' 2015-05-18t09:51:12.162+0800 I control [Initandlisten] 2015-05-18t09:51:12.162+0800 i control [Initandl Isten] DB version v3.0.22015-05-18t09:51:12.163+0800 I CONTROL [Initandlisten] git version: 6201872043ecbbc0a4cc169b5482dcf385fc464f2015-05-18t09:51:12.163+0800 I CONTROL [Initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 20132015-05-18t09:51:12.163+0800 i CONTROL [iNitandlisten] Build info:linux ip-10-169-109-10 3.10.0-121.el7.x86_64 #1 SMP Tue Apr 8 10:48:19 EDT x86_64 boost_lib _version=1_492015-05-18t09:51:12.163+0800 I CONTROL [Initandlisten] allocator:tcmalloc2015-05-18t09:51:12.163+0800 I CONTROL [Initandlisten] Options: {master:true, net: {port:27017}, replication: {oplogsizemb:64}, storage: {DbP Ath: "/var/lib/mongo"}}
3, execute the following command from the node
Mongod--dbpath=/var/lib/mongo--port=37017--slave
You see the following log is started successfully
015-05-18t11:26:21.321+0800 I REPL [replslave] repl:end sync_pulloplog syncedto:may 11:26:16 55595bd8:12015-05-1 8t11:26:21.322+0800 i REPL [Replslave] repl:syncing from host:192.168.1.1:270172015-05-18t11:26:31.322+0800 i REPL [Replslave] repl:applied 1 operations2015-05-18t11:26:31.322+0800 I repl [replslave] repl:end Sync_pullOpLog sy Ncedto:may 11:26:26 55595be2:12015-05-18t11:26:31.322+0800 I REPL [Replslave] repl:syncing from host:192.168.1.1: 270172015-05-18t11:26:41.322+0800 i REPL [Replslave] repl:applied 1 operations2015-05-18t11:26:41.322+0800 i REPL [Replslave] repl:end sync_pulloplog syncedto:may 11:26:36 55595bec:12015-05-18t11:26:41.322+0800 I repl [repl Slave] repl:syncing from host:192.168.1.1:270172015-05-18t11:26:51.322+0800 I repl [Replslave] repl:applied 1 oper ations2015-05-18t11:26:51.322+0800 I REPL [replslave] repl:end sync_pulloplog syncedto:may 11:26:46 55595bf6:120 15-05-18t11:26:51.322+08xx i REPL [Replslave] repl:syncing from host:192.168.1.1:270172015-05-18t11:27:01.323+0800 I REPL [Replslave] Rep L:applied 1 operations2015-05-18t11:27:01.323+0800 I REPL [replslave] repl:end sync_pulloplog syncedto:may 18 11 : 26:56 55595c00:12015-05-18t11:27:01.323+0800 I REPL [Replslave] repl:syncing from HOST:192.168.1.1:270172015-05-18T1 1:27:11.323+0800 i REPL [Replslave] repl:applied 1 operations2015-05-18t11:27:11.323+0800 I REPL [Replslave] Re Pl:end sync_pulloplog syncedto:may 11:27:06 55595c0a:12015-05-18t11:27:11.323+0800 I REPL [Replslave] Repl:sync ing from host:192.168.1.1:270172015-05-18t11:27:21.323+0800 I REPL [Replslave] repl:applied 1 operations2015-05-18t 11:27:21.323+0800 I REPL [replslave] repl:end sync_pulloplog syncedto:may 11:27:16 55595c14:12015-05-18t11:27:21 .323+0800 i REPL [Replslave] repl:syncing from host:192.168.1.1:270172015-05-18t11:27:31.323+0800 i REPL [Replsla VE] repl:applied 1 operations2015-05-18t11:27:31.323+0800 I REPL [replslave] repl:end sync_pulloplog syncedto:may 11:27:26 55595c1e:1 2015-05-18t11:27:31.323+08
4, a new record is added to the main library
> Use testswitched to DB test> Db.blog.save ({title: "new Article"}) Writeresult ({"ninserted": 1})
5, Query from library
> db.blog.find () {"_id": ObjectId ("55595022e26fd5794a58bdd3"), "title": "New Article"}
Both master and slave have this record, that is, the configuration is successful.
Precautions:
1, the error is as follows:
REPL:--source 192.168.1.1:27017! = 192.168.1.1:27017 from local.sources collection
Cause: The first time you execute the command from the library, the wrong source is written from the library's IP, and the second time the correct main library IP is modified. Since the previous source IP was stored in the local table from the library, an error will be made after execution
Workaround: Execute the following command to delete the wrong information
Db.sources.remove ({' Host ': ' 192.168.1.1:27017 '})
2. Set synchronization timing from the library
Add by joeyon1985
redhat7.0 under MongoDB master-slave collocation