mongdb副本集搭建

发布时间:2020-08-08 11:51:02 作者:贺子_DBA时代
来源:ITPUB博客 阅读:242

一:安装mongodb 常用的有两种方法:我采用的是方法2:

方法1 安装包方式,直接下载.tgz文件,然后解压即可:

https://www.mongodb.com/download-center/enterprise # 下载需要的版本

下载percona for mongodb

https://www.percona.com/downloads/percona-server-mongodb-LATEST/tar -zxvf mongodb-linux-x86_64-3.0.6.tgz # 解压mv mongodb-linux-x86_64-3.0.6/ /usr/bin/mongodb/bin/mongod # 将解压包拷贝到指定目录

这种启动方式如下:

/usr/bin/mongodb/bin/mongod -f /etc/mongod.conf

方法2 yum 安装,然后默认配置文件在/etc/mongod.conf,这样安装的mongodb

的相关命令都在/usr/bin下面的!

安装yum源(percona自己的yum源)

# yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm

yum list | grep mongo #选择合适版本

[root@beijing-fuli-hadoop-02 bin]# yum install Percona-Server-MongoDB-34-mongos.x86_64

这种方法的启动方式:

service mongod start

二:生成 keyfile

[root@beijing-fuli-hadoop-02 mongodb]# openssl rand -base64 756 >/data/mongodb/config/mongodb.key

三:编写配置文件

1.注意文件的空格和大小写都会导致启动失败

2.如果配置了配置了clusterAuthMode,那么一定要提前生成keyfile

3.关于节点的优先级的设置,具有较高Priority的Secondary相对于较低Priority的Secondary

会更早的发起选举,也更容易成为新的Primary节点,但是即使优先级低的Secondary

也可以在段时间内被选举为Primary节点,重点来了,这种情况发生的时候副本集就会继续

进行选举,直到可用的最高优先级Secondary被选举为Primary,所以当你想让特定的

Secondary节点成为主的时候,你才有必要配置不同的Primary,否则你只需要配置成一样的

Priority(例如都是1即可)

4.关于vote的设置:从mongodb 3.2开始,无投票权的成员必须配置Priority为0,priority大于

0的成员其vote必须配置为1

四:如果你想用非root来启动mongodb,那么就创建 mongodb的账号和属组

[root@beijing-fuli-hadoop-02 init.d]# groupadd -g 666 mongod

[root@beijing-fuli-hadoop-02 init.d]# useradd -m -s /bin/bash -g mongod -u 666 mongod

五:启动数据库:三个节点都启动

[mongod@beijing-fuli-hadoop-02 db]$ /usr/bin/mongodb/bin/mongod -f /etc/mongod.conf

about to fork child process, waiting until server is ready for connections.

forked process: 89234

child process started successfully, parent exiting

六:转为复制集架构

> use admin # 切换到 admin 数据库switched to db admin # 初始化副本集,副本集名称为 liuhe_rs ,第一个成员为自己本身,然后他会被选举为主> rs.initiate({_id:'liuhe_rs',members: [{ _id: 0 , host: "10.9.21.178:27017"},{ _id: 1 , host: "10.9.21.179:27017"},{ _id: 2 , host: "10.9.21.114:27017"}]}) { "ok" : 1 }

或者如下:

>config={_id:'liuhe_rs',members: [{ _id: 0 , host: "10.9.21.178:27017"},{ _id: 1 , host: "10.9.21.179:27017"},{ _id: 2 , host: "10.9.21.114:27017"}]}

>rs.initiate(config)

七:启动报错:

7.1,参数indexBuildRetry不能在副本集中配置

[root@beijing-fuli-hadoop-02 mongodb]# /usr/bin/mongodb/bin/mongod -f /etc/mongod.conf

2019-11-11T22:36:15.551+0800 F CONTROL [main] Failed global initialization: BadValue: replication.replSetName is not allowed when storage.indexBuildRetry is specified

报错内容:

Failed global initialization: BadValue: replication.replSetName is not allowed when storage.indexBuildRetry is specified

storage 参数 indexBuildRetry 不能与 副本集共存,当开启 副本集的时候,就需要将indexBuildRetry 参数注释。官网链接

Changed in version 4.0: The setting storage.indexBuildRetry cannot be used in conjunction withreplication.replSetName.

7.2.启动报错 error number 100

启动的时候会在数据目录下生成相应的初始化库以及特定的问题,所以

要保证这个目录是空的

[mongod@beijing-fuli-hadoop-02 ~]$ /usr/bin/mongodb/bin/mongod -f /etc/mongod.conf

about to fork child process, waiting until server is ready for connections.

forked process: 89082

ERROR: child process failed, exited with error number 100

To see additional information in this output, start without the "--fork" option.

可能是因为 你的 数据库目录下有别的文件,清空即可!

7.3 启动报错:error number 14

[mongod@beijing-fuli-hadoop-04 log]$ /usr/bin/mongodb/bin/mongod -f /etc/mongod.conf

about to fork child process, waiting until server is ready for connections.

forked process: 100524

ERROR: child process failed, exited with error number 14

To see additional information in this output, start without the "--fork" option.

查看日志发现如下:

2019-11-16T16:51:37.938+0800 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted

原来是之前用root账号启动的,生成了一个root用户的mongodb-27017.sock文件,然后又用mongod

用户启动,结果没有权限操作,删除这个文件即可,然后用mongod用户启动会自动生成一个

属于mongod的文件,如下:

[root@beijing-fuli-hadoop-02 tmp]# ll mongodb-27017.sock

srwx------ 1 mongod mongod 0 Nov 16 17:12 mongodb-27017.sock

那么mongodb-27017.sock这个文件是做什么的?

服务器与本地客户端进行通信的Unix套接字文件。

启动时会打开。

默认的存储路径是/tmp/mongodb-<port>.sock

八:mongodb的基本维护操作命令:

8.1:查看mongodb 副本集的状态

MongoDB Enterprise liuhe_rs:PRIMARY> rs.status()

8.2:允许secondary节点可以写

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

第一种方法:db.getMongo().setSlaveOk();#从库执行,并且是临时启作用,

第二种方法:rs.slaveOk(); #从库执行,并且是临时启作用,

上面的两种方法都是临时起作用的,下次再通过mongo进入实例的时候,查询仍然会报错,为此可以通过下列方式

[mongod@beijing-fuli-hadoop-04 ~]$ vi .mongorc.js

rs.slaveOk();

这样的话以后每次通过mongo命令进入都可以查询了

8.3.查看当前mongodb连接的那个库

> use liuwenhe

switched to db liuwenhe

> db.stats()

{

"db" : "liuwenhe",

"collections" : 1,

"views" : 0,

"objects" : 1,

"avgObjSize" : 48,

"dataSize" : 48,

"storageSize" : 16384,

"numExtents" : 0,

"indexes" : 1,

"indexSize" : 16384,

"fsUsedSize" : 18297913344,

"fsTotalSize" : 75949613056,

"ok" : 1

}

8.4查看副本集从库状态信息(延迟、成员等)

MongoDB Enterprise liuhe_rs:PRIMARY> rs.printSlaveReplicationInfo()

source: 10.9.21.179:27017

syncedTo: Sat Nov 16 2019 21:56:29 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

source: 10.9.21.114:27017

syncedTo: Sat Nov 16 2019 21:56:29 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

如上所示,有两个从库,分别是10.9.21.179、10.9.21.114,然后同步到了主库的oplogs

Sat Nov 16 2019 21:56:29 的时间点了,没有延迟;

8.5:查看副本集的oplog信息:

MongoDB Enterprise liuhe_rs:PRIMARY> rs.printReplicationInfo()

configured oplog size: 51200MB

log length start to end: 424168secs (117.82hrs)

oplog first event time: Tue Nov 12 2019 00:10:41 GMT+0800 (CST)

oplog last event time: Sat Nov 16 2019 22:00:09 GMT+0800 (CST)

now: Sat Nov 16 2019 22:00:16 GMT+0800 (CST)

8.6查看某个库下的用户:

MongoDB Enterprise liuhe_rs:PRIMARY> use admin

switched to db admin

MongoDB Enterprise liuhe_rs:PRIMARY> show users

九:创建管理员用户

use admin

db.createUser(

{

user: "liuwenhe",

pwd: "liuwenhe",

roles: [ { role: "root", db: "admin" } ]

}

)

MongoDB Enterprise liuhe_rs:PRIMARY> db.auth('liuwenhe','liuwenhe')

登录数据库:

mongo -u liuwenhe -p liuwenhe --authenticationDatabase admin

创建普通账号:

db.createUser({user:'user', pwd:'123456', roles:[ {role:'readWrite', db:'liuwenhe'}]})

推荐阅读:
  1. MongDB 副本集
  2. Docker搭建MongoDB 4.0副本集

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mongdb 搭建 副本

上一篇:云服务器管理平台 批量管理程序

下一篇:数商云汽车后市场电商平台解决方案

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》