mongodb分片集群 搭建 + keyFile认证
<h1> 创建mongodb分片集群 + keyFile认证</h1><h2>一、环境准备</h2>
<p> 1. 服务器节点信息【三台服务器】<br> x.x.x.159、 x.x.x..160、 x.x.x..161<br> 2. 服务器系统<br> linux x86_64 GNU/Linux 或者centos7或者redhat<br> 3. mongodb版本<br> mongodb v4.2.5<br> 下载地址:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.5.tgz<br> </p>
<h2> 二、 集群环境设计 </h2>
<p> 1. 三个 分片 复制集合<br> shard1(x.x.x.159:27017、 x.x.x.160:27017、 x.x.x.161:27017)<br> shard2(x.x.x.159:27018、 x.x.x.160:27018、 x.x.x.161:27018)<br> shard3(x.x.x.159:27019、 x.x.x.160:27019、 x.x.x.161:27019)<br> 2. 一个 config复制集合<br> (x.x.x.159:28018、 x.x.x.160:28018、 x.x.x.161:28018)<br> 3. 一个 mongos节点<br> (x.x.x.159:27000)</p>
<h2> 三、mongodb 安装、</h2>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">`tar -zxvf mongodb-linux-x86_64-rhel70-4.2.5.tgz`
mv mongodb-linux-x86_64-rhel70-4.2.5 /opt/mongodb4.2
</pre>
</div>
<p> </p>
<h2>四、添加环境变量</h2>
<p> 1. vim /etc/profile<br> 2. 文件末尾追加: export PATH=$PATH:/opt/mongodb/bin<br> 3. 加载文件立刻生效: source /etc/profile</p>
<h2>五、搭建mongodb分片复制集</h2>
<p> 1. 分别在三台服务器(x.x.x.159、x.x.x.160、 x.x.x.161)节点 执行如下命令</p>
<p> 2. mongodb分片集群 配置文件 目录 </p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mkdir -p /opt/mongodb4.2/conf</pre>
</div>
<p> 3. 分片日志目录</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mkdir -p /opt/mongodb4.2/logs</pre>
</div>
<p> 4. 创建集群安全认证机制KeyFile</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> openssl rand -base64 736 > /opt/mongodb4.2/conf/mongodb.keyfile</pre>
</div>
<p> Notice: keyfile 必须满足以下条件:</p>
<p style="margin-left: 60px">1) 至少6个字符,小于1024字节。<br>2) 认证的时候不考虑文件中的空白字符。<br>3) 连接副本集成员的KeyFile和启动mongos进程的 KeyFile文件内容必须一致。<br>4) 必须是base64编码,但不能有等号。<br>5) 文件权限必须是 600(chmod 600),不能分配任何权限给group成员和其他成员。</p>
<p style="margin-left: 60px"> </p>
<p> 5. 创建分片 数据存储目录</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mkdir -p /opt/mongodb4.2/data/db1 【分片1】
mkdir -p /opt/mongodb4.2/data/db2 【分片2】
mkdir -p /opt/mongodb4.2/data/db3 【分片3】</pre>
</div>
<p> 6. 创建分片配置文件</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">vim /opt/mongodb4.2/conf/mongodb1.conf 【分片1 配置文件】
storage:
journal:
enabled: true
wiredTiger:
engineConfig:
cacaeSizeGB: 2
directoryForIndexes: true
systemLog:
destination: file
logAppend: true
path: /opt/mongodb4.2/logs/mongodb1.log
net:
port: 27017
bindIp: 0.0.0.0
processManagement:
fork: true
replication:
oplogSizeMB: 4000
replSetName: jxk1
sharding:
clusterRole: shardsvr
security:
authorization: enabled
keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
clusterAuthMode: keyFile
vim /opt/mongodb4.2/conf/mongodb2.conf 【分片2 配置文件】
storage:
journal:
enabled: true
wiredTiger:
engineConfig:
cacaeSizeGB: 2
directoryForIndexes: true
systemLog:
destination: file
logAppend: true
path: /opt/mongodb4.2/logs/mongodb2.log
net:
port: 27018
bindIp: 0.0.0.0
processManagement:
fork: true
replication:
oplogSizeMB: 4000
replSetName: jxk2
sharding:
clusterRole: shardsvr
security:
authorization: enabled
keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
clusterAuthMode: keyFile
vim /opt/mongodb4.2/conf/mongodb3.conf 【分片3 配置文件】
storage:
journal:
enabled: true
wiredTiger:
engineConfig:
cacaeSizeGB: 2
directoryForIndexes: true
systemLog:
destination: file
logAppend: true
path: /opt/mongodb4.2/logs/mongodb3.log
net:
port: 27018
bindIp: 0.0.0.0
processManagement:
fork: true
replication:
oplogSizeMB: 4000
replSetName: jxk3
sharding:
clusterRole: shardsvr
security:
authorization: enabled
keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
clusterAuthMode: keyFile
注意: 配置文件 yuml语法格式 直接拷贝下来 语法格式估计不能用
</pre>
</div>
<p> </p>
<h2> 六、启动 分片复制集</h2>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> 1. 分别在三台服务器(x.x.x.159、x.x.x.160、 x.x.x.161)节点 执行如下命令
2. mongod -f /opt/mongodb4.2/conf/mongodb1.conf
3. mongod -f /opt/mongodb4.2/conf/mongodb2.conf
4. mongod -f /opt/mongodb4.2/conf/mongodb3.conf</pre>
</div>
<h2>七、 初始化分片复制集 并 设置 分片复制集账号</h2>
<p> 1. 在其中任意一台节点服务器登录 本人使用 x.x.x.159节点<br> 2. mongo -port 27017 【登录】<br> 3. 进入mongodb命令输入终端 输入命令如下</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> var conf = {
_id:'jxk',
members:[
{_id:1, host:'x.x.x.159:27017'},
{_id:2, host:'x.x.x.160:27017'},
{_id:3, host:'x.x.x.161:27017'},
],
}
rs.initiate(conf)
rs.status()
</pre>
</div>
<p> </p>
<p> 4. 找到mongodb自己选举出来的 主库 输入命令:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> use admin
db.createUser({
user: "username",
pwd : "password",
roles:[
{role:"root", db:"admin"}
]
})
db.auth("username", "password")</pre>
</div>
<h2>八、搭建config节点复制集</h2>
<p> 1. 分别在三台服务器(x.x.x.159、x.x.x.160、 x.x.x.161)节点 执行如下命令</p>
<p> 2. 创建 config 数据存储 目录</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mkdir -p /opt/mongodb4.2/mongo-cfg/data</pre>
</div>
<p> 3. 创建 config 日志 目录</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mkdir -p /opt/mongodb4.2/mongo-cfg/logs</pre>
</div>
<p> 4. 创建 config复制集 配置文件 <br> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> vim /opt/mongodb4.2/conf/mongodb-cfg.conf
systemLog:
destination: file
logAppend: true
path: /opt/mongodb4.2/logs/mongodb3.log
storage:
journal:
enabled: true
dbPath: /opt/mongodb4.2/mongo-cfg/data
directoryPerDB: true
wiredTiger:
engineConfig:
cacaeSizeGB: 2
directoryForIndexes: true
collectionConfig:
blockCompressor: zstd
indexConfig:
prefixCompression: true
net:
port: 28018
bindIp: x.x.x.159
replication:
oplogSizeMB: 4000
replSetName: configReplSet
sharding:
clusterRole: configsvr
processManagement:
fork: true </pre>
</div>
<p> 5. 启动config节点复制集</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf</pre>
</div>
<p> 6. 登录config节点</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mongo -host x.x.x.(159/160/161) -port 28018</pre>
</div>
<p> 7.初始化 config节点</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> var conf = {
_id:'configReplSet',
members:[
{_id:0, host:"x.x.x.159:28018"},
{_id:1, host:"x.x.x.160:28018"},
{_id:2, host:"x.x.x.161:28018"},
]
}
rs.initiate(conf)
rs.status()
</pre>
</div>
<p> 8. 创建认证用户</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> use admin
db.createUser({
user: "username",
pwd : "password",
roles:[
{role:"root", db:"admin"}
]
})
db.auth("username", "password")
</pre>
</div>
<p> </p>
<p> 9. 开启config复制集 认证<br> 关闭 config复制集<br> 在配置文件追加<br> vim /opt/mongodb4.2/conf/mongodb-cfg.conf</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> security:
authorization: enabled
keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
clusterAuthMode: keyFile
</pre>
</div>
<p> </p>
<p> 启动 config复制集</p>
<h2>九、搭建mongos节点(x.x.18.159)</h2>
<p> 1. 在 x.x.x.159 服务器上搭建客户端<br> 2. 创建mongos 日志目录</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mkdir -p /opt/mongodb4.2/mongos/logs</pre>
</div>
<p> 3. 创建mongos 配置文件<br> vim /opt/mongodb4.2/conf/mongos.conf</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> systemLog:
destination: file
logAppend: true
path: /opt/mongodb4.2/mongos/logs/mongodb.log
net:
port: 27000
bindIp: 0.0.0.0
sharding:
configDB: configReplSet/x.x.x.159:28018,x.x.x.160:28018,x.x.x.160:28018,
processManagement:
fork: true
security:
keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
clusterAuthMode: keyFile</pre>
</div>
<p> 4. 启动mongos</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mongos -f /opt/mongodb4.2/conf/mongos.conf</pre>
</div>
<p> 5. 登录并初始化 mongos</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mongo -port 27000
use admin
db.auth("username", "password")#这里使用的账号是 config复制集的账号
db.runCommand({
addshard:"jxk1/x.x.x.159:27017,x.x.x.160:27017,x.x.x.161:27017,",
name: "shard1"
});
db.runCommand({
addshard:"jxk2/x.x.x.159:27018,x.x.x.160:27018,x.x.x.161:27018,",
name: "shard1"
});
db.runCommand({
addshard:"jxk3/x.x.x.159:27019,x.x.x.160:27019,x.x.x.161:27019,",
name: "shard1"
})
</pre>
</div>
<p> </p>
<p> 6. 测试是否搭建成功</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> db.runCommand( {enablesharding:"dbtest"})
db.runCommand( {shardcollection:"dbtest.one", key:{id:1} })
use dbtest
var tmp = [];
for(var i =0; i<10000000; i++){
tmp.push({ 'id':i, "name":"lunck dog " + i});
}
db.one.insertMany(tmp);
sh.status()
</pre>
</div>
<p> </p>
<h2>十、常用命令汇总</h2>
<p> 1. 分片命令</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mongod -f /opt/mongodb4.2/conf/mongodb1.conf 启动
mongod -f /opt/mongodb4.2/conf/mongodb1.conf --shutdown 关闭
OR
mongo -port 27017
use admin
db.shutdownServers()
</pre>
</div>
<p> </p>
<p> 2.config 复制集命令</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf 启动
mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf --shutdown 关闭</pre>
</div>
<p> 3. mongos 命令</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> mongos -f /opt/mongodb4.2/conf/mongos.conf 启动
mongo -port 27017 关闭
use admin
db.shutdownServers()
</pre>
</div>
<p> </p>
<h2>参考资料</h2>
<p> https://my.oschina.net/u/563789/blog/3050068<br> https://blog.csdn.net/qq_24434491/article/details/102907486<br> https://blog.csdn.net/lezeqe/article/details/90518179<br> https://blog.csdn.net/tototuzuoquan/article/details/78295040<br> https://www.freesion.com/article/6886370039/#8mongo_247<br> https://www.cnblogs.com/shaosks/p/5775757.html<br> https://www.cnblogs.com/wxmdevelop/p/7341292.html</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/jxkshu/p/mongodb.html
頁:
[1]