MongoDB to configure master and slave mode with permission authentication

Source: Internet
Author: User
Tags auth mongodb

Turn from: http://www.tech126.com/mongodb-master-slave/

MongoDB is an extensible, high-performance, open source, schema free, document-oriented database

It's not our usual RDBMS, it's a recent popular "NOSQL" database category

It's not the same as the general Key-value DB, because it's very powerful.

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

When the amount of data in the application reaches a mass level, the traditional mysql,oracle can not meet our performance requirements

And for the data there are certain query requirements, you may consider the use of MongoDB

The installation is simple:

Collapse Expand Replication Code

wget "Http://downloads.mongodb.org/linux/mongodb-linux-x86_64-1.2.4.tgz"

Tar zxvf Mongodb-linux-x86_64-1.2.4.tz

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

Mkdir/opt/mongodb_data

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

MongoDB provides a simple auth function, if you want to turn on authentication, you need to first increase the user before starting

Collapse Expand Replication Code

/usr/local/mongodb/bin/mongo Admin

Db.adduser (' admin ', ' manager ')

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

Show DBS

Use Address

Db.adduser (' MongoDB ', ' test ')

In which, admin is a special database, only after admin Db.auth Pass, can give other database to add permissions

As above, we have added a MongoDB user to the address database

Also , if we want to enable the Master/slave mode and turn on the Auth feature, we need to add a REPL user to the local database on both master and slave

collapse Expand Replication Code

Use Local

db.adduser (' Repl ', ' Replication ')

Otherwise, when we start slave, we will get an error:

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

Replauthenticate:can ' t authenticate to master server, USER:REPL

Then, we use the following script to start both master and slave, note the –oplogsize option, it specifies the master to slave synchronization, update log Save the maximum size, the default 64-bit machine, the maximum 1G, if too small, slave not in time to keep up, There may be inconsistencies in the data.

Collapse Expand Replication Code

Nohup/usr/local/mongodb/bin/mongod run–dbpath=/data/mongodb_data/–logpath=/data/mongodb_data/mongodb.log– Oplogsize=10000m–logappend–auth–master &

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

Collapse Expand Replication Code

From pymongo.connection Import Connection

Connection = connection ("127.0.0.1", 27017)

db = Connection.mail_addr

Db.authenticate (' MongoDB ', ' test ')

......

Db.address.save (User_addr,safe=true)

Use Save to save a record, and if we have used find () to investigate a record, then we can use save to update the operation.

If you want to implement an insert if a record does not exist, you can use the

Collapse Expand Replication Code

Db.foo.update ({' yy '}:5,{' $set ': {' xx ': 2}},upsert=true)

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.