Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

NoSQL-- Mongodb installation uses replica set backup restore

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Mongodb is an open source database system based on distributed file storage written by C++, which is between relational database and non-relational database. In the case of high load, adding multiple nodes to ensure server performance is designed to provide scalable high-performance storage for web applications; mongodb stores the data as a document, composed of key-value pairs (key= > value), similar to json objects.

Official website: https://www.mongodb.com/

Application:

CERN, the famous Institute of Particle Physics and CERN, uses MongoDB for data from the large Hadron Collider.

Craiglist, billions of records archived using MongoDB

Shutterfly, Internet-based social and personal publishing services, requirements for various persistent data storage using MongoDB

Intuit, a software and service provider for small businesses and individuals, uses MongoDB to track user data for small businesses

Mongodb installation

Www.apelearn.com/bbs/data/download/mongodb-org-3.0.tar.gz / / rpm package, you can use it.

The official source only supports 64-bit systems. If you install the epel extension source, you can install the mongodb2.4 version. This time we install version 3.0.

Vim / etc/yum.repos.d/mongodb-org-3.0.repo / / add the following

[mongodb-org-3.0]

Name=MongoDB Repository

Baseurl= http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/

Gpgcheck=0

Enabled=1

Installation: yum install-y mongodb-org

Edit: vim / etc/mongod.conf

Fork: true

PidFilePath: / var/run/mongodb/mongod.pid

Bind_ip: 192.168.1.1192.168.1.2192.168.1.3 # left blank means listening for all

Note: delete the comments at the beginning of the # after these two lines, otherwise there will be a problem when you restart

Echo never > / sys/kernel/mm/transparent_hugepage/enabled

Echo never > / sys/kernel/mm/transparent_hugepage/defrag

Vim / etc/security/limits.conf / / join

Mongod soft nofile 64000

Mongod hard nofile 64000

Mongod soft nproc 32000

Mongod hard nproc 32000

Startup: service mongod start # starts slowly because it is writing data / var/lib/mongo

If startup fails, you can start it in the following way: mongod-f / etc/mongod.conf

The use of mongodb

Mongodb connection:

Mongo / / directly enter the shell of mongo

Mongo-- port 27018 / / designated listening port to enter

Mongo-- host 127.0.0.1 / / Connect to the remote mongo

Mongo-uusername-ppasswd / / specify username and password when setting authentication, similar to mysql

Mongodb user Management:

Use admin / / switch to admin library. Admin and test are the two libraries that come with them by default.

Use teng12 / / switch to the teng12 library. It will be created automatically if it doesn't exist.

Show users / / View all users under the current library

Db.system.users.find () / / lists all users and needs to switch to the admin library

Db.createUser ({user: "admin", pwd: "123456", roles: [{role:'dbOwner',db:'userdb'}]}) / / create a user

Db.dropUser ('admin') / / Delete user

User role: http://bbs.51cto.com/thread-1146654-1.html

Mongodb Library Management:

Db.stats () / / View the information of the current library

Db.version () / / View version

Show teng12 / / View the library, which is empty, so there is no userdb. You can see it by creating a collection.

Db.createCollection ('clo1') / / create a collection clo1, created under the current library

Db.dropDatabase () / / Delete the current library. To delete a library, you must switch to that library.

Db.serverStatus () / / View the status of the mongodb server

Mongodb Collection:

Db.createCollection ("mycol", {capped: true, autoIndexID: true, size: 6142800, max: 10000})

/ / Syntax: db.createCollection (name,options)

Name: the name of the collection. Options is optional, which is used to configure the parameters of the collection, as follows

AutoindexID true/false (optional) if true, the default value for the automatically created index _ id field is false

Capped true/false (optional) enables capped collections if true. Reaching its maximum will overwrite the earliest entry

Size (optional) specifies the maximum size byte capped collection. If the cap is true, then you also need to specify this field. Unit B

Max (optional) specifies the maximum number of files allowed in the capped collection

Mongodb data Management:

Db.Account.insert ({AccountID:1,UserName: "123", password:" 123456 "}) / / create a collection

Db.Account.update ({AccountID:1}, {"$set": {"Age": 20}}) / / Update

Show collections / / View the collection, or use show tables

Db.Account.find () / / View all documents

Db.Account.drop () / / Delete all documents, that is, delete the collection

Db.printCollectionStats () / / then check the collection status

Db.Account.find ({AccountID:1}) / / query based on criteria

Db.Account.remove ({AccountID:1}) / / delete according to condition

A copy of the collection

Earlier versions used one master, one slave, similar to mysql, but slave is read-only in this architecture. After a master outage, the master cannot be automatically switched to master.

At present, it has been changed from master slave to replica set, which is one master (primary) and multi-slave (secondary, read-only), and supports switching between weights and slaves. An arbiter role can be established in this architecture, which is only responsible for adjudication and does not store data. In this architecture, the read and write data are all on the master. To achieve load balancing, you need to specify the target server of the slave database manually.

Primary

Secondary

Secondary

192.168.1.1

192.168.1.2

192.168.1.3

Edit: restart three machines after vim / etc/mongod.conf #

Replication:##oplog size oplogSizeMB: 20 replica # replication set name replSetName: teng

Connect to the master and run the command mongo on the master

> use admin

> config= {_ id: "teng", members: [{_ id:0,host: "192.168.1.1config= 27017"}, {_ id:1,host: "192.168.1.2VR 27017"}, {_ id:2,host: "192.168.1.3VR 27017"}]}

> rs.initiate (config)

> rs.add ("192.168.1.2")

> rs.add ("192.168.1.3")

Rs.status () / / View status

If the status of the two slaves is "stateStr": "STARTUP", you need to do the following

> var config=config= {_ id: "teng", members: [{_ id:0,host: "192.168.1.1var config=config= 27017"}, {_ id:1,host: "192.168.1.2VR 27017"}, {_ id:2,host: "192.168.1.3VR 27017"}]}

> rs.reconfig (config)

If you look at rs.status () again at this time, you will find that it has changed from the state of to SECONDARY.

Build libraries and collections on the Lord

> use mydb

> db.acc.insert ({AccountID:1,UserName: "123", password:" 123456 "})

> show dbs

View from above

> show dbs

If there is an error Error: listDatabases failed: {"note": "from execCommand", "ok": 0, "errmsg": "not master"}, you need to execute

> rs.slaveok ()

Change weight impersonation master downtime:

By default, the weights of the three machines are all 1. If any one of them is set to be higher than the others, the machine will immediately switch to the primary role, so we preset the weights of the three machines as follows: 1.

Execute on the Lord

Cfg = rs.conf ()

Cfg.members [0] .priority = 3

Cfg.members [1] .priority = 2

Cfg.members [2] .priority = 1

Rs.reconfig (cfg)

In that case, the second node will be the candidate primary node.

Main execution: iptables-I INPUT-p tcp-- dport 27017-j DROP

Mongodb backup reply

Backup specified library: mongodump-h ip-d dbname-o dir / /-o specify where to backup, it is a directory

Back up all libraries: mongodump-h ip-o dir / / without database dbname, back up all libraries

Backup specified collection: mongodump-d mydb-c testc-o / tmp/testc / /-c specify the name of the collection

Export the specified collection: mongoexport-d mydb-c testc-o / tmp/testc.json / /-o followed by a file name

Restore specified library: name of mongorestore-d mydb dir/-d recovery library. Dir is the directory where the library was backed up.

Restore all libraries: mongorestore-- drop dir/-- drop is optional, which means that the previous data is deleted before recovery, and is not recommended.

Restore collection: mongorestore-d mydb-c testc dir/mydb/testc.bson / /-c collection name, path to the bson file

Import collection: mongoimport-d mydb-c testc-- file / tmp/testc.json

-

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report