MongoDB installation and master-slave configuration in Linux system

Source: Internet
Author: User
Tags mongodb mongodb version

MongoDB, master and slave configuration

A MongoDB installation

The code is as follows Copy Code

[Root@zabbix_server src]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.0.tgz
[Root@zabbix_server src]# tar xzvf mongodb-linux-x86_64-2.0.0.tgz
[Root@zabbix_server src]# MV Mongodb-linux-x86_64-2.0.0/usr/local/mongodb

[Root@zabbix_server src]# Mkdir/usr/local/mongodb/data
[Root@zabbix_server src]# Touch/usr/local/mongodb/logs

[Root@zabbix_server bin]#/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data--logpath=/usr/local/ Mongodb/logs--logappend--port=27017--fork

[Root@zabbix_server bin]#./mongo
MongoDB Shell version:2.0.0
Connecting To:test
> Use test;
Switched to DB test
> Exit
Bye

[Root@zabbix_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 the error

The code is as follows Copy Code

-bash:/usr/local/mongodb/bin/mongod:cannot Execute binary file

Explain your server and MongoDB version does not correspond, if the server is 64 bits, download x86_64 MongoDB, if the server is 32 bits, download i686 mongodb/
Two MongoDB master and slave configuration


1) Experimental environment

Main: 192.168.0.14

From: 192.168.0.64
2) Time synchronization

Two machines do time synchronization

The code is as follows Copy Code
[Root@zabbix_server src]# ntpdate time.windows.com


3) Start Service

The code is as follows Copy Code

master:192.168.0.14

Start command

The code is as follows Copy Code

/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

The code is as follows Copy Code
/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

Insert data in our Lord
Test 1:

1 inserting data on the main library

The code is as follows Copy Code
[Root@cacti 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 View the data from the above (the data that has been inserted in the Lord indicates the master-slave success)

The code is as follows Copy Code
[Root@zabbix_server bin]#./mongo
MongoDB Shell version:2.0.0
Connecting To:test
> Db.foo.find ({"id": 123456})
{"_id": ObjectId ("51c3f4b21399022afd992f39"), "id": 123456, "name": "Lizonggang"}
>

1 inserting data on the main library

The code is as follows Copy Code


> 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

The code is as follows Copy Code
[Root@zabbix_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 2013 14:59:10 gmt+0800 (CST)
= 9 secs ago (0hrs)
>
> Db.printslavereplicationinfo ();
source:192.168.0.14:27017
Syncedto:fri June 2013 14:59:30 gmt+0800 (CST)
= secs ago (0hrs)
>

Test 3:

In this state, slave is not available to insert data.

The code is as follows Copy Code
> Db.test.save ({title: "This is slave test!"})
Not master


5 Statements of some query states

1) Query Library

The code is as follows Copy Code

> Show DBS;
Elaindb 0.0625GB
Local 0.125GB
Test 0.0625GB

2 Query the address of the main library from the library

5

The code is as follows Copy Code
> 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

The code is as follows Copy Code
> Db.printreplicationinfo ();
Configured Oplog SIZE:47.6837158203125MB
Log length start to End:1375secs (0.38hrs)
Oplog Time:fri June 2013 14:30:35 gmt+0800 (CST)
Oplog Last event Time:fri June 2013 14:53:30 gmt+0800 (CST)
Now:fri June 2013 14:53:32 gmt+0800 (CST)
>

4) View collection status

The code is as follows Copy Code
> 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 Lord hangs out in the application?

This situation will need to be stopped from the library and put the start command from the library to the main library.

1) stop from the library

The code is as follows Copy Code

Kill-9 Process Number

2) Delete the Locat file

The code is as follows Copy Code
RM mongodb/db/locat.*-RF

3 Boot from Library

The code is as follows Copy Code

/usr/local/mongodb/bin/mongod--master--dbpath=/usr/local/mongodb/data--logpath=/usr/local/mongodb/logs-- Logappend--port=27017--fork

7) MongoDB, delete, change, check

The code is as follows Copy Code
[root@cacti 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 ("51C4187090DB4E4751A3CD ' Name ': ' Ymind ', ' Age ': 8}
> Db.test.find ({' Age ': 8}); #查询数据
{"_id": ObjectId ("51c4187090db4e4751 A3cd05 ")," 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"});


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.