mongodb master-slave

 In the era of big data, the traditional relational database service must be able to solve more high concurrent read and write, and efficient mass data storage, high scalability and high availability of these problems. But because these issues Nosql born.

NOSQL these advantages:

Large amount of data , you can store large amounts of data through low-cost server, mysql easily get rid of the traditional single-table storage middleweight limit.

High scalability , NoSQL removed relational characteristic of a relational database, it is easy to scale, out of the past always criticized longitudinal extension.

High-performance , Nosql get the data through a simple key-value mode, very fast. There is NoSQL record level of Cache, Cache is a fine-grained, so many NoSQL on this level will be higher performance.

Flexible data model , NoSQL without prior establishment of field data to be stored, ready to store custom data formats. In a relational database, add or delete fields is a very troublesome thing. If a very large amount of data tables, increase the field it is simply a nightmare.

High availability , NoSQL in the case do not affect performance, you can easily achieve high availability architecture. By such mongodb mongos, mongo fragmentation can quickly configure the high availability configuration.

  In nosql database, most of the queries are key ways (key, value) of. MongoDB product is interposed between a relational database and non-relational databases, most of which are non-relational database as a relational database. Support similar object-oriented query language, most of the functionality can be achieved almost single-table queries similar to a relational database, but also support for data indexing. So this is very convenient, we can use sql operation MongoDB, migrated from relational databases, developers learn the cost will be greatly reduced. If we do sql API to the underlying layer of packaging, the basic development can not feel the difference mongodb and relational databases. Also MongoDB also claim to be able to quickly build a scalable, highly available distributed cluster, there are many online articles to build, when we build also seek to modify a lot of things, so put their real steps be recorded in a note. Step by step look at how we build this stuff.

A, mongodb single instance . This configuration is only suitable for simple use and development, production not used, because the single data service node hang the entire trailer, as shown below.

mongodb1

Although not manufactured, but this mode can be quickly set up to start, and can operate with mongodb database commands. Listed below are installed in the single node mongodb step linux

1, the establishment mongodb test folder

 

2, the installation package download mongodb

 

3, single-instance start mongodb

 

Output log is as follows, success!

[initandlisten] db version v2.4.6
……..
[initandlisten] waiting for connections on port 27017
[websvr] admin web console waiting for connections on port 28017

The default comes mongodb provides web access interface, the form can be accessed via IP + port.

http://192.168.0.1:28017/

mongodb2

二、主从模式。使用mysql数据库时大家广泛用到,采用双机备份后主节点挂掉了后从节点可以接替主机继续服务。所以这种模式比单节点的高可用性要好很多。

mongodb3

 

下面看一下怎么一步步搭建一个mongodb的主从复制节点:

  • 1、准备两台机器 192.168.0.1 和 192.168.0.2。 192.168.0.1 当作主节点, 192.168.0.2作为从节点
  • 2、分别下载mongodb安装程序包。在192.168.0.1上建立文件夹 /data/mongodbtest/master,192.168.0.2建立文件夹/data/mongodbtest/slave
  • 3、在192.168.0.1启动mongodb主节点程序。注意后面的这个 “ –master ”参数,标示主节点。

    mongod –dbpath /data/mongodbtest/master –master

输出日志如下,成功!

[initandlisten] MongoDB starting : pid=18285 port=27017 dbpath=/data/mongodbtest/master master=1
#日志显示主节点参数
[initandlisten] options: { dbpath: “/data/mongodbtest/master”, master: true }
……..
[initandlisten] waiting for connections on port 27017

4、在192.168.0.2启动mongodb从节点程序。关键配置,指定主节点ip地址和端口 –source 192.168.0.1:27017 和 标示从节点 –source 参数。

mongod –dbpath /data/mongodbtest/slave –slave –source 192.168.0.1:27017

输出日志如下,成功!

[initandlisten] MongoDB starting : pid=17888 port=27017 dbpath=/data/mongodbtest/slave slave=1
……..
#日志显示从节点参数
[initandlisten] options: { dbpath: “/data/mongodbtest/slave”, slave: true, source: “192.168.0.1:27017″ }
……..
[initandlisten] waiting for connections on port 27017
#日志显示从节点 从主节点同步复制数据
[replslave] repl: from host:192.168.0.1:27017

5、测试主从复制。

可以看到主机的同步日志

[initandlisten] connection accepted from 192.168.0.2:37285 #3 (2 connections now open)
[slaveTracking] update local.slaves query: { _id: ObjectId(’5284e6268ed115d6238bdb39′), config: { host: “192.168.0.2:35271″, upgradeNeeded: true }, ns: “local.oplog.$main” } update: { $set: { syncedTo: Timestamp 1384441570000|1 } } nscanned:1 nupdated:1 fastmod:1 keyUpdates:0 locks(micros) w:132015 132ms

检查从主机的数据。

mongo 127.0.0.1

查看当前数据库。

查询后数据已经同步过来了。再看看日志,发现从主机确实从主机同步了数据。

查看服务状态

到此主从结构的mongodb搭建好了。

故障转移测试,现在两台服务器如果主服务器挂掉了,从服务器可以正常运转吗?

    • a、先测试下从服务器可以当成主服务器吗,也就是往从服务器里写能够同步主服务器吗?

       

      在192.168.0.2上连接mongodb。

      可以看到 mongodb的从节点是不能提供写操作的,只能提供读操作。

b、如果从服务器挂掉,主服务器还可以提供服务。如果主服务器挂掉了从服务器能否自动变为可写。
测试一下!

先杀掉原来的mongodb主服务器。

测试从服务器能否可写。在192.168.0.2上连接mongodb测试。

看起来从服务器没有自动接替主服务器的功能,只有手工处理了!

停止从服务器,在原数据文件启动并添加主服务器标示。

mongod   --dbpath /data/mongodbtest/slave --master

Wait until successful start (a little longer). Connected to 192.168.0.2

 

success!

From multiple nodes . Now just a database server also provides a read-write and the machine will carry a bottleneck. I still remember the mysql where separate read and write it? 20% of the write into the main node, read into the 80% reduction from the node sharing the load on the server. But most applications are read-pressure operation brought a load from the node can not pressure, can become more nodes from a node. A main mongodb more from that can support? The answer is yes.

mongodb4

For testing, on 192.168.0.2 then create a folder / data / mongodbtest / slave1 As another slave server.
Start slave2 service,

mongod   --dbpath /data/mongodbtest/slave1 --slave  --port 27017 --source 192.168.0.1:27017。

After successfully initiate a connection test by mongodb:

Guess you like

Origin www.cnblogs.com/ajianboke/p/10930823.html