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

Mongodb master-slave copy installation document

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

Share

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

Environment description

IPRole10.240.216.151master10.240.216.152slave (standby master) 10.240.216.153slave

Download and install mongodb

On 10.240.216.151 (main library)

# wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz

(if you can't download it properly, you can download it directly from the browser and upload it to the server.)

# tar-zxf mongodb-linux-x86_64-2.0.4.tgz-C / usr/local/

# mkdir-p / data/ {db/geomaster/,log}

# touch / data/log/geomaster.log

# cd / usr/local/

# mv mongodb-linux-x86_64-2.0.4 mongodb

# / usr/local/mongodb/bin/mongod-fork--port 4000-master-dbpath / data/db/geomaster/-logpath / data/log/geomaster.log--logappend launch the database

# kill-2 `ps-ef | grep mongod | grep-vgrep | awk'{print $2} 'shut down the database

On 10.240.216.152 and 10.240.216.153 (standby)

# wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz

(if you can't download it properly, you can download it directly from the browser and upload it to the server.)

# tar-zxf mongodb-linux-x86_64-2.0.4.tgz-C / usr/local/

# mkdir-p / data/ {db/geoslave/,log}

# touch / data/log/geosalve.log

# cd / usr/local/

# mv mongodb-linux-x86_64-2.0.4 mongodb

# / usr/local/mongodb/bin/mongod-- fork--port 4000-- slave-- source 10.240.216.151 slave-- dbpath / data/db/geoslave/--logpath / data/log/geosalve.log-- logappend-- nojournal launch database

# kill-2 `ps-ef | grep mongod | grep-vgrep | awk'{print $2} 'shut down the database

Verification

On the main library

# / usr/local/mongodb/bin/mongo127.0.0.1:4000

> db.runCommand ({"isMaster": 1})

{"ismaster": true, "maxBsonObjectSize": 16777216, "ok": 1}

On the repository.

# / usr/local/mongodb/bin/mongo127.0.0.1:4000

> db.runCommand ({"isMaster": 1})

{

"ismaster": 0

"info": "dead: data toostale halted replication"

"maxBsonObjectSize": 16777216

"ok": 1

}

> use local

Switched to db local

> db.sources.find ()

{"_ id": ObjectId ("55ebfa53ecf735439d6bafeb"), "host": "10.240.216.151host", "source": "main", "syncedTo": {"t": 1441528400000, "I": 1}}

Create libraries and tables on the main library and add a piece of data

> use mydb

Switched to db mydb

> db

Mydb

> db.mydb.user.insert ({"username": "zdh"})

> db.mydb.user.find ()

{"_ id": ObjectId ("55ebfc2d92e237c3cb30ef9a"), "username": "zdh"}

> show dbs

Local 6.2001953125GB

Mydb 0.203125GB

Check data synchronization on the slave library

> show dbs

Local 0.203125GB

Mydb 0.203125GB

> use mydb

Switched to db mydb

> db.mydb.user.find ()

{"_ id": ObjectId ("55ebfc2d92e237c3cb30ef9a"), "username": "zdh"}

The above output indicates OK

A very important defect in the above master-slave structure is that when you want to upgrade from the master library to the master library, you need to do a lot of work manually. The specific steps are as follows:

1. Stop one of the slave and restart as master (promote 10.240.216.152 as the main library)

# kill-2 `ps-ef | grep mongod | grep-vgrep | awk'{print $2}'`

# / usr/local/mongodb/bin/mongod-- fork--port 4000-- master-- dbpath / data/db/geoslave/-- logpath/data/log/geosalve.log-logappend

two。 If you cannot find the R & D modification code to point to the new master library IP, you can shut down the original master library and cut its IP to the new master library server (cut the IP scenario. In this case, the slave side does not need to restart the mongodb process, and the original master library will not be able to start, which is a problem)

# ifconfig bond0:1 10.240.216.151 netmask255.255.255.0 up

# arping-I bond0-c 3-s 10.240.216.15110.240.216.254

3. Point the slave library 10.240.216.153 to the new master library. You need to stop mongodb first, and then source points to the new master library to start (operate without IP)

# kill-2 `ps-ef | grep mongod | grep-vgrep | awk'{print $2}'`

# rm-f / data/db/geoslave/mongod.lock / data/db/geoslave/local.*

# / usr/local/mongodb/bin/mongod-- fork--port 4000-- slave-- source 10.240.216.152 slave-- dbpath / data/db/geoslave/--logpath / data/log/geosalve.log-- logappend-nojournal

# / usr/local/mongodb/bin/mongo127.0.0.1:4000

> use local

Switched to db local

> db.sources.find ()

{"_ id": ObjectId ("55ec04dadfd5d65f2f7fe404"), "host": "10.240.216.152 host 4000", "source": "main", "syncedTo": {"t": 1441531234000, "I": 1}}

In view of the above complex operation process, we make the following optimization

Execute on the main library 10.240.216.151

# / usr/local/mongodb/bin/mongod--fork-port 4000-dbpath / data/db/geomaster/-logpath/data/log/geomaster.log-logappend-master

Execute on slave library (slave master) 10.240.216.152

# / usr/local/mongodb/bin/mongod--fork-port 4000-master-slave-dbpath / data/db/geoslave/-logpath/data/log/geosalve.log-logappend

# / usr/local/mongodb/bin/mongo127.0.0.1:4000

> use local

Switched to db local

> db.sources.find ()

> db.sources.insert ({"host": "10.240.216.151pur4000"})

> db.sources.find ()

{"_ id": ObjectId ("55ec120751fd14c6b2f23a18"), "host": "10.240.216.151VR 4000"}

> db.sources.find () is about 30 seconds apart

{"_ id": ObjectId ("55ec120751fd14c6b2f23a18"), "host": "10.240.216.151host", "source": "main", "syncedTo": {"t": 1441534596000, "I": 1}}

Execute on slave library 10.240.216.153

# / usr/local/mongodb/bin/mongod-fork-port 4000-slave-dbpath / data/db/geomaster/-logpath/data/log/geosalve.log-logappend-nojournal

# / usr/local/mongodb/bin/mongo127.0.0.1:4000

> use local

Switched to db local

> db.sources.find ()

> db.sources.insert ({"host": "10.240.216.151pur4000"})

> db.sources.insert ({"host": "10.240.216.152pur4000"})

> db.sources.find ()

{"_ id": ObjectId ("55ec127a1924006f4e8b2e97"), "host": "10.240.216.151host", "source": "main", "syncedTo": {"t": 1441534586000, "I": 1}}

{"_ id": ObjectId ("55ec127e1924006f4e8b2e98"), "host": "10.240.216.152 host 4000", "source": "main", "syncedTo": {"t": 1441534593000, "I": 1}}

The above configuration is complete. It is realized that when the master library 10.240.216.151 dies, the standby master library 10.240.216.152 can directly provide external services without restarting the mongodb process, and the slave library does not need to make any changes.

Verification

On the main library 10.240.216.151

> show dbs

Local 6.2001953125GB

Mydb 0.203125GB

> use mydb

Switched to db mydb

> db.mydb.user.insert ({"username": "zhongguo", "age": "26"})

> db.mydb.user.find ()

{"_ id": ObjectId ("55ec12e4bab480b5485dd72e"), "username": "zhongguo", "age": "26"}

> db.mydb.user.insert ({"username": "zhongguo2", "age": "30"})

On the slave library (standby master) 10.240.216.152

> use mydb

Switched to db mydb

> db.mydb.user.find ()

{"_ id": ObjectId ("55ec12e4bab480b5485dd72e"), "username": "zhongguo", "age": "26"}

> db.mydb.user.insert ({"username": "finished", "time": "2015-09-06"})

> db.mydb.user.find ()

{"_ id": ObjectId ("55ec12e4bab480b5485dd72e"), "username": "zhongguo", "age": "26"}

{"_ id": ObjectId ("55ec133e51fd14c6b2f23a19"), "username": "finished", "time": "2015-09-06"}

> db.mydb.user.find ()

{"_ id": ObjectId ("55ec12e4bab480b5485dd72e"), "username": "zhongguo", "age": "26"}

{"_ id": ObjectId ("55ec133e51fd14c6b2f23a19"), "username": "finished", "time": "2015-09-06"}

{"_ id": ObjectId ("55ec136abab480b5485dd72f"), "username": "zhongguo2", "age": "30"}

On slave library 10.240.216.153

> use mydb

Switched to db mydb

> db.mydb.user.find ()

{"_ id": ObjectId ("55ec12e4bab480b5485dd72e"), "username": "zhongguo", "age": "26"}

> db.mydb.user.find ()

{"_ id": ObjectId ("55ec12e4bab480b5485dd72e"), "username": "zhongguo", "age": "26"}

{"_ id": ObjectId ("55ec133e51fd14c6b2f23a19"), "username": "finished", "time": "2015-09-06"}

> db.mydb.user.find ()

{"_ id": ObjectId ("55ec12e4bab480b5485dd72e"), "username": "zhongguo", "age": "26"}

{"_ id": ObjectId ("55ec133e51fd14c6b2f23a19"), "username": "finished", "time": "2015-09-06"}

{"_ id": ObjectId ("55ec136abab480b5485dd72f"), "username": "zhongguo2", "age": "30"}

The above results show that the standby slave library can take effect immediately, and we need to make sure that our writes are written from the master library.

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