mongodb安装 分片集群

一、安装
1.制作yum文件

# vim /etc/yum.repos.d/mongodb.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc 

2.yum 安装

# yum makecache && yum install -y mongodb-org

3.启动

# systemctl restart mongod
# systemctl enable mongod

4.连接,使用mongo即可连接到数据库,但是会有警告,提示我们禁用大内存页面,解决方法如下
(1)方法一:临时解决,机器重启失效

# echo never >>  /sys/kernel/mm/transparent_hugepage/enabled
# echo never >>  /sys/kernel/mm/transparent_hugepage/defrag
# systemctl restart mongod

(2)方法二:永久解决

step1 编辑 /etc/default/grub,在GRUB_CMDLINE_LINUX加入选项 transparent_hugepage=never
# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet transparent_hugepage=never"
GRUB_DISABLE_RECOVERY="true"

step2 重新生成grub配置文件
# grub2-mkconfig -o /boot/grub2/grub.cfg
# grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg

step3 重启你的系统
# reboot

二、用户管理
1.创建用户admin

> use admin					#切换到admin库
switched to db admin		
> db.createUser( { user: "admin", customData: {describtion: "superuser"}, pwd: "admin123", roles:[ {role: "root", db: "admin"}]})
> db.system.users.find()	#列出所有用户,需要切换到admin库下
> show users;				#查看当前库下所有用户
> db.createUser({user:"test", pwd: "test123", roles:[{role: 'read', db: "admin"}]})
> db.dropUser('test')		#删除用户

2.添加完用户之后需要重启mongo,在重启前在启动文件/usr/lib/systemd/system/mongod.service 中OPTIONS= 后面添加 --auth

# cat /usr/lib/systemd/system/mongod.service
Environment="OPTIONS=-f /etc/mongod.conf --auth"
# systemctl daemon-reload
# systemctl restart mongod
# mongo --host 127.0.0.1 --port  27017 -u admin -p 'admin123' --authenticationDatabase "admin"	 #连接数据库

三、创建集合

> use db3		#创建数据库
> db.createCollection("mycol", {capped: true, autoIndexId:true, size:6142800, max:10000})	#创建集合
# 解释:
# capped true/false(可选)如果为true则启用封顶集合。封顶集合时固定集合的大小,当他达到其最大大小时会自动覆盖最早的条目,如果您指定为true需要指定尺寸参数
# autoIndexId true/false(可选)如果为true,自动创建索引_id字段的默认值为false
# size:指定最大大小字节封顶集合。如果封顶为true,还需要指定这个字段。单位为B
# max:指定封顶集合允许在文件的最大数量
> show collections;		#查看集合
> db.Account.insert({AccountID:2, username:"zhangsan", password:"zhangsan123"})		#如果集合不存在,则会创建集合并插入数据,此时Account 集合不存在则会创建
> db.Account.insert({AccountID:1, username:"xiaoming", password:"xiaoming123"})
> db.Account.update({AccountID:1}, {"$set":{"Age":20}})		#当AccountID=1时,再添加一个字段Age并且Age=20

> db.Account.find()		#查看数据
{ "_id" : ObjectId("5cac9febf40482b03949a35b"), "AccountID" : 2, "username" : "zhangsan", "password" : "zhangsan123" }
{ "_id" : ObjectId("5caca05df40482b03949a35c"), "AccountID" : 1, "username" : "xiaoming", "password" : "xiaoming123", "Age" : 20 }
> db.Account.find({AccountID:2})	#根据条件查找
{ "_id" : ObjectId("5cac9febf40482b03949a35b"), "AccountID" : 2, "username" : "zhangsan", "password" : "zhangsan123" }

> db.Account.find({AccountID:1})		#根据条件删除
> db.Account.find()
{ "_id" : ObjectId("5cac9febf40482b03949a35b"), "AccountID" : 2, "username" : "zhangsan", "password" : "zhangsan123" }
> db.Account.drop()						#删除所有文档,即删除集合
true
> show tables;							#查看集合
mycol
> db.mycol.insert({'id': 1, 'name': 'z3', pwd:'z3123'})
WriteResult({ "nInserted" : 1 })
> db.mycol.find()
{ "_id" : ObjectId("5caca2f8f62fd0e0effa3a27"), "id" : 1, "name" : "z3", "pwd" : "z3123" }
> db.printCollectionStats()				#查看集合状态

三、副本集
1.ip分配: 192.168.19.101 192.168.19.102 1982.168.19.103
2.修改配置文件/etc/mongod,监听本机ip 以及配置replication

# cat /etc/mongod.conf |grep -A 2 'net:'
net:
  port: 27017
  bindIp: 127.0.0.1,192.168.19.101  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

# cat /etc/mongod.conf |grep -A 2 'repli'
replication:
  oplogSizeMB: 20		#oplog 文件大小,可以不用配置
  replSetName: sxg		#副本集名称

3.在其中一台机器进行配置,则这台机器会会变成主

> use admin
> config={_id:"sxg", members:[{_id:101, host:"192.168.19.101:27017"},{_id:102, host:"192.168.19.102:27017"},{_id:103, host:"192.168.19.103:27017"}]}
> rs.initiate(config)		#配置后等一会会发现当前机器的状态变为PRIMARY
sxg:PRIMARY> rs.status()		

四、测试副本集
1.在从节点上是没有查看等操作的, 若想操作执行命令 rs.slaveOk()即可

sxg:SECONDARY> show dbs
2019-04-10T21:49:50.226+0800 E QUERY    [js] Error: listDatabases failed:{
	"operationTime" : Timestamp(1554904188, 1),
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1554904188, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:139:1
shellHelper.show@src/mongo/shell/utils.js:882:13
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1


sxg:SECONDARY> rs.slaveOk()
sxg:SECONDARY> show dbs
admin   0.000GB
config  0.000GB
db1     0.000GB
db2     0.000GB
db3     0.000GB
local   0.000GB

2.修改权重:若主节点(192.168.19.101)宕机了,则会从19.102 或19.103中选择一个作为主节点,当再次启动19.101时,主从关系并不会改变

sxg:PRIMARY> rs.config()		#查看权重 发现个节点的priority 均为1


#修改权重,将主节点重启,然后在新的主节点上操作
sxg:PRIMARY> cfg=rs.config()
sxg:PRIMARY> cfg.members[0].priority=3
sxg:PRIMARY> cfg.members[1].priority=2
sxg:PRIMARY> cfg.members[2].priority=1
#等一会后,发现主节点会变到权重最大的那个节点

五、mongo分片
1.机器分配,三台机器 A :192.168.19.101 B: 192.168.19.102 C: 192.168.19.103
A机器:mongos 、config server、副本集1主节点、副本集2仲裁、 副本集3从节点
B机器:mongos 、config server、副本集1从节点、副本集2主节点、副本集3仲裁
C机器:mongos 、config server、副本集1仲裁、 副本集2从节点、副本集3主节点
端口分配:mongos 20000、config 21000 副本集1 27001 副本集2 27002、副本集3 27003
三台机器全部关闭firewalld服务和selinunx,或者增加相应端口规则

2.创建相关目录,在三台机器上创建各个角色所需要的目录 三台机器都要执行

# mkdir -p /data/mongodb/mongos/log
# mkdir -p /data/mongodb/config/{data,log}
# mkdir -p /data/mongodb/shard1/{data,log}
# mkdir -p /data/mongodb/shard2/{data,log}
# mkdir -p /data/mongodb/shard3/{data,log}

3.config server的配置,三台机器都需要操作,将bind_ip改为当前主机ip

# mkdir /etc/mongod
# cat /etc/mongod/config.conf
pidfilepath = /var/run/mongodb/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/configsrv.log
logappend = true
bind_ip = 192.168.19.101			### 改为当前主机ip
port = 21000
fork = true
configsvr = true    #declare this is a config db of a cluster
replSet = configs   #replication name
maxConns = 20000    #max connections

# mongod -f /etc/mongod/config.conf 			#启动config server
# ps aux|grep mong								#查看进程
mongod     1453  0.7  1.5 1478004 61352 ?       Sl   21:41   0:29 /usr/bin/mongod -f /etc/mongod.conf
root       1722  1.6  1.2 1153476 48872 ?       Sl   22:41   0:03 mongod -f /etc/mongod/config.conf

# 初始化副本集,任意一台机器都可以
# mongo --host 192.168.19.101 --port 21000
> use admin
> config = {_id:"configs", members:[{_id:101, host:"192.168.19.101:21000"},{_id:102, host:"192.168.19.102:21000"},{_id:103, host:"192.168.19.103:21000"}]}
> rs.initiate(config)
configs:SECONDARY> rs.status()
configs:PRIMARY> 		#等一会会发现状态变为PRIMARY

4.分片操作

###############   以下是第一个分片的配置   ##############
# cat /etc/mongod/shard1.conf 
pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 192.168.19.101		#改为当前主机ip
port = 27001
fork = true
replSet = shard1	# 副本集名称
shardsvr = true	# declare this is a shard db of a cluster
maxConns = 20000

# 启动,三个节点均需要启动
# mongod -f /etc/mongod/shard1.conf
# mongo --host 192.168.19.101 --port 27001		#连接并设置副本集,注意登录不能在仲裁节点登录
> config = {_id:"shard1",members:[{_id:101,host:"192.168.19.101:27001"},{_id:102,host:"192.168.19.102:27001"},{_id:103,host:"192.168.19.103:27001",arbiterOnly:true}]}
> rs.initiate(config)
shard1:PRIMARY> 			# 等一会状态会变为PRIMARY

###############   以下是第二个分片的配置   ##############
# cat /etc/mongod/shard2.conf 
pidfilepath = /var/run/mongodb/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 192.168.19.102			### 修改为当前主机ip
port = 27002
fork = true
replSet = shard2	# 副本集名称
shardsvr = true	# declare this is a shard db of a cluster
maxConns = 20000

# 启动,三个节点均需要启动
# mongod -f /etc/mongod/shard2.conf 
# mongo --host 192.168.19.102 --port 27002		#连接并设置副本集,注意登录不能在仲裁节点登录
> use admin
switched to db admin
> config = {_id:"shard2", members:[{_id:101, host:"192.168.19.101:27002",arbiterOnly:true}, {_id:102, host:"192.168.19.102:27002"},{_id:103, host:"192.168.19.103:27002"}]}
shard2:PRIMARY> 			# 等一会状态会变为PRIMARY

###############   以下是第三个分片的配置   ##############
# cat /etc/mongod/shard3.conf
pidfilepath = /var/run/mongodb/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 192.168.19.103
port = 27003
fork = true
replSet = shard3	# 副本集名称
shardsvr = true	# declare this is a shard db of a cluster
maxConns = 20000


# 启动,三个节点均需要启动
# mongod -f /etc/mongod/shard3.conf 
# mongo --host 192.168.19.103 --port 27003  	#连接并设置副本集,注意登录不能在仲裁节点登录
> use admin
switched to db admin
> config = {_id:"shard3", members:[{_id:101,host:"192.168.19.101:27003"},{_id:102,host:"192.168.19.102:27003",arbiterOnly:true},{_id:103,host:"192.168.19.103:27003"}]}
> rs.initiate(config)
shard3:PRIMARY>	 	# 等一会状态会变为PRIMARY

5.mongos配置

# cat /etc/mongod/mongs.conf
pidfilepath = /var/run/mongodb/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 192.168.19.101
port = 20000
fork = true
configdb = configs/192.168.19.101:21000,192.168.19.102:21000,192.168.19.103:21000  #监听的配置服务器,只能有1个或3个,configs为配置服务器的副本集名称
maxConns = 20000

#启动,任意一台机器
# mongos -f /etc/mongod/mongs.conf 
# mongo --host 192.168.19.101 --port 20000
# 把所有的分片和路由器串联
mongos> sh.addShard("shard1/192.168.19.101:27001,192.168.19.102:27001,192.168.19.103:27001")
mongos> sh.addShard("shard2/192.168.19.101:27002,192.168.19.102:27002,192.168.19.103:27002")
mongos> sh.addShard("shard3/192.168.19.101:27003,192.168.19.102:27003,192.168.19.103:27003")
mongos> sh.status()		#查看状态

注意:
1.分片时将本身的mongo关闭 对分片没影响 # systemctl stop mongod
2.mongodb本身的配置文件(/etc/mongod.conf)中replication不打开也没影响

六、keepalived + haproxy 实现mongs的高可用
在使用过程中,我们需要对外提供一个ip所以我们使用haproxy进行代理, 并对haproxy做高可用,提供一个对外流动ip,ip分配情况如下,关于keepalived与haproxy的安装见博客:https://blog.csdn.net/sun_xuegang/article/details/86526402

192.168.19.90     haproxy keepalived(master)
192.168.19.90     haproxy keepalived(backup)
192.168.19.110    vip

关于haproxy的配置,最后几行配置如下

# cat /etc/haproxy/haproxy.cfg |tail -12
frontend  mongo_frontend
    bind *:20000
    mode tcp
    option tcplog            #tcp日志格式
    default_backend mongo_backend

backend mongo_backend
    mode tcp
    balance source
    server mongo01 192.168.19.101:20000  check inter 2000 fall 3 weight 30
    server mongo02 192.168.19.102:20000  check inter 2000 fall 3 weight 30
    server mongo03 192.168.19.103:20000  check inter 2000 fall 3 weight 30

启动haproxy 和 keepalived 后, 在任意一台mongo机器上执行,是能够实现功能的

# mongo --host 192.168.19.110 --port 20000
MongoDB shell version v4.0.9
connecting to: mongodb://192.168.19.110:20000/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("97b482c2-3426-4bfd-9434-8427088249ce") }
MongoDB server version: 4.0.9
Server has startup warnings: 
2019-04-15T23:35:20.049+0800 I CONTROL  [main] 
2019-04-15T23:35:20.049+0800 I CONTROL  [main] ** WARNING: Access control is not enabled for the database.
2019-04-15T23:35:20.049+0800 I CONTROL  [main] **          Read and write access to data and configuration is unrestricted.
2019-04-15T23:35:20.049+0800 I CONTROL  [main] ** WARNING: You are running this process as the root user, which is not recommended.
2019-04-15T23:35:20.049+0800 I CONTROL  [main] 
mongos> 

七、keepalived 实现mongs的高可用
在配置文件中 关于bind_ip 均写为0.0.0.0 可以实现只用keepalived绑定一个vip,由于上面的文章写的是bind_ip 为各主机ip,导致只使用keepalived时,使用mongo --host 192.168.19.110 --host 20000 不能连接到mongos所以使用haproxy做了一层代理

八、测试分片

mongos> use admin
mongos> db.runCommand( { enablesharding :"test2"});
mongos> db.runCommand( { shardcollection : "test2.table2",key : {id: "hashed"} } )
mongos> use test2
mongos> for (var i = 1; i <= 100; i++) db.table2.save({id:i,"test2":"testval1"});
mongos> db.table2.stats()

https://www.oschina.net/question/3571801_2286249?sort=default

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sun_xuegang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值