• 测试环境

    操作系统:CentOS 7.2 最小化安装

    主服务器IP地址:192.168.197.21 master-node

    从服务器IP地址:192.168.197.22 slave-node

    关闭selinux,关闭防火墙。

    Mongodb版本:mongodb-linux-x86_64-3.4.10.tgz

  • 上传mongodb包到/software目录下,然后解压缩该包

    [root@localhost software]# tar -zxvf mongodb-linux-x86_64-3.4.10.tgz

  • 移动解压缩之后的文件至/usr/local/mongodb目录

    [root@localhost software]# mv mongodb-linux-x86_64-3.4.10 /usr/local/mongodb

  • 在mongodb下面创建data目录和logs目录,用于存放data和logs

    [root@localhost local]# mkdir -p /usr/local/mongodb/data

    [root@localhost local]# mkdir -p /usr/local/mongodb/logs

  • 增加环境变量,并使环境变量生效

    [root@localhost data]# vi /etc/profile

    export PATH=$PATH:/usr/local/mongodb/bin

    [root@localhost data]# source /etc/profile

  • 创建mongodb.conf文件并写入一下内容

    [root@localhost data]# touch /usr/local/mongodb/bin/mongodb.conf

    [root@localhost data]# vi /usr/local/mongodb/bin/mongodb.conf

  • 主服务器配置

    [root@master-node software]# more /usr/local/mongodb/bin/mongodb.conf

    port=27017

    dbpath= /usr/local/mongodb/data

    logpath= /usr/local/mongodb/logs/log.log

    logappend=true

    fork=true

    maxConns=100

    noauth=true

    journal=true

    storageEngine=wiredTiger

    bind_ip = 192.168.197.21

    master=true

  • 从服务器配置

    [root@slave-node software]# more /usr/local/mongodb/bin/mongodb.conf

    port=27017

    dbpath= /usr/local/mongodb/data

    logpath= /usr/local/mongodb/logs/log.log

    logappend=true

    fork=true

    maxConns=100

    noauth=true

    slave=true

    journal=true

    storageEngine=wiredTiger

    bind_ip = 192.168.197.22

    source=192.168.197.21:27017

  • 使用配置文件启动两台服务器上的数据库

    mongod --config /usr/local/mongodb/bin/mongodb.conf

  • 在主服务器上登陆数据库创建testa数据库,并写入20条数据

    [root@master-node software]# mongo 192.168.197.21:27017

    MongoDB shell version v3.4.10

    connecting to: 192.168.197.21:27017

    MongoDB server version: 3.4.10

    Server has startup warnings:

    2019-02-10T21:38:04.484-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

    2019-02-10T21:38:04.484-0500 I CONTROL [initandlisten]

    2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten]

    2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

    2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten]

    2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

    2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten]

    > use testa

    switched to db testa

    > function add(){var i = 0;for(;i<20;i++){db.persons.insert({"nameA":"li"+i})}}

    > add()

    > db.persons.find()

    { "_id" : ObjectId("5c61272206144917c514f1a3"), "nameA" : "li0" }

    { "_id" : ObjectId("5c61272206144917c514f1a4"), "nameA" : "li1" }

    { "_id" : ObjectId("5c61272206144917c514f1a5"), "nameA" : "li2" }

    { "_id" : ObjectId("5c61272206144917c514f1a6"), "nameA" : "li3" }

    { "_id" : ObjectId("5c61272206144917c514f1a7"), "nameA" : "li4" }

    { "_id" : ObjectId("5c61272206144917c514f1a8"), "nameA" : "li5" }

    { "_id" : ObjectId("5c61272206144917c514f1a9"), "nameA" : "li6" }

    { "_id" : ObjectId("5c61272206144917c514f1aa"), "nameA" : "li7" }

    { "_id" : ObjectId("5c61272206144917c514f1ab"), "nameA" : "li8" }

    { "_id" : ObjectId("5c61272206144917c514f1ac"), "nameA" : "li9" }

    { "_id" : ObjectId("5c61272206144917c514f1ad"), "nameA" : "li10" }

    { "_id" : ObjectId("5c61272206144917c514f1ae"), "nameA" : "li11" }

    { "_id" : ObjectId("5c61272206144917c514f1af"), "nameA" : "li12" }

    { "_id" : ObjectId("5c61272206144917c514f1b0"), "nameA" : "li13" }

    { "_id" : ObjectId("5c61272206144917c514f1b1"), "nameA" : "li14" }

    { "_id" : ObjectId("5c61272206144917c514f1b2"), "nameA" : "li15" }

    { "_id" : ObjectId("5c61272206144917c514f1b3"), "nameA" : "li16" }

    { "_id" : ObjectId("5c61272206144917c514f1b4"), "nameA" : "li17" }

    { "_id" : ObjectId("5c61272206144917c514f1b5"), "nameA" : "li18" }

    { "_id" : ObjectId("5c61272206144917c514f1b6"), "nameA" : "li19" }

  • 登陆从服务器的数据库查看数据是否同步过来

    [root@slave-node software]# mongo 192.168.197.22:27017

    MongoDB shell version v3.4.10

    connecting to: 192.168.197.22:27017

    MongoDB server version: 3.4.10

    Server has startup warnings:

    2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

    2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten]

    2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten]

    2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

    2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten]

    2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

    2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'

    2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten]

    > show dbs

    2019-02-11T02:43:26.999-0500 E QUERY [thread1] Error: listDatabases failed:{

    "ok" : 0,

    "errmsg" : "not master and slaveOk=false",

    "code" : 13435,

    "codeName" : "NotMasterNoSlaveOk"

    } :

    _getErrorWithCode@src/mongo/shell/utils.js:25:13

    Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1

    shellHelper.show@src/mongo/shell/utils.js:781:19

    shellHelper@src/mongo/shell/utils.js:671:15

    @(shellhelp2):1:1

    > rs.slaveOk()

    > show dbs

    admin 0.000GB

    local 0.000GB

    master_slave 0.000GB

    testa 0.000GB

    > use testa

    switched to db testa

    > db.persons.find()

    { "_id" : ObjectId("5c61272206144917c514f1a3"), "nameA" : "li0" }

    { "_id" : ObjectId("5c61272206144917c514f1a4"), "nameA" : "li1" }

    { "_id" : ObjectId("5c61272206144917c514f1a5"), "nameA" : "li2" }

    { "_id" : ObjectId("5c61272206144917c514f1a6"), "nameA" : "li3" }

    { "_id" : ObjectId("5c61272206144917c514f1a7"), "nameA" : "li4" }

    { "_id" : ObjectId("5c61272206144917c514f1a8"), "nameA" : "li5" }

    { "_id" : ObjectId("5c61272206144917c514f1a9"), "nameA" : "li6" }

    { "_id" : ObjectId("5c61272206144917c514f1aa"), "nameA" : "li7" }

    { "_id" : ObjectId("5c61272206144917c514f1ab"), "nameA" : "li8" }

    { "_id" : ObjectId("5c61272206144917c514f1ac"), "nameA" : "li9" }

    { "_id" : ObjectId("5c61272206144917c514f1ad"), "nameA" : "li10" }

    { "_id" : ObjectId("5c61272206144917c514f1ae"), "nameA" : "li11" }

    { "_id" : ObjectId("5c61272206144917c514f1af"), "nameA" : "li12" }

    { "_id" : ObjectId("5c61272206144917c514f1b0"), "nameA" : "li13" }

    { "_id" : ObjectId("5c61272206144917c514f1b1"), "nameA" : "li14" }

    { "_id" : ObjectId("5c61272206144917c514f1b2"), "nameA" : "li15" }

    { "_id" : ObjectId("5c61272206144917c514f1b3"), "nameA" : "li16" }

    { "_id" : ObjectId("5c61272206144917c514f1b4"), "nameA" : "li17" }

    { "_id" : ObjectId("5c61272206144917c514f1b5"), "nameA" : "li18" }

    { "_id" : ObjectId("5c61272206144917c514f1b6"), "nameA" : "li19" }

  • 关于报错

    如果在slave-node节点上的数据库中查看,有报错:"errmsg" : "not master and slaveOk=false"!!!

    首先这是正常的,因为SECONDARY是不允许读写的, 在写多读少的应用中,使用Replica Sets来实现读写分离。通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力,Primary只承担写操作。对于replica set 中的secondary 节点默认是不可读的。

    解决办法:在slave-node节点数据库中执行"rs.slaveOk();"命令即可

    条数据,说明mongodb的主从复制环境已经成功了!当配置完主从服务器后,一但主服务器上的数据发生变化,从服务器也会发生变化。

  • 设置mongodb的开机启动。

    首先要在系统服务目录下创建启动文件mongodb.servicer然后写入一下内容

    [root@localhost system]# cd /lib/systemd/system

    [root@localhost system]# vi mongodb.service

    [Unit]

    Description=mongodb

    After=network.target remote-fs.target nss-lookup.target

    [Service]

    Type=forking

    ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf

    ExecReload=/bin/kill -s HUP $MAINPID

    ExecStop=/usr/local/mongodb/bin/mongod --shutdown

    PrivateTmp=true

    [Install]

    WantedBy=multi-user.target

    注册到开机启动

    [root@localhost system]# systemctl enable mongodb.service

    Created symlink from /etc/systemd/system/multi-user.target.wants/mongodb.service to /usr/lib/systemd/system/mongodb.service.

    启动

    systemctl start mongodb.service

    关闭

    systemctl stop mongodb.service

mongodb集群配置主从模式的更多相关文章

  1. Docker:docker搭建redis一主多从集群(配置哨兵模式)

    角色 实例IP 实例端口 宿主机IP 宿主机端口 master 172.19.0.2 6382 192.168.1.200 6382 slave01 172.19.0.3 6383 192.168.1 ...

  2. 实例:Mongodb集群配置过程

    最近因为一些项目公司开始采用Mongodb做为大量的数据存储,通过网络上大量的资源自己已经掌握一套可行的Mongodb集群配置过程,Mongodb具有无规则存储.大数据存储.多平台支持.强大的扩展插件 ...

  3. mongodb集群配置及备份恢复

    Mongodb安装: 编辑/etc/yum.repos.d/mongodb.repo,添加以下: [MongoDB] name=MongoDB Repository baseurl=https://r ...

  4. MongoDB集群配置笔记一

    MongoDB 的部署方案有单机部署.复本集(主备)部署.分片部署.复本集与分片混合部署.混合的部署方式如图: 分片集群的构造 (1)mongos :数据路由,和客户端打交道的模块.mongos本身没 ...

  5. MariaDB集群配置(主从和多主)

    1.mariadb主从 主从多用于网站架构,因为主从的同步机制是异步的,数据的同步有一定延迟,也就是说有可能会造成数据的丢失,但是性能比较好,因此网站大多数用的是主从架构的数据库,读写分离必须基于主从 ...

  6. Mongodb集群配置(sharding with replica set)

    转自:http://blog.csdn.net/zhangzhaokun/article/details/6269514 前言 最近在研习MongoDB集群,找到一个不错的例子,加了几句,按照自己的理 ...

  7. MongoDB集群配置笔记二(实战)

    单台mongodb配置文件: dbpath=/opt/mongodb/data logpath=/opt/mongodb/logs/mongodb.log logappend=true fork=tr ...

  8. redis sentinel 集群配置-主从切换

    1.配置redis master,redis slave(配置具体操作见上文http://www.cnblogs.com/wangchaozhi/p/5140469.html). redis mast ...

  9. mongodb集群配置副本集

    测试环境 操作系统:CentOS 7.2 最小化安装 主服务器IP地址:192.168.197.21 mongo01 从服务器IP地址:192.168.197.22 mongo02 从服务器IP地址: ...

随机推荐

  1. PHP中buffer的认知

    buffer其实就是缓冲区,一个内存地址空间,只要用于存储数据区域. <?php echo 1; 运行这段代码,浏览器会显示1,其实中间会经历一个buffer(数据1先到buffer,当buff ...

  2. acm

    给定一组数字,一组有9个数字,将这9个数字填写到3*3的九宫格内:使得横,竖,斜对角一条线上的三个数字之和相等:如果无解则打印无解: #include <iostream> #includ ...

  3. guxh的python笔记六:类的属性

    1,私有属性 class Foo: def __init__(self, x): self.x = x 类的属性在实例化之后是可以更改的: f = Foo(1) print(f.x) # 1 f.x ...

  4. zend cache使用

    require_once 'Zend/Cache.php';//引用文件$frontendOptions = array( 'lifeTime' => 60, // cache lifetime ...

  5. QComboBox样式

    https://blog.csdn.net/lys211/article/details/43956979https://blog.csdn.net/x_nazgul/article/details/ ...

  6. 黏包-黏包的成因、解决方式及struct模块初识、文件的上传和下载

    黏包: 同时执行多条命令之后,得到的结果很可能只有一部分,在执行其他命令的时候又接收到之前执行的另外一部分结果,这种显现就是黏包. 只有TCP协议中才会产生黏包,UDP协议中不会有黏包(udp协议中数 ...

  7. 微服务-注册与发现-zookeeper bydasn

    目录 一.微服务注册的概述 二.zookeeper2.1 zookeeper安装启动2.2 zookeeper集群搭建2.3 zkcli操作 一.微服务注册概述 在微服务中,有这么一个东西叫服务注册表 ...

  8. Mac下安装Apache

    没错,这一篇又是因为头头给我安排的任务得出来的总结. 本身Mac是有自带的Apache,但是对并发量有限制,这个可以在系统的配置参数里面看,所以本人决定重新安装一个,来,请按照下面的流程来走: 一.下 ...

  9. mysql装完计算机管理里面没mysql服务怎么解决(转)

    mysql装完计算机管理里面没mysql服务怎么解决 2017年07月04日 09:32:51 XC_Echizen 阅读数:7335 标签: mysql计算机mysql服务找不到mysql服务 更多 ...

  10. MySQL-8.0.15在Win10和Ubuntu上安装&使用

    一.Windows环境下安装: 1.下载MySQL压缩包 官网地址:https://dev.mysql.com/downloads/mysql/ 点击直接下载: 2.解压到本地目录,并添加一个配置文件 ...