Original quote: http://blog.chinaunix.net/uid-24250828-id-3770298.html
Official website:
http://www.mongodb.org/
MongoDB installation, master-slave configuration
One MongoDB installation
Click (here) to collapse or open
Yum install wget #选y online Download
- [Email protected]_server src]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.0.tgz
- [Email protected]_server src]# tar xzvf mongodb-linux-x86_64-2.0.0.tgz
- [Email protected]_server src]# MV Mongodb-linux-x86_64-2.0.0/usr/local/mongodb
- [Email protected]_server src]# Mkdir/usr/local/mongodb/data
- [Email protected]_server src]# touch/usr/local/mongodb/logs
- [Email protected]_server bin]#/usr/local/mongodb/bin/mongod–dbpath=/usr/local/mongodb/data–logpath=/usr/local/ Mongodb/logs–logappend–port=27017–fork
- [Email protected]_server bin]#./mongo
- MongoDB Shell version:2.0.0
- Connecting To:test
- > Use test;
- Switched to DB test
- > Exit
- Bye
- [[Email protected]_server bin]# Netstat-anlpt | grep MONGO
- TCP 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 11504/mongod
- TCP 0 0 0.0.0.0:28017 0.0.0.0:* LISTEN 11504/mongod
Attention:
If an error
-bash:/usr/local/mongodb/bin/mongod:cannot Execute binary file
Description of your server and MongoDB version does not correspond, if the server is 64-bit, download x86_64 MongoDB, if the server is 32-bit, download i686 mongodb/
Two MongoDB master-slave configuration
1) Experimental environment
Master: 192.168.0.14
From: 192.168.0.64
2) Time synchronization
Two machines doing time synchronization
[Email protected]_server src]# ntpdate time.windows.com
3) Start the service
master:192.168.0.14
Start command
/usr/local/mongodb/bin/mongod–master–dbpath=/usr/local/mongodb/data–logpath=/usr/local/mongodb/logs–logappend– Port=27017–fork
slave:192.168.0.64
Start command
/usr/local/mongodb/bin/mongod–slave–source 192.168.0.14:27017–dbpath=/usr/local/mongodb/data–logpath=/usr/local /mongodb/logs–logappend–port=27017–fork
4) test Master and slave
Inserting data in the Lord
Test 1:
Click (here) to collapse or open
- Test 1:
- 1 inserting data on the main library
- [Email protected] bin]#./mongo
- MongoDB Shell version:2.0.0
- Connecting To:test
- > Db.foo.save ({"id": 123456, "name": ' Lizonggang '})
- > Db.foo.find ({"id": 123456})
- {"_id": ObjectId ("51c3f4b21399022afd992f39″"), "id": 123456, "name": "Lizonggang"}
- >
- 2 Viewing data from above (data already inserted in the Lord, indicating master-slave success)
- [Email protected]_server bin]#./mongo
- MongoDB Shell version:2.0.0
- Connecting To:test
- > Db.foo.find ({"id": 123456})
- {"_id": ObjectId ("51c3f4b21399022afd992f39″"), "id": 123456, "name": "Lizonggang"}
- >
Test 2:
Click (here) to collapse or open
- 1 inserting data on the main library
- > Use ELAINDB
- Switched to DB elaindb
- > Db.test.save ({title: "This is a test!"})
- > Db.test.find ();
- {"_id": ObjectId ("51c3f8f11a90c8da157c7f64″"), "title": "This is a test!"}
- >
- 2 validating data from a library
- [Email protected]_server bin]#./mongo
- MongoDB Shell version:2.0.0
- Connecting To:test
- > Use ELAINDB
- Switched to DB elaindb
- > Db.test.find ();
- {"_id": ObjectId ("51c3f8f11a90c8da157c7f64″"), "title": "This is a test!"}
- > Db.printreplicationinfo ();
- This is a slave, printing slave replication info.
- source:192.168.0.14:27017
- Syncedto:fri June 14:59:10 gmt+0800 (CST)
- = 9 secs ago (0hrs)
- >
- > Db.printslavereplicationinfo ();
- source:192.168.0.14:27017
- Syncedto:fri June 14:59:30 gmt+0800 (CST)
- = secs ago (0hrs)
- >
Test 3:
Click (here) to collapse or open
- In this state, slave is not allowed to insert data.
- > Db.test.save ({title: "This is slave test!"})
- Not master
5) Some statements of the state of the query
1) Query Library
Click (here) to collapse or open
- > Show DBS;
- Elaindb 0.0625GB
- Local 0.125GB
- Test 0.0625GB
2) Querying the address of the main library from the library
Click (here) to collapse or open
- > Use local;
- Switched to DB Local
- > Db.sources.find ()
- {"_id": ObjectId ("51c3f5dd568d10529950b10d"), "host": "192.168.0.14:27017″," "Source": "Main", "Syncedto": {"T": 137 1797300000, "I": 1}}
- >
3) View master-slave replication status
Click (here) to collapse or open
- > Db.printreplicationinfo ();
- Configured Oplog SIZE:47.6837158203125MB
- Log length start to End:1375secs (0.38hrs)
- Oplog first event Time:fri June 14:30:35 gmt+0800 (CST)
- Oplog last event Time:fri June 14:53:30 gmt+0800 (CST)
- Now:fri June 14:53:32 gmt+0800 (CST)
- >
4) View collection status
Click (here) to collapse or open
- > Db.printcollectionstats ();
- Foo
- {
- "NS": "Test.foo",
- "Count": 1,
- "Size": 56,
- "Avgobjsize": 56,
- "Storagesize": 4096,
- "Numextents": 1,
- "Nindexes": 1,
- "Lastextentsize": 4096,
- "Paddingfactor": 1,
- "Flags": 1,
- "Totalindexsize": 8176,
- "Indexsizes": {
- "_id_": 8176
- },
- "OK": 1
- }
- —
- System.indexes
- {
- "NS": "Test.system.indexes",
- "Count": 1,
- "Size": 64,
- "Avgobjsize": 64,
- "Storagesize": 4096,
- "Numextents": 1,
- "Nindexes": 0,
- "Lastextentsize": 4096,
- "Paddingfactor": 1,
- "Flags": 0,
- "Totalindexsize": 0,
- "Indexsizes": {
- },
- "OK": 1
- }
- —
- >
6) Emergency
What if the master hangs up in the app?
This situation needs to be stopped from the library and the start command from the library to the main library is up.
1) stop from the library
Kill-9 Process Number
2) Delete the Locat file
Click (here) to collapse or open
- RM mongodb/db/locat.*-RF
3) Start from library
Click (here) to collapse or open
- /usr/local/mongodb/bin/mongod–master–dbpath=/usr/local/mongodb/data–logpath=/usr/local/mongodb/logs–logappend– Port=27017–fork
6) mongdb Web viewing interface
7) MongoDB increase, delete, change, check
Click (here) to collapse or open
- [Email protected] bin]#./mongo
- MongoDB Shell version:2.0.0
- Connecting To:test
- > Db.test.insert ({"Name": "Ymind", "Age": 8}); #在test数据库里插入数据
- >
- > Db.test.find ({"Name": "Ymind"}); #查询数据
- {"_id": ObjectId ("51c4187090db4e4751a3cd05″"), "Name": "Ymind", "Age": 8}
- > Db.test.find ({"Age": 8}); #查询数据
- {"_id": ObjectId ("51c4187090db4e4751a3cd05″"), "Name": "Ymind", "Age": 8}
- >
- >
- > db.test.update ({"Name": "Ymind"}, {"$set": {"Age": "5″}}"); #修改数据
- > Db.test.find ({"Name": "Ymind"});
- {"Name": "Ymind", "_id": ObjectId ("51c4187090db4e4751a3cd05″"), "Age": "5″}
- >
- > Db.test.remove ({"Name": "Ymind"}); #删除数据
- > Db.test.find ({"Name": "Ymind"});
CentOS Installation MongoDB