In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to build a high availability mongodb cluster, I believe many inexperienced people are helpless about this, for this reason this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.
In the era of big data, traditional relational databases must solve the problems of high concurrency, efficient storage of mass data, high scalability and high availability in order to provide higher services. But because of these problems Nosql was born.
NOSQL has these advantages:
Large amount of data, can be stored through cheap servers, easily get rid of the traditional mysql single table storage limit.
High scalability, Nosql removes the relational characteristics of relational databases, and it is easy to scale horizontally, getting rid of the criticism of vertical expansion in the past.
High performance, Nosql gets data in a simple key-value way, very fast. NoSQL Cache is a record-level, fine-grained Cache, so NoSQL performance at this level is much higher.
Flexible data model, NoSQL does not need to establish fields for the data to be stored in advance, and can store custom data formats at any time. In relational databases, adding and deleting fields is a very troublesome thing. Adding fields is a nightmare for very large tables.
Highly available, NoSQL can easily implement a highly available architecture without affecting performance. For example, mongodb can quickly configure high availability configurations through mongos and mongo fragmentation.
In nosql databases, most queries are key-value pairs. MongoDB is an intermediate product between a relational database and a non-relational database, the most relational of which is non-relational. Support similar to object-oriented query language, almost can achieve similar to relational database single table query most of the functions, but also support the establishment of indexes on data. So this is very convenient, we can use sql operation MongoDB, from the relational database migration, developers learning costs will be greatly reduced. If you encapsulate the underlying SQL API, you can basically feel no difference between mongodb and relational database. MongoDB also claims to be able to quickly build a highly available and scalable distributed cluster. There are many articles on the Internet. When we build it, we still need to find and modify many things, so we record our actual combat steps as a memo. Let's see how to build this thing step by step.
MongoDB is a single instance. This configuration is only suitable for simple development, but not for production, because a single node hangs up the entire data business, as shown in the following figure.
Although not production-ready, this pattern can be quickly set up and started, and can be used to manipulate databases with mongodb commands. Here are the steps to install single-node mongodb under linux
1. Create mongodb test folder
#Store the entire mongodb file
mkdir -p /data/mongodbtest/single
#Store mongodb data files
mkdir -p /data/mongodbtest/single/data
#Enter mongodb folder
cd /data/mongodbtest/single
2. Download the mongodb installation package
wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.6.tgz
#Unzip the downloaded zip
tar xvzf mongodb-linux-x86_64-2.4.6.tgz
#Enter the mongodb program execution folder
cd mongodb-linux-x86_64-2.4.6/bin/
3. Start a single instance mongodb
mongod --dbpath /data/mongodbtest/single/data
Output log 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
Mongodb provides web access interface by default, which can be accessed through IP + port.
http://192.168.0.1:28017/
二、主从模式。使用mysql数据库时大家广泛用到,采用双机备份后主节点挂掉了后从节点可以接替主机继续服务。所以这种模式比单节点的高可用性要好很多。
下面看一下怎么一步步搭建一个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、测试主从复制。
在主节点上连接到终端:
mongo 127.0.0.1
#建立test 数据库。
use test;
往testdb表插入数据。
> db.testdb.insert({"test1":"testval1"})
查询testdb数据看看是否成功。
> db.testdb.find();
{ "_id" : ObjectId("5284e5cb1f4eb215b2ecc463"), "test1" : "testval1" }
可以看到主机的同步日志
[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
查看当前数据库。
> show dbs;
local 0.203125GB
test 0.203125GB
use test;
db.testdb.find();
{ "_id" : ObjectId("5284e5cb1f4eb215b2ecc463"), "test1" : "testval1" }
查询后数据已经同步过来了。再看看日志,发现从主机确实从主机同步了数据。
Thu Nov 14 23:05:13 [replslave] repl: checkpoint applied 15 operations
Thu Nov 14 23:05:13 [replslave] repl: syncedTo: Nov 14 23:08:10 5284e75a:1
查看服务状态
> db.printReplicationInfo();
this is a slave, printing slave replication info.
source: 192.168.0.1:27017
syncedTo: Sun Nov 17 2013 16:04:02 GMT+0800 (CST)
= -54 secs ago (-0.01hrs)
到此主从结构的mongodb搭建好了。
故障转移测试,现在两台服务器如果主服务器挂掉了,从服务器可以正常运转吗?
a、先测试下从服务器可以当成主服务器吗,也就是往从服务器里写能够同步主服务器吗?
在192.168.0.2上连接mongodb。
mongo 127.0.0.1:27017
> db.testdb.insert({"test3":"testval3"});
not master
可以看到 mongodb的从节点是不能提供写操作的,只能提供读操作。
b、如果从服务器挂掉,主服务器还可以提供服务。如果主服务器挂掉了从服务器能否自动变为可写。
测试一下!
先杀掉原来的mongodb主服务器。
kill -3 `ps -ef|grep mongod|grep -v grep|awk '{print $2}'`
测试从服务器能否可写。在192.168.0.2上连接mongodb测试。
> db.testdb.insert({"test3":"testval3"});
not master
看起来从服务器没有自动接替主服务器的功能,只有手工处理了!
停止从服务器,在原数据文件启动并添加主服务器标示。
mongod --dbpath /data/mongodbtest/slave --master
等到启动成功(时间有点长)。在192.168.0.2 上 连接
mongo 192.168.0.2:27017
。
> db.testdb.find();
{ "_id" : ObjectId("5288629e9b0318be4b20bd4c"), "test1" : "testval1" }
{ "_id" : ObjectId("528862d69b0318be4b20bd4d"), "test2" : "testval2" }
成功!
多个从节点。现在只是一个数据库服务器又提供写又提供读,机器承载会出现瓶颈。大家还记得mysql里的读写分离吗?把20%的写放到主节点,80%的读放到从节点分摊了减少了服务器的负载。但是大部分应用都是读操作带来的压力,一个从节点压力负载不了,可以把一个从节点变成多个节点。那mongodb的一主多从可以支持吗?答案是肯定的。
For testing purposes, create a folder/data/mongodbtest/slave1 on 192.168.0.2 as another slave server.
Start slave2 service,
mongod --dbpath /data/mongodbtest/slave1 --slave --port 27017 --source 192.168.0.1:27017。
After successful startup through mongodb connection test:
> db.testdb.find();
{ "_id" : ObjectId("5288629e9b0318be4b20bd4c"), "test1" : "testval1" }
{ "_id" : ObjectId("528862d69b0318be4b20bd4d"), "test2" : "testval2" }
Is this master-slave replication system very robust? Actually, it is not... See these questions?
Can the connection be switched automatically when the master node hangs? Manual switching is currently required.
How to solve the problem of excessive write pressure on the master node?
The data on each slave node is a full copy of the database. Will the pressure on the slave node be too large?
Even if routing access policy is implemented on slave nodes, can it be automatically extended?
After reading the above, do you know how to build a highly available mongodb cluster? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.