1. 程式人生 > >安裝MongoDB centos 7

安裝MongoDB centos 7

... 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服務: