Configure the Master and Slave modes for MongoDB

Source: Internet
Author: User

MongoDB is a scalable, high-performance, open-source, free-mode, document-oriented database.

It is not a commonly used RDBMS and belongs to the database category of the popular NoSql database.

It is not the same as a general key-value-based db because it has powerful functions.

It has implemented many functions similar to SQL, such as index, limit, skip, group ....

When the application data volume reaches the sea level, the traditional Mysql and Oracle cannot meet our performance requirements.

If you still need to query data, consider using MongoDB.

Installation is simple:

  1. Wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-1.2.4.tgz"

  2. Tar zxvf mongodb-linux-x86_64-1.2.4.tz

  3. Ln-s/opt/mongodb-linux-x86_64-1.2.4 // usr/local/mongodb

  4. Mkdir/opt/mongodb_data

MongoDB has 2 versions, the official recommendation of the use of mongodb-linux-x86_64-1.2.4.tgz, only in linux is the old system, mongodb can not be started, the use of "legacy static" version

MongoDB provides a simple Auth function. To enable authentication, you must first add users

  1. /Usr/local/mongodb/bin/mongo admin

  2. Db. addUser ('admin', 'manager ')

  3. Db. auth ('admin', 'manager ')

  4. Show dbs

  5. Use address

  6. Db. addUser ('mongodb ', 'test ')

Here, admin is a special database. Only after db. auth is passed on admin can you add permissions to other databases.

As shown above, we added a mongodb user to the address database.

In addition, if we want to enable the Master/Slave Mode and enable the Auth function, we need to add a repl user to the local database on the Master and Slave at the same time.

  1. Use local

  2. Db. addUser ('repl', 'replicase ')

Otherwise, when we start slave, an error is reported:

Replauthenticate: no user in local. system. users to use for authentication

Replauthenticate: can't authenticate to master server, user: repl

Then, we can start the Master and Slave respectively using the following script. Note that the-oplogSize option specifies the maximum size of log storage updated when the master synchronizes data to the slave. The default value is 64-bit, 1G max. If it is too small and slave does not keep up with it in time, data may be inconsistent.

  1. Nohup/usr/local/mongodb/bin/mongod run-Dbpath=/Data/mongodb_data/-Logpath=/Data/mongodb_data/mongodb. log -OplogSize = 10000 M-Logappend-auth-master &

  2. Nohup/usr/local/mongodb/bin/mongod run-Dbpath=/Opt/mongodb_data/-Logpath=/Opt/mongodb_data/mongodb. log-logappend-auth-slave-Source=192. 168.10.10: 27017 &

In python, you can use the pymongo module.

  1. FromPymongo. connectionImportConnection

  2. Connection = Connection ("127.0.0.1",27017)

  3. Db = connection. mail_addr

  4. Db. authenticate ('Mongodb','Test')

  5. ......

  6. Db. address. save (user_addr, safe =True)

Use save to save a record. If we have used find () to investigate a record, you can use save to update the record after modification.

If you want to implement insert if a record does not exist and update if it exists, you can use

    1. Db. foo. update ({'yy'}: 5, {'$ set': {'xx': 2 }},Upsert=True)

  • MongoDB has powerful functions. It can create indexes like Mysql, and supports multi-field indexes and unique indexes...

    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.