CentOS Installation MongoDB

Source: Internet
Author: User
Tags mongodb version

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

  1. [Email protected]_server src]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.0.tgz
  2. [Email protected]_server src]# tar xzvf mongodb-linux-x86_64-2.0.0.tgz
  3. [Email protected]_server src]# MV Mongodb-linux-x86_64-2.0.0/usr/local/mongodb
  4. [Email protected]_server src]# Mkdir/usr/local/mongodb/data
  5. [Email protected]_server src]# touch/usr/local/mongodb/logs
  6. [Email protected]_server bin]#/usr/local/mongodb/bin/mongod–dbpath=/usr/local/mongodb/data–logpath=/usr/local/ Mongodb/logs–logappend–port=27017–fork
  7. [Email protected]_server bin]#./mongo
  8. MongoDB Shell version:2.0.0
  9. Connecting To:test
  10. > Use test;
  11. Switched to DB test
  12. > Exit
  13. Bye
  14. [[Email protected]_server bin]# Netstat-anlpt | grep MONGO
  15. TCP 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 11504/mongod
  16. 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

    1. Test 1:
    2. 1 inserting data on the main library
    3. [Email protected] bin]#./mongo
    4. MongoDB Shell version:2.0.0
    5. Connecting To:test
    6. > Db.foo.save ({"id": 123456, "name": ' Lizonggang '})
    7. > Db.foo.find ({"id": 123456})
    8. {"_id": ObjectId ("51c3f4b21399022afd992f39″"), "id": 123456, "name": "Lizonggang"}
    9. >
    10. 2 Viewing data from above (data already inserted in the Lord, indicating master-slave success)
    11. [Email protected]_server bin]#./mongo
    12. MongoDB Shell version:2.0.0
    13. Connecting To:test
    14. > Db.foo.find ({"id": 123456})
    15. {"_id": ObjectId ("51c3f4b21399022afd992f39″"), "id": 123456, "name": "Lizonggang"}
    16. >

Test 2:

Click (here) to collapse or open

  1. 1 inserting data on the main library
  2. > Use ELAINDB
  3. Switched to DB elaindb
  4. > Db.test.save ({title: "This is a test!"})
  5. > Db.test.find ();
  6. {"_id": ObjectId ("51c3f8f11a90c8da157c7f64″"), "title": "This is a test!"}
  7. >
  8. 2 validating data from a library
  9. [Email protected]_server bin]#./mongo
  10. MongoDB Shell version:2.0.0
  11. Connecting To:test
  12. > Use ELAINDB
  13. Switched to DB elaindb
  14. > Db.test.find ();
  15. {"_id": ObjectId ("51c3f8f11a90c8da157c7f64″"), "title": "This is a test!"}
  16. > Db.printreplicationinfo ();
  17. This is a slave, printing slave replication info.
  18. source:192.168.0.14:27017
  19. Syncedto:fri June 14:59:10 gmt+0800 (CST)
  20. = 9 secs ago (0hrs)
  21. >
  22. > Db.printslavereplicationinfo ();
  23. source:192.168.0.14:27017
  24. Syncedto:fri June 14:59:30 gmt+0800 (CST)
  25. = secs ago (0hrs)
  26. >

Test 3:

Click (here) to collapse or open

    1. In this state, slave is not allowed to insert data.
    2. > Db.test.save ({title: "This is slave test!"})
    3. Not master

5) Some statements of the state of the query

1) Query Library

Click (here) to collapse or open

    1. > Show DBS;
    2. Elaindb 0.0625GB
    3. Local 0.125GB
    4. Test 0.0625GB

2) Querying the address of the main library from the library

Click (here) to collapse or open

    1. > Use local;
    2. Switched to DB Local
    3. > Db.sources.find ()
    4. {"_id": ObjectId ("51c3f5dd568d10529950b10d"), "host": "192.168.0.14:27017″," "Source": "Main", "Syncedto": {"T": 137 1797300000, "I": 1}}
    5. >

3) View master-slave replication status

Click (here) to collapse or open

    1. > Db.printreplicationinfo ();
    2. Configured Oplog SIZE:47.6837158203125MB
    3. Log length start to End:1375secs (0.38hrs)
    4. Oplog first event Time:fri June 14:30:35 gmt+0800 (CST)
    5. Oplog last event Time:fri June 14:53:30 gmt+0800 (CST)
    6. Now:fri June 14:53:32 gmt+0800 (CST)
    7. >

4) View collection status

Click (here) to collapse or open

  1. > Db.printcollectionstats ();
  2. Foo
  3. {
  4. "NS": "Test.foo",
  5. "Count": 1,
  6. "Size": 56,
  7. "Avgobjsize": 56,
  8. "Storagesize": 4096,
  9. "Numextents": 1,
  10. "Nindexes": 1,
  11. "Lastextentsize": 4096,
  12. "Paddingfactor": 1,
  13. "Flags": 1,
  14. "Totalindexsize": 8176,
  15. "Indexsizes": {
  16. "_id_": 8176
  17. },
  18. "OK": 1
  19. }
  20. System.indexes
  21. {
  22. "NS": "Test.system.indexes",
  23. "Count": 1,
  24. "Size": 64,
  25. "Avgobjsize": 64,
  26. "Storagesize": 4096,
  27. "Numextents": 1,
  28. "Nindexes": 0,
  29. "Lastextentsize": 4096,
  30. "Paddingfactor": 1,
  31. "Flags": 0,
  32. "Totalindexsize": 0,
  33. "Indexsizes": {
  34. },
  35. "OK": 1
  36. }
  37. >

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

    1. RM mongodb/db/locat.*-RF

3) Start from library

Click (here) to collapse or open

    1. /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

  1. [Email protected] bin]#./mongo
  2. MongoDB Shell version:2.0.0
  3. Connecting To:test
  4. > Db.test.insert ({"Name": "Ymind", "Age": 8}); #在test数据库里插入数据
  5. >
  6. > Db.test.find ({"Name": "Ymind"}); #查询数据
  7. {"_id": ObjectId ("51c4187090db4e4751a3cd05″"), "Name": "Ymind", "Age": 8}
  8. > Db.test.find ({"Age": 8}); #查询数据
  9. {"_id": ObjectId ("51c4187090db4e4751a3cd05″"), "Name": "Ymind", "Age": 8}
  10. >
  11. >
  12. > db.test.update ({"Name": "Ymind"}, {"$set": {"Age": "5″}}"); #修改数据
  13. > Db.test.find ({"Name": "Ymind"});
  14. {"Name": "Ymind", "_id": ObjectId ("51c4187090db4e4751a3cd05″"), "Age": "5″}
  15. >
  16. > Db.test.remove ({"Name": "Ymind"}); #删除数据
  17. > Db.test.find ({"Name": "Ymind"});

CentOS Installation MongoDB

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.