MongoDB master-slave mode and master-master mode

Source: Internet
Author: User

-- Master slave Experiment
-- Create a data directory
$ Mkdir/data/1
$ Mkdir/data/2
$ Mkdir/data/3

-- Start the MongoDB Database
$/Data/mongodb-linux-i686-1.6.0/bin/mongod -- Port 27020 -- dbpath/data/1 -- master &
$/Data/mongodb-linux-i686-1.6.0/bin/mongod -- Port 27021 -- dbpath/data/2 -- master &
$/Data/mongodb-linux-i686-1.6.0/bin/mongod -- Port 27022 -- dbpath/data/3 -- slave &

-- Start the client
$/Data/mongodb-linux-i686-1.6.0/bin/Mongo localhost: 27020
$/Data/mongodb-linux-i686-1.6.0/bin/Mongo localhost: 27020
> Use db1
> DB. Foo. insert ({X: 1 })
> DB. Foo. insert ({X: 2 })
> Exit
$ # Master 2
$/Data/mongodb-linux-i686-1.6.0/bin/Mongo localhost: 27021
> Use DB2
> DB. Foo. insert ({X: 999, note: "in DB2 "})
> Exit

$ # Configure slave
$/Data/mongodb-linux-i686-1.6.0/bin/Mongo localhost: 27022
> Use local
> DB. Sources. insert ({Host: "localhost: 27020 "})
> DB. Sources. insert ({Host: "localhost: 27021 "})
> DB. Sources. Find ()

> // Wait a little, still connected to slave

> Use db1
> DB. Foo. Count ()
2
> Use DB2
> DB. Foo. Find ()
{& Quot; _ id & quot;: objectid (& quot; 4b8ed00a1d42d47b3afa3c47 & quot;), & quot; X & quot;: 999, & quot; Note & quot;: & quot; in DB2 & quot "}
> DB. printslavereplicationinfo ()

 

 

========================================================== ========================================================== =====
-- Master experiment
-- Create a data directory
$ Mkdir/data/M1
$ Mkdir/data/m2

-- Start the server
$/Data/mongodb-linux-i686-1.6.0/bin/mongod -- Port 27017 -- dbpath/data/M1 -- master

$/Data/mongodb-linux-i686-1.6.0/bin/mongod -- slave -- master -- dbpath/data/slave -- port 10000 -- source localhost

-- Start the client
$/Data/mongodb-linux-i686-1.6.0/bin/Mongo localhost: 27017
> Z= connect ("localhost: 10000/test ")
> DB. Foo. insert ({X: 7 });
> Z. Foo. Find ()
> DB. Foo. Find ()

// Master and slave Data Synchronization

> DB. Foo. insert ({X: 81 });
> DB. Foo. Find ()
> Z. Foo. Find ()

// Master and slave Data Synchronization

> Z. Foo. insert ({X: 91 });
> DB. Foo. Find ()
> Z. Foo. Find ()

// The data is not synchronized here, And the DB cannot find data of x 9. Inconsistent with the log on http://www.mongodb.org/display/docs/master?master=replication

> Z. Foo. Remove ({X: 8 });
> DB. Foo. Find ()
> Z. Foo. Find ()

// If the data is not synchronized, the database returns the data of x 8. Inconsistent with the log on http://www.mongodb.org/display/docs/master?master=replication

 

[] Conclusion: The dB (master) can be synchronized to the slave, but the slave cannot be synchronized to the master. It is inconsistent with that in Doc.

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.