本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名原文链接~~~

Ubuntu快速部署MongoDB(单节点和分片集群)

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:


下载

官网下载:

我这里使用 Ubuntu 18.04,所以对应下载的二进制包链接为:

单机部署

调整内核参数:

$ cat << EOF > /etc/rc.local 
#!/bin/bash
echo never >  /sys/kernel/mm/transparent_hugepage/enabled
echo never >  /sys/kernel/mm/transparent_hugepage/defrag
EOF

$ . /etc/rc.local

创建 Mongo 启动用户:

$ groupadd mongo -g 668 && useradd mongo -u 668 -g 668

创建安装目录:

$ mkdir -p /opt/packages /opt/apps/mongodb/{conf,log,data,bin}

解压二进制包并移动二进制文件到安装目录:

$ tar xf mongodb-linux-x86_64-ubuntu1804-4.4.2.tgz
$ mv mongodb-linux-x86_64-ubuntu1804-4.4.2/bin/* /opt/apps/mongodb/bin

配置:

$ cat << EOF > /opt/apps/mongodb/conf/mongod.conf
systemLog:
  destination: file
  path: "/opt/apps/mongodb/log/mongod.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: "/opt/apps/mongodb/data/"
processManagement:
  fork: true
net:
  port: 27017
  bindIp: $(hostname -I),127.0.0.1
EOF

使用 Systemd 管理 Mongo 服务:

$ cat > /lib/systemd/system/mongod.service <<EOF
[Unit]
Description=MongoDB 
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongo
Type=forking
ExecStart=/opt/apps/mongodb/bin/mongod --config /opt/apps/mongodb/conf/mongod.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/apps/mongodb/bin/mongod --config /opt/apps/mongodb/conf/mongod.conf --shutdown
PrivateTmp=true
LimitNOFILE=64000
LimitNPROC=64000
[Install]
WantedBy=multi-user.target
EOF

配置并加载环境变量:

$ cat > /etc/profile.d/mongo_env.sh <<EOF
export PATH="/opt/apps/mongodb/bin:$PATH"
EOF

$ . /etc/profile.d/mongo_env.sh

授权安装目录:

$ chown -R mongo.mongo /opt/apps/mongodb

启动服务:

$ systemctl start mongod

创建用户:

$ mongo 
> use admin
> db.createUser(
{
    user: "root",
    pwd: "root123",
    roles: [ { role: "root", db: "admin" } ]
})

修改配置:

$ vim /opt/apps/mongodb/conf/mongod.conf
security:
  authorization: enabled

重启服务:

$ systemctl restart mongod

分片集群部署

规划

角色实例
ConfigServer10.0.1.111:27018、10.0.1.112:27018、10.0.1.113:27018
Shard 110.0.1.111:27019、10.0.1.112:27019、10.0.1.113:27019(arbiter)
Shard 210.0.1.111:27020、10.0.1.112:27020(arbiter)、10.0.1.113:27020
Shard 310.0.1.111:27021(arbiter)、10.0.1.112:27021、10.0.1.113:27021
Mongos10.0.1.111:27017、10.0.1.112:27017、10.0.1.113:27017

下面步骤如果没有注明操作机器则需要在所有机器上操作。

基础环境

$ cat << EOF > /etc/rc.local 
#!/bin/bash
echo never >  /sys/kernel/mm/transparent_hugepage/enabled
echo never >  /sys/kernel/mm/transparent_hugepage/defrag
EOF

$ . /etc/rc.local

$ groupadd mongo -g 668 && useradd mongo -u 668 -g 668

$ mkdir -p /opt/apps/mongodb/{bin,27017,27018,27019,27020,27021}/{conf,log,data}

$ tar xf mongodb-linux-x86_64-ubuntu1804-4.4.2.tgz
$ mv mongodb-linux-x86_64-ubuntu1804-4.4.2/bin/* /opt/apps/mongodb/bin
$ chown -R mongo.mongo /opt/apps/mongodb

$ cat > /etc/profile.d/mongo_env.sh <<EOF
export PATH="/opt/apps/mongodb/bin:$PATH"
EOF

$ . /etc/profile.d/mongo_env.sh

ConfigServer

$ cat << EOF > /opt/apps/mongodb/27018/conf/mongod.conf
systemLog:
  destination: file
  path: /opt/apps/mongodb/27018/log/mongod.log
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /opt/apps/mongodb/27018/data/
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
net:
  bindIp: $(hostname -I),127.0.0.1
  port: 27018
replication:
  oplogSizeMB: 2048
  replSetName: configReplSet
sharding:
  clusterRole: configsvr
processManagement: 
  fork: true
EOF

$ cat > /lib/systemd/system/mongo-config-server.service <<EOF
[Unit]
Description=mongodb config server 
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongo
Type=forking
ExecStart=/opt/apps/mongodb/bin/mongod --config /opt/apps/mongodb/27018/conf/mongod.conf
ExecReload=/bin/kill -s HUP 
ExecStop=/opt/apps/mongodb/bin/mongod --config /opt/apps/mongodb/27018/conf/mongod.conf --shutdown
PrivateTmp=true  
LimitNOFILE=64000
LimitNPROC=64000
[Install]
WantedBy=multi-user.target
EOF

$ systemctl start mongo-config-server.service

Shard1

$ cat > /opt/apps/mongodb/27019/conf/mongod.conf <<EOF
systemLog:
  destination: file
  path: /opt/apps/mongodb/27019/log/mongod.log   
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /opt/apps/mongodb/27019/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
net:
  bindIp: $(hostname -I), 127.0.0.1
  port: 27019
replication:
  oplogSizeMB: 2048
  replSetName: sh1
sharding:
  clusterRole: shardsvr
processManagement: 
  fork: true
EOF
# 10.0.1.113 改为 mongod-sh1-arbiter.service
$ cat << EOF > /lib/systemd/system/mongod-sh1.service
# cat << EOF > /lib/systemd/system/mongod-sh1-arbiter.service
[Unit]
Description=mongodb 
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongo
Type=forking
ExecStart=/opt/apps/mongodb/bin/mongod --config /opt/apps/mongodb/27019/conf/mongod.conf
ExecReload=/bin/kill -s HUP 
ExecStop=/opt/apps/mongodb/bin/mongod --config /opt/apps/mongodb/27019/conf/mongod.conf --shutdown
PrivateTmp=true
LimitNOFILE=64000
LimitNPROC=64000
[Install]
WantedBy=multi-user.target
EOF

Shard2

$ cat > /opt/apps/mongodb/27020/conf/mongod.conf <<EOF
systemLog:
  destination: file
  path: /opt/apps/mongodb/27020/log/mongod.log   
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /opt/apps/mongodb/27020/data
  directoryPerDB: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
net:
  bindIp: $(hostname -I),127.0.0.1
  port: 27020
replication:
  oplogSizeMB: 2048
  replSetName: sh2
sharding:
  clusterRole: shardsvr
processManagement: 
  fork: true
EOF
# 10.0.1.112 改为 mongod-sh2-arbiter.service
$ cat << EOF > /lib/systemd/system/mongod-sh2.service
# cat << EOF > /lib/systemd/system/mongod-sh2-arbiter.service
[Unit]
Description=mongodb 
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongo
Type=forking
ExecStart=/opt/apps/mongodb/bin/mongod --config /opt/apps/mongodb/27020/conf/mongod.conf
ExecReload=/bin/kill -s HUP 
ExecStop=/opt/apps/mongodb/bin/mongod --config /opt/apps/mongodb/27020/conf/mongod.conf --shutdown
PrivateTmp=true
LimitNOFILE=64000
LimitNPROC=64000
[Install]
WantedBy=multi-user.target
EOF

Shard3

$ cat > /opt/apps/mongodb/27021/conf/mongod.conf <<EOF
systemLog:
  destination: file
  path: /opt/apps/mongodb/27021/log/mongod.log   
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /opt/apps/mongodb/27021/data
  directoryPerDB: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
net:
  bindIp: $(hostname -I),127.0.0.1
  port: 27021
replication:
  oplogSizeMB: 2048
  replSetName: sh3
sharding:
  clusterRole: shardsvr
processManagement: 
  fork: true
EOF
# 10.0.1.111 改为 mongod-sh3-arbiter.service
$ cat << EOF > /lib/systemd/system/mongod-sh3.service
# cat << EOF > /lib/systemd/system/mongod-sh3-arbiter.service
[Unit]
Description=mongodb 
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongo
Type=forking
ExecStart=/opt/apps/mongodb/bin/mongod --config /opt/apps/mongodb/27021/conf/mongod.conf
ExecReload=/bin/kill -s HUP 
ExecStop=/opt/apps/mongodb/bin/mongod --config /opt/apps/mongodb/27021/conf/mongod.conf --shutdown
PrivateTmp=true
LimitNOFILE=64000
LimitNPROC=64000
[Install]
WantedBy=multi-user.target
EOF

Mongos

$ cat > /opt/apps/mongodb/27017/conf/mongos.conf << EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/apps/mongodb/27017/log/mongos.log

processManagement:
  fork: true
  pidFilePath: /opt/apps/mongodb/27017/data/mongos.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27017
  bindIp: 127.0.0.1,$(hostname -I)

sharding:
  configDB: 
    configset/10.0.1.111:27018,10.0.1.112:27018,10.0.1.113:27018
EOF

$ cat << EOF > /lib/systemd/system/mongos.service
[Unit]
Description=mongos
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongo
Type=forking
ExecStart=/opt/apps/mongodb/bin/mongos --config /opt/apps/mongodb/27017/conf/mongos.conf
ExecReload=/bin/kill -s HUP 
ExecStop=/bin/kill -2 \$MAINPID
PrivateTmp=true
LimitNOFILE=64000
LimitNPROC=64000
[Install]
WantedBy=multi-user.target
EOF

启动配置

所有节点启动 ConfigServer:

$ systemctl start mongo-config-server.service

启动配置第一组复制集:

# 10.0.1.111 和 10.0.1.112
$ systemctl start mongod-sh1.service
# 10.0.1.113
$ systemctl start mongod-sh1-arbiter.service
#  10.0.1.111 或 10.0.1.112
$ mongo --port 27019
> use admin
> config = {_id: 'sh1', members: [
                          {_id: 0, host: '10.0.1.111:27019', priority: 10},
                          {_id: 1, host: '10.0.1.112:27019', priority: 5},
                          {_id: 2, host: '10.0.1.113:27019', arbiterOnly: true}]
           }
> rs.initiate(config)

启动配置第二组复制集:

# 10.0.1.111 和 10.0.1.113
$ systemctl start mongod-sh2.service
# 10.0.1.112
$ systemctl start mongod-sh2-arbiter.service
# 10.0.1.111 或 10.0.1.113
$ mongo --port 27020
> use admin
> config = {_id: 'sh2', members: [
						  {_id: 0, host: '10.0.1.111:27020', priority: 5},
                          {_id: 1, host: '10.0.1.112:27020', arbiterOnly: true},
                          {_id: 2, host: '10.0.1.113:27020', priority: 10}]
           }
> rs.initiate(config)

启动配置第三组复制集:

# 10.0.1.112 和 10.0.1.113
$ systemctl start mongod-sh3.service
# 10.0.1.111
$ systemctl start mongod-sh3-arbiter.service
# 10.0.1.112 或 10.0.1.113
$ mongo --port 27021
> use admin
> config = {_id: 'sh3', members: [
						  {_id: 0, host: '10.0.1.111:27021', arbiterOnly: true},
                          {_id: 1, host: '10.0.1.112:27021', priority: 10},
                          {_id: 2, host: '10.0.1.113:27021', priority: 5}]
           }
> rs.initiate(config)

为 ConfigServer 配置复制集:

$ mongo --port 27018
> use admin
> config = {_id: 'configReplSet', members: [
                          {_id: 0, host: '10.0.1.111:27018'},
                          {_id: 1, host: '10.0.1.112:27018'},
                          {_id: 2, host: '10.0.1.113:27018'}]
           }
> rs.initiate(config)  

所有节点启动 mongos:

$ systemctl start mongos.service

登入 Mongos 添加分片:

$ mongo --port 27017
> use admin
> db.runCommand( { addshard : "sh1/10.0.1.111:27019,10.0.1.112:27019,10.0.1.113:27019",name:"shard1"} )
> db.runCommand( { addshard : "sh2/10.0.1.111:27020,10.0.1.112:27020,10.0.1.113:27020",name:"shard2"} )
> db.runCommand( { addshard : "sh3/10.0.1.111:27021,10.0.1.112:27021,10.0.1.113:27021",name:"shard3"} )
> db.runCommand( { listshards : 1 } )

密码认证

登入 Mongos 进行操作:

$ mongo --port 27017
> use admin
> db.createUser(
{
    user: "admin",
    pwd: "xxxx",
    roles: [ { role: "root", db: "admin" } ]
})
> db.auth('admin','xxxx')

创建认证文件:

$ openssl rand -base64 756 > /opt/apps/mongodb/auth_key
$ chown mongo.mongo /opt/apps/mongodb/auth_key
$ chmod 400 /opt/apps/mongodb/auth_key
# 将认证文件同步到所有节点的 /opt/apps/mongodb/auth_key,注意权限

配置:

# config server、mongod、arbiter 添加如下配置(27018、27019、27020、27021)
security:
  keyFile: /opt/apps/mongodb/auth_key
  authorization: enabled
# mongos 添加如下配置(27017)
security:
  keyFile: /opt/apps/mongodb/auth_key

重启服务:

# 10.0.1.111
$ systemctl restart mongod-sh1.service mongod-sh2.service mongod-sh3-arbiter.service mongo-config-server.service mongos.service
# 10.0.1.112
$ systemctl restart mongod-sh1.service mongod-sh2-arbiter.service  mongod-sh3.service mongo-config-server.service mongos.service
# 10.0.1.113
$ systemctl restart mongod-sh1-arbiter.service mongod-sh2.service mongod-sh3.service mongo-config-server.service mongos.service
# MongoDB  

如果这篇文章对您有帮助,可点击下方链接分享给你的朋友们😋,如果遇到问题欢迎评论、留言~~~😇

评论

公众号:zze_coding

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×