Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB 副本集部署-3.6版本 #8

Open
johnnian opened this issue Jun 30, 2017 · 1 comment
Open

MongoDB 副本集部署-3.6版本 #8

johnnian opened this issue Jun 30, 2017 · 1 comment

Comments

@johnnian
Copy link
Owner

johnnian commented Jun 30, 2017

操作系统: CentOS 6.9
安装软件包:mongodb-linux-x86_64-rhel62-3.6.2.tgz
MongoDB版本:3.6

安装流程思路

目前最小节点是3个,本文档暂且按照3个节点来配置:A、B、C
1、在集群每个节点上安装一份MongoDB;
2、配置副本集;
3、配置副本集的用户、密码;
4、配置副本集的KeyFile安全鉴权;
5、配置开机自启动;

每个节点IP:

  • A节点: 172.17.0.3
  • B节点: 172.17.0.4
  • C节点: 172.17.0.5

步骤1: 每个节点安装MongoDB

1、下载MongoDB安装包:mongodb-linux-x86_64-3.4.1.tgz

[root@a6a766e6204a ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.6.2.tgz

2、解压安装包, 创建目录,拷贝配置文件:

[root@a6a766e6204a ~]# tar -zxvf mongodb-linux-x86_64-rhel62-3.6.2.tgz
[root@a6a766e6204a ~]# mv mongodb-linux-x86_64-rhel62-3.6.2 mongodb
[root@a6a766e6204a ~]# mv mongo.conf mongodb/
[root@a6a766e6204a ~]# mkdir mongodb/data mongodb/keyfile mongodb/logs

创建完成后,目录的结构:
mongodb
├── bin #可执行文件
├── data #存放数据库文件
├── keyfile #存放Keyfile
├── logs #存放系统日志
├── mongo.conf #配置文件

3、添加到环境变量

[root@a6a766e6204a ~]# vi /etc/bashrc
#MongoDB config
MONGO_HOME=/root/mongodb
PATH=$MONGO_HOME/bin:$PATH
export PATH MONGO_HOME

[root@a6a766e6204a ~]# source /etc/bashrc

4、创建 mongo.conf 配置项,包括端口、路径等, 例如,配置后的文件:

[root@a6a766e6204a ~]# vi mongodb/mongo.conf

#日志文件位置:改为实际路径
logpath=/root/mongodb/logs/mongo.log
#以追加的方式写日志
logappend=true
#端口
port=27018
#是否以守护进程的方式运行
fork=true
#数据库存储位置:改为实际路径
dbpath=/root/mongodb/data/
#是否以安装认证方式运行
#auth=true
#副本集名字
replSet=replSet
#KeyFile鉴权文件:改为实际路径
#keyFile=/root/mongodb/keyfile
#最大缓存大小,根据实际情况而定
wiredTigerCacheSizeGB=8

谈到配置文件,MongoDB有下面两种配置:

下面的配置与上面INI形式配置是一样的:

systemLog:
   destination: file
   path: "/root/mongodb/logs/mongo.log"
   logAppend: true
storage:
   dbPath:"/root/mongodb/data/"
   journal:
      enabled: true
   wiredTiger:
      engineConfig:
         cacheSizeGB: 6
replication:
    replSetName: "replSet"
processManagement:
   fork: true
net:
   bindIp: 0.0.0.0
   port: 27018
setParameter:
   enableLocalhostAuthBypass: false

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

[root@a6a766e6204a ~]# mongod -f mongodb/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1289
child process started successfully, parent exiting

步骤2: 组建副本集

1、使用mongo 客户端 连接任意节点(假设 目前创建的节点有 A、B、C 三台)

[root@c43bfab6f744 ~]# mongo --port 27018

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

> config = {_id: 'replSet', members: [{_id: 0, host: '172.17.0.3:27018'},{_id: 1, host: '172.17.0.4:27018'},{_id: 2, host:'172.17.0.5:27018'}]}
{
	"_id" : "replSet",
	"members" : [
		{
			"_id" : 0,
			"host" : "172.17.0.3:27018"
		},
		{
			"_id" : 1,
			"host" : "172.17.0.4:27018"
		},
		{
			"_id" : 2,
			"host" : "172.17.0.5:27018"
		}
	]
}

> rs.initiate(config)   #初始化副本集
{ "ok" : 1 }

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

步骤3:创建帐户密码

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

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

[root@c43bfab6f744 ~]# mongo --port 27018
replSet:PRIMARY> use admin
switched to db admin

#创建分配用户权限的帐户:admin
replSet:PRIMARY> db.createUser({user:"admin", pwd:"admin", roles:[{role: "userAdminAnyDatabase", db:"admin" }]})
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

#创建普通数据库、用户
replSet:PRIMARY> db.auth("admin","admin")
1

replSet:PRIMARY> use mytest  #创建mytest数据库
switched to db mytest

replSet:PRIMARY> db.createUser({user:"mytest",pwd:"mytest",roles:[{role:"dbOwner",db:"mytest"}]})
Successfully added user: {
	"user" : "mytest",
	"roles" : [
		{
			"role" : "dbOwner",
			"db" : "mytest"
		}
	]
}

这样,就创建数据库:mytest, 数据库用户:mytest / mytest, 生产部署的时候,改成对应的数据库名、用户名

可以通过连接验证下:

[root@c43bfab6f744 ~]# mongo 172.17.0.3:27018/mytest -u mytest -p mytest

步骤4:创建副本集认证key文件

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

[root@c43bfab6f744 ~]# openssl rand -base64 90 -out ./keyfile
[root@c43bfab6f744 ~]# cp keyfile mongodb/keyfile/
[root@c43bfab6f744 ~]# chmod 600 mongodb/keyfile/keyfile

备注:keyfile的属性必须更改,否则会报错:

2017-06-30T07:01:28.950+0000 I CONTROL  [main] ***** SERVER RESTARTED *****
2017-06-30T07:01:28.954+0000 I ACCESS   [main] permissions on /root/mongodb/keyfile/keyfile are too open

并且把这份keyfile同步到其他两个节点的 ~/mongodb/keyfile/ 文件夹中;

2、关闭副本集:分别关闭每个节点的mongod

[root@c43bfab6f744 ~]# mongo --port 27018
replSet:PRIMARY> use admin
replSet:PRIMARY> db.shutdownServer()

3、修改每个节点的配置文件mongo.conf中的下面项:

[root@c43bfab6f744 ~]# vi  ~/mongodb/mongo.conf

...
...
#是否以安装认证方式运行
auth=true
#KeyFile鉴权文件:改为实际路径
keyFile=/root/mongodb/keyfile/keyfile

4、重新启动副本集

[root@c43bfab6f744 ~]# mongod -f mongodb/mongo.conf #机器A
[root@c43bfab6f744 ~]# mongod -f mongodb/mongo.conf #机器B
[root@c43bfab6f744 ~]# mongod -f mongodb/mongo.conf #机器C

步骤5:设置MongoDB开机自启动

[root@c43bfab6f744 ~]# vi /etc/rc.d/rc.local #加入下面的自启动脚本 
/root/mongodb/bin/mongod -f /root/mongodb/mongo.conf 

步骤6: 设置MongoDB自动备份

注意:

  1. 需要连接到MongoDB的主节点;
  2. 命令中输出文件夹,% 号需要专一成 \%
[root@017d14b36363 ~]# crontab  -e
0 7 * * * mongodump --host 172.17.0.4 --port 27018 -u mytest -p mytest -d mytest -o /data/mongodata/$(date +\%Y\%m\%d)

附录

1、创建数据库的用户角色:

role角色
  • 数据库用户角色:read、readWrite;
  • 数据库管理角色:dbAdmin、dbOwner、userAdmin;
  • 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
  • 备份恢复角色:backup、restore;
  • 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
  • 超级用户角色:root
  • 内部角色:__system
角色说明
  • read:允许用户读取指定数据库
  • readWrite:允许用户读写指定数据库
  • dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
  • userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
  • clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
  • readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
  • readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
  • userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
  • dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
  • root:只在admin数据库中可用。超级账号,超级权限
  • dbOwner: readWrite + dbAdmin + dbAdmin

2、Java客户端连接配置

Spring XML配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/data/mongo     
        http://www.springframework.org/schema/data/mongo/spring-mongo.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- credentials="用户名:密码@用户归属数据库" -->
	<mongo:mongo-client replica-set="172.17.0.3:27018,172.17.0.4:27018, 172.17.0.5:27018" credentials="mytest:mytest@mytest"  id="mongo">
        <mongo:client-options 
            connections-per-host="20"
	        threads-allowed-to-block-for-connection-multiplier="10" 
	        connect-timeout="120000"
	        max-wait-time="120000"
	        socket-keep-alive="true"
	        socket-timeout="150000"
	         />	
	</mongo:mongo-client>

	<mongo:db-factory dbname="数据库名" mongo-ref="mongo" />

	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
	</bean>
</beans>
Spring Boot配置
spring.data.mongodb.uri=mongodb://user:pwd@ip1:port1,ip2:port2/database
@johnnian johnnian changed the title MongoDB副本集安装—带keyfile安全认证以及用户权限 生产环境部署MongoDB副本集(带keyfile安全认证以及用户权限) Jul 7, 2017
@johnnian johnnian changed the title 生产环境部署MongoDB副本集(带keyfile安全认证以及用户权限) MongoDB 副本集部署-3.6版本 Jan 24, 2018
@youyang
Copy link

youyang commented Nov 2, 2018

mongo.conf 中缺少 bind_ip 设置,导致rs.initiate(config)时提示 Connection refused

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants