安装MongoDB centos 7

 


使用tgz压缩包安装:
#wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.1.tgz
或者时候用RPM包:
1.配置软件仓库:
#cat /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb]
name=MongoDB
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 search mongodb-org --show-duplicate
...
====================================== N/S matched: mongodb-org =======================================
mongodb-org-4.0.0-1.el7.x86_64 : MongoDB open source document-oriented database system (metapackage)
mongodb-org-4.0.1-1.el7.x86_64 : MongoDB open source document-oriented database system (metapackage)
mongodb-org-mongos-4.0.0-1.el7.x86_64 : MongoDB sharded cluster query router
mongodb-org-mongos-4.0.1-1.el7.x86_64 : MongoDB sharded cluster query router
mongodb-org-server-4.0.0-1.el7.x86_64 : MongoDB database server
mongodb-org-server-4.0.1-1.el7.x86_64 : MongoDB database server
mongodb-org-shell-4.0.0-1.el7.x86_64 : MongoDB shell client
mongodb-org-shell-4.0.1-1.el7.x86_64 : MongoDB shell client
mongodb-org-tools-4.0.0-1.el7.x86_64 : MongoDB tools
mongodb-org-tools-4.0.1-1.el7.x86_64 : MongoDB tools
 
  Name and summary matches only, use "search all" for everything.
 
3.安装:
# yum -y install mongodb-org-4.0.1 mongodb-org-mongos-4.0.1 mongodb-org-server-4.0.1 mongodb-org-shell-4.0.1 mongodb-org-tools-4.0.1


...
Dependencies Resolved
 
=======================================================================================================
 Package                        Arch               Version                   Repository           Size
=======================================================================================================
Installing:
 mongodb-org                    x86_64             4.0.1-1.el7               mongodb             5.8 k
 mongodb-org-mongos             x86_64             4.0.1-1.el7               mongodb              12 M
 mongodb-org-server             x86_64             4.0.1-1.el7               mongodb              21 M
 mongodb-org-shell              x86_64             4.0.1-1.el7               mongodb              13 M
 mongodb-org-tools              x86_64             4.0.1-1.el7               mongodb              29 M
 
Transaction Summary
=======================================================================================================
Install  5 Packages
 
4.查看mongoDB的软件包位置:
# rpm -ql mongodb-org-server
/etc/mongod.conf
/lib/systemd/system/mongod.service
/usr/bin/mongod
/usr/share/doc/mongodb-org-server-4.0.1
/usr/share/doc/mongodb-org-server-4.0.1/GNU-AGPL-3.0
/usr/share/doc/mongodb-org-server-4.0.1/LICENSE-Community.txt
/usr/share/doc/mongodb-org-server-4.0.1/MPL-2
/usr/share/doc/mongodb-org-server-4.0.1/README
/usr/share/doc/mongodb-org-server-4.0.1/THIRD-PARTY-NOTICES
/usr/share/man/man1/mongod.1
/var/lib/mongo
/var/log/mongodb
/var/log/mongodb/mongod.log
/var/run/mongodb
通过上面的查询我们可以看到mongoDB的服务名为mongod.
5.启动:
 
# systemctl start mongod

 

删除mongod服务

由于不是通过软件包内置的服务启动mongod进程,删除mongod这个服务避免误启动,也可以保留服务文件作为参照。

# systemctl disable mongod
# rm –f /usr/lib/systemd/system/mongod.service
# systemctl daemon-reload

 

每台服务器上都运行monogs、config、shard01、shard02、shard03服务,分别对应一个配置文件,统一将配置文件存放在/etc/mongodb/目录下。

# mkdir /etc/mongodb/
# chown –R mongod:mongod /etc/mongodb/

将config和shard的数据保存在/data/mongodb/目录下。

# mkdir -p /data/mongodb/{config,shard01,shard02,shard03}/data /data/mongodb/mongos
# chown –R mongod:mongod /data/mongodb/

日志统一存放在/var/log/mongodb/目录下

# mkdir /var/log/mongodb
# chown –R mongod:mongod /var/log/mongodb/

/etc/mongodb/shard01.conf

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/shard01.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/shard01/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20

# how the process runs
processManagement:
  fork: true
  pidFilePath: /data/mongodb/shard01/mongodb-shard01.pid
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27101
  bindIp: 0.0.0.0
  #unixDomainSocket:
  # pathPrefix: /var/run/mongodb

#operationProfiling:
replication:
    replSetName: ussmongo-shard01

sharding:
    clusterRole: shardsvr

 

/etc/mongodb/shard02.conf

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/shard02.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/shard02/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20

# how the process runs
processManagement:
  fork: true
  pidFilePath: /data/mongodb/shard02/mongodb-shard02.pid
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27102
  bindIp: 0.0.0.0
#  unixDomainSocket:
#    pathPrefix: /var/run/mongodb

#operationProfiling:
replication:
    replSetName: ussmongo-shard02

sharding:
    clusterRole: shardsvr

/etc/mongodb/shard03.conf

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/shard03.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/shard03/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20

# how the process runs
processManagement:
  fork: true
  pidFilePath: /data/mongodb/shard03/mongodb-shard03.pid
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27103
  bindIp: 0.0.0.0
  #unixDomainSocket:
   # pathPrefix: /var/run/mongodb

#operationProfiling:
replication:
    replSetName: ussmongo-shard03

sharding:
    clusterRole: shardsvr

/etc/mongodb/config.conf

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/config.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/config/data
  journal:
    enabled: true

# how the process runs
processManagement:
  fork: true
  pidFilePath: /data/mongodb/config/mongodb-config.pid
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27018
  bindIp: 0.0.0.0
  #unixDomainSocket:
   # pathPrefix: /var/run/mongodb

#operationProfiling:
replication:
    replSetName: ussmongo-config

sharding:
    clusterRole: configsvr

/etc/mongodb/mongos.conf

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongos.log

processManagement:
  fork: true
#  pidFilePath: /data/mongodb/mongos.pid

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0
#  unixDomainSocket:
#    pathPrefix: /var/run/mongodb

sharding:
   configDB: ussmongo-config/10.212.36.38:27018,10.212.36.39:27018,10.212.36.40:27018

setParameter:
  diagnosticDataCollectionDirectoryPath: /data/mongodb/mongos/diagnostic.data/

 

执行命令:

mongod --quiet -f /etc/mongodb/shard01.conf
mongod --quiet -f /etc/mongodb/shard02.conf
mongod --quiet -f /etc/mongodb/shard03.conf
mongod --quiet -f /etc/mongodb/config.conf
mongos --quiet -f /etc/mongodb/mongos.conf

配置副本集

config和shard服务本质上都是mongod进程,将他们都配置为三副本模式。下面的操作可以在三个节点中的任意一个上执行,只需要执行一遍。

config副本集:

# mongo --port 27018
> use admin
> config = {
...    _id : "ussmongo-config",
...     members : [
...         {_id : 0, host : "10.212.36.38:27018" },
...         {_id : 1, host : "10.212.36.39:27018" },
...         {_id : 2, host : "10.212.36.40:27018" }
...     ]
... }
> rs.initiate(config);

shard01副本集:

# mongo --port 27101
> use admin
> config = {
...    _id : "ussmongo-shard03",
...     members : [
...         {_id : 0, host : "10.212.36.38:27101" },
...         {_id : 1, host : "10.212.36.39:27101" },
...         {_id : 2, host : "10.212.36.40:27101" }
...     ]
... }
> rs.initiate(config);

shard02副本集:

# mongo --port 27102
> use admin
> config = {
...    _id : "ussmongo-shard02",
...     members : [
...         {_id : 0, host : "10.212.36.39:27102" },
...         {_id : 1, host : "10.212.36.40:27102" },
...         {_id : 2, host : "10.212.36.38:27102" }
...     ]
... }
> rs.initiate(config);

shard03副本集:

# mongo --port 27103
> use admin
> config = {
...    _id : "ussmongo-shard03",
...     members : [
...         {_id : 0, host : "10.212.36.40:27103" },
...         {_id : 1, host : "10.212.36.38:27103" },
...         {_id : 2, host : "10.212.36.39:27103" }
...     ]
... }
> rs.initiate(config);

配置分片路由

mongos对外提供服务,是集群的入口。需要先将分片添加到mongos配置中:

# mongo --port 27017
> use admin
> sh.addShard("ussmongo-shard01/10.212.36.38:27101,10.212.36.39:27101,10.212.36.40:27101")
> sh.addShard("ussmongo-shard02/10.212.36.39:27102,10.212.36.40:27102,10.212.36.38:27102")
> sh.addShard("ussmongo-shard03/10.212.36.40:27103,10.212.36.38:27103,10.212.36.39:27103")
> sh.status();

 

 启用访问控制

线上环境集群不可能使用免认证的方式,都要开启安全认证。MongoDB在开启了访问控制后,只有一次添加用户的机会,此后的操作都需要先认证通过。为了方便,我们先添加用户,然后再开启访问控制。

   添加用户

连接上mongos添加的用户会保存在config副本集中,但是不会保存到shard副本集,因此添加用户的操作需要分别在config、shard01、shard02、shard03上执行。

config副本集:

# mongo --port 27018
> use admin
> db.createUser(
...   {
...     user: "admin",
...     pwd: "admin",
...     roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
...   }
... )

shard01副本集:

# mongo --port 27101
> use admin
> db.createUser(
...   {
...     user: "admin",
...     pwd: "admin",
...     roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
...   }
... )

shard02副本集:

# mongo --port 27102
> use admin
> db.createUser(
...   {
...     user: "admin",
...     pwd: "admin",
...     roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
...   }
... )

shard03副本集:

# mongo --port 27103
> use admin
> db.createUser(
...   {
...     user: "admin",
...     pwd: "admin",
...     roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
...   }
... )

启用访问控制

1) 创建秘钥文件

启用访问控制之后,外部访问MongoDB服务需要进行身份验证,而mongos访问config和shard服务则是通过配置的秘钥文件。

# openssl rand -base64 756 >/data/mongodb/ussmongo.key
# chmod 0600 /data/mongodb/ussmongo.key
# chown mongod:mongod /data/mongodb/ussmongo.key

将密钥文件复制到所有节点上。

2) 添加security配置

mongos的配置文件添加如下配置:

security:
  keyFile: /data/mongodb/ussmongo.key

config和shard的配置文件添加如下配置:

security:
  authorization: enabled
  keyFile: /data/mongodb/ussmongo.key

3) 重启服务

在所有节点上重启所有MongoDB服务:

分片设置命令

db.runCommand({enableSharding:"testdb"})    #diameter_test is database name
db.runCommand( { shardCollection: "testdb.table1",key:{"_id":1}})
 

分片查询

db.printShardingStatus()

sh.status()

设置主,副,仲裁方式

cfg={ _id:"testrs", members:[ {_id:0,host:'10.10.148.130:27017',priority:2}, {_id:1,host:'10.10.148.131:27017',priority:1}, 
{_id:2,host:'10.10.148.132:27017',arbiterOnly:true}] };
 

mongodb 状态查询需要加 auth

mongostat  --port 27101 -u admin -p admin  --authenticationDatabase "admin"   

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值