#MongoDB 集群配置
mongodb在生产环境中必然是以集群的形式存在的,不然不安全。mongodb集群主要是一主多从的形式存在。主节点master负责与客户端进行交互,处理读写操作。
从节点slave主要负责从主节点读取数据并保存到本地。
## mongodb主从配置
新建两台服务器,安装mongodb。
192.168.226.129 master 将192.168.226.129机器作为master节点来配置。
192.168.226.130 slave 将192.168.226.130机器作为slave从节点配置。
### 主节点配置
```shell script
[root@localhost data]# mkdir -p /data/master
[root@localhost data]# cd /home/mongodb/bin
[root@localhost bin]# nohup mongod --dbpath /data/master --master &
[3] 3282
[root@localhost bin]# nohup: ignoring input and appending output to `nohup.out'
[root@localhost bin]# cat nohup.out
...
2020-04-27T03:33:42.456-0700 I CONTROL [initandlisten] allocator: tcmalloc
2020-04-27T03:33:42.456-0700 I CONTROL [initandlisten] options: { master: true, storage: { dbPath: "/data/master" } }
2020-04-27T03:33:42.459-0700 I JOURNAL [durability] Durability thread started
2020-04-27T03:33:42.461-0700 I JOURNAL [journal writer] Journal writer thread started
2020-04-27T03:33:42.463-0700 I NETWORK [initandlisten] waiting for connections on port 27017
```
### 从节点配置
```shell script
[root@moggledb bin]# mkdir /data/slave
[root@localhost data]# cd /home/mongodb/bin
[root@moggledb bin]# nohup mongod --dbpath /data/slave --slave --source=192.168.226.129:27017 &
[root@moggledb bin]# nohup: ignoring input and appending output to `nohup.out'
[root@moggledb bin]# cat nohup.out
2020-04-30T04:26:14.029-0700 I CONTROL [initandlisten] allocator: tcmalloc
2020-04-30T04:26:14.029-0700 I CONTROL [initandlisten] options: { slave: true, source: "192.168.226.129:27017", storage: { dbPath: "/data/slave" } }
2020-04-30T04:26:14.033-0700 I JOURNAL [journal writer] Journal writer thread started
2020-04-30T04:26:14.035-0700 I NETWORK [initandlisten] waiting for connections on port 27017
2020-04-30T04:26:15.036-0700 I REPL [replslave] repl: syncing from host:192.168.226.129:27017
2020-04-30T04:26:20.054-0700 I REPL [replslave] repl: sleep 1 sec before next pass
2020-04-30T04:26:21.055-0700 I REPL [replslave] repl: syncing from host:192.168.226.129:27017
```
至此主从节点配置完毕,测试主从节点是否配置成功。
打开从节点slave 的nohup.out日志.`tail -f nohup.out`
然后在主节点中插入一条数据:
```shell script
> show dbs
local 1.078GB
> use book
switched to db book
> db.coll.insert({"name":"java","price":"100"})
WriteResult({ "nInserted" : 1 })
```
查看从节点日志:
```shell script
Cleanup...
2020-04-30T04:31:47.888-0700 I JOURNAL [replslave] removeJournalFiles
2020-04-30T04:31:47.889-0700 I JOURNAL [replslave] journalCleanup...
2020-04-30T04:31:47.889-0700 I JOURNAL [replslave] removeJournalFiles
2020-04-30T04:31:47.894-0700 I REPL [replslave] resync: cloning database book to get an initial copy
2020-04-30T04:31:47.912-0700 I INDEX [replslave] allocating new ns file /data/slave/book.ns, filling with zeroes...
2020-04-30T04:31:47.988-0700 I STORAGE [FileAllocator] allocating new datafile /data/slave/book.0, filling with zeroes...
2020-04-30T04:31:47.989-0700 I STORAGE [FileAllocator] done allocating datafile /data/slave/book.0, size: 64MB, took 0.001 secs
2020-04-30T04:31:47.997-0700 I INDEX [replslave] build index on: book.coll properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "book.coll" }
2020-04-30T04:31:47.997-0700 I INDEX [replslave] building index using bulk method
2020-04-30T04:31:47.997-0700 I INDEX [replslave] build index done. scanned 1 total records. 0 secs
2020-04-30T04:31:47.998-0700 I STORAGE [replslave] copying indexes for: { name: "coll", options: {} }
```
这样的主从配置如果主节点宕机的话,从节点不会自动转为主节点,会一直等待主节点重启。主节点宕机期间,服务是不可用的。
mongodb学习笔记--集群配置(一)
原创
©著作权归作者所有:来自51CTO博客作者wx61de8d4d02ae8的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
ZooKeeper-3.4.6学习笔记(一)集群配置
ZooKeeper-3.4.6学习笔记(一)集群配置
zookeeper-3.4.6 -
MongoDB学习笔记~Mongo集群和副本集
回到目录 一些概
服务器 副本集 优先级