您的位置 首页 golang

Golang + MongoDB ReplSet副本集

Golang + MongoDB ReplSet副本集

本文介绍MongoDB副本集,暂且按照3个节点来配置:A( PRIMARY )、B(secondary)、C(arbiter)

  1. 在集群每个节点上安装一份MongoDB;

  2. 配置副本集;

  3. 配置副本集的用户名、密码;

  4. 配置副本集的KeyFile安全鉴权;

  5. 配置开机启动;

  6. golang连接MongoDB副本集,并执行CRUD操作;

每个节点IP:

  • A节点: 11.11.11.11

  • B节点: 22.22.22.22

  • C节点: 33.33.33.33

第一步: 安装MongoDB

每个节点安装 mongodb

vi /etc/yum.repos.d/mongodb-enterprise.repo 

在mongodb-enterprise.repo文件中加入以下内容

然后运行以下命令

sudo yum install -y mongodb-enterprise 

配置mongod.conf

先创建以下目录

/test/db/mongodb

|–logs

|–keyfiles

|–data

再配置日志文件的路径、数据目录、副本集名称

> vi /etc/mongod.conf

systemLog:

path: /test/db/mongodb/logs/mongod.log #日志文件的路径

storage:

dbPath: /test/db/mongodb/data #数据目录

net:

port: 27017

bindIp: 11.11.11.11 #客户端通过此ip连接

replication:

replSetName: “rs0” #副本集名称

注意 : 数据目录、日志文件目录要将用户改为mongod

chown mongod:mongod -R /test/db/mongodb/data

chown mongod:mongod -R /test/db/mongodb/logs

分别启动每个节点:A、B、C节点

mongod --config /etc/mongod.conf 

第二步: 组建副本集

假设我们要将A结点作为 primary , B结点作为 secondary , C结点作为 arbiter

在A结点(11.11.11.11)这台机器上运行

mongo 

连接成功后,开始配置副本集:

> config = {

“_id”: “rs0”,

“members”: [

{

“_id”: 0,

“host”: “11.11.11.11:27017”

}

]

}

> rs.initiate(config) #初始化副本集

{ “ok” : 1 }

> rs.add({host: ‘22.22.22.22:27017’})

> rs.addArb(‘33.33.33.33:27017’)

> rs.status() #查看副本集状态,找到primary节点的IP

第三步: 创建帐户密码

副本集搭建成功后,需要给整个副本集创建帐户、密码

在A结点(primary)上,用客户端连接,创建用户权限(主节点,可以用 rs.status() 查看)

[root@servera ~]# mongo

rs0:PRIMARY> use admin

switched to db admin

#创建超级管理员:root

replSet:PRIMARY> db.createUser({

“user”: “root”,

“pwd”: “rootpwd”,

“roles”: [

{

“role”: “root”,

“db”: “admin”

}

]

})

Successfully added user: {

“user” : “root”,

“roles” : [

{

“role” : “root”,

“db” : “admin”

}

]

}

rs0:PRIMARY> use mytest #创建mytest数据库

switched to db mytest

#创建mytest数据库管理员

replSet:PRIMARY> db.createUser({

“user”: “mytest”,

“pwd”: “mytestpwd”,

“roles”: [

{

“role”: “dbOwner”,

“db”: “mytest”

}

]

})

Successfully added user: {

“user” : “mytest”,

“roles” : [

{

“role” : “dbOwner”,

“db” : “mytest”

}

]

}

第四步: 创建keyfile

创建key文件, 三个节点必须要用同一份keyfile,在一台机器生成,拷贝到另外两台,并且修改成 400 的文件属性。

结点A、结点B、结点C上都创建 /test/db/mongodb/keyfiles 目录, 然后在结点A上输入以下命令

openssl rand -base64 756 > /test/db/mongodb/keyfiles/keyfile
chmod 400 /test/db/mongodb/keyfiles/keyfile 

并且把这份keyfile同步到其他两个节点的 /test/db/mongodb/keyfiles 目录下;

关闭副本集的所有成员

先关闭结点B(secondary)上的mongod, 再关闭结点C(arbiter)上的mongod, 最后关闭结点A(primary)上的mongod

分别依次在B, C, A上运行mongo shell, 然后再输入以下命令来关闭mongod

use admin
db.shutdownServer() 

修改每个节点的配置文件mongo.conf中的security配置项:

security:
 keyFile: /test/db/mongodb/keyfiles/keyfile
 authorization: enabled 

注意 :三台机器都要修改mongod.conf。另外,keyfiles目录要将用户改为mongod

chown mongod:mongod -R /test/db/mongodb/keyfiles 

重新启动副本集

[root@servera ~]# mongod -f /etc/mongod.conf #机器A

[root@serverb ~]# mongod -f /etc/mongod.conf #机器B

[root@serverc ~]# mongod -f /etc/mongod.conf #机器C

第五步: 设置MongoDB开机启动

[root@servera ~]# systemctl enable mongod. service

[root@serverb ~]# systemctl enable mongod.service

[root@serverc ~]# systemctl enable mongod.service

第六步: Golang连接MongoDB

package main

import (
 "os"
"fmt"
"github.com/globalsign/mgo")func main() { // user, password, rs0分别为用户名、密码、副本集,请自行修改
URL := "mongodb://user:password@11.11.11.11,22.22.22.22/database?replicaSet=rs0"
session, err := mgo.Dial(URL) if err != nil {
fmt.Println(err.Error())
os.Exit(-1)
}
session.SetMode(mgo.Monotonic, true)
Conn := session.DB("")// 获得 Mongodb的连接后,再就可以进行各种CRUD啦
} 

本文由 shen100 原创, Golang中文社区 首发,现同步到头条。欢迎转载, 请在正文中标明原文链接及作者,谢谢。也欢迎来 Golang中文社区 学习、交流。

文章来源:智云一二三科技

文章标题:Golang + MongoDB ReplSet副本集

文章地址:https://www.zhihuclub.com/98811.shtml

关于作者: 智云科技

热门文章

评论已关闭

26条评论

  1. This type of test may be used to tell the difference between different types of soft tissue sarcoma 67 Zithromax 6 people, 66

  2. Shown is the mean sem for five experiments of the percentage of pixel density using the pixel density detected for E2 ERО± GST SHP incubated at 37 C as 100

  3. New Challenges in Psycho Oncology Research IV Cognition and cancer Conceptual and methodological issues and future directions

  4. Ideggyogy Sz 2007; 60 7 8 301 10 Tamoxifen pharmacokinetics depends on a multi pathway biotransformation Fig

  5. The primary analysis of investigator assessed progression free survival was conducted at a data cutoff date of August 20, 2017, after 318 patients had had disease progression or had died

  6. What s upp to every, as I am actually keewn of reading this blog s ppst to be updated regularly

  7. In a meta analysis of phase II III RCTs on ET versus combined strategies, combined ET, especially adding CDK4 6 showed an improvement in PFS as first line treatment in mBC as compared to ET alone 75

  8. He is next scheduled to appear in court on October 23, but Wollman expects a grand jury to return an indictment before then Germline TP53 and APC mutations result in Li Fraumeni syndrome and familial adenomatous polyposis, which are not associated with lobular carcinoma of the breast

  9. 2013 Emerging Role of Calcium Activated Potassium Channel in the Regulation of Cell Viability Following Potassium Ions Challenge in HEK293 Cells and Pharmacological Modulation Considering the individual CRB 65 score components, mental confusion was present in 8

  10. Inorganic mercury poisoning associated with skin lightening cosmetic products Using Boyden chamber assays we observed that only the double mutant showed a strong reduction in its invasive potential Fig

  11. My DH and I were TTC for about 5 months and nothing was happening I am 33 years old and thought that that might have something to do with it

  12. Contamination of our samples with strains N40 and 297 is impossible, since these strains are not present in the laboratory in which molecular analyses were carried out

  13. I doubt it though Following this concept, Maurya and Rana 17 recently reported on the diversification of macrocycles by carbohydrate derived building blocks

  14. Little is known about Tallis s early life and he probably never set foot in the Cotswolds, but as an inspiration to Vaughan Williams whose celebrated Fantasia on a Theme of Thomas Tallis was premiered in Gloucester Cathedral in 1910 he deserved his honorary place on the programme

  15. 17 The different effects of iPTH on osteoclasts may be caused by the different cell responses to iPTH under normal and inflammatory conditions, as it was difficult to absolutely mimic the in vivo inflammatory environment An anti- fungal antibiotic therapy

  16. However, the story you may tell yourself They forgot because they don t care or They never listen when I ask them to do things is what may get you increasingly upset

  17. Nicardipine is highly protein bound greater than 95 in human plasma over a wide concentration range

网站地图