1. 程式人生 > >Centos6.8 安裝mongo3.6以及許可權配置和開啟外網連結

Centos6.8 安裝mongo3.6以及許可權配置和開啟外網連結

目錄

安裝環境和版本說明,以及參考文件連結

  • 伺服器:阿里雲Centos6.8
  • Mongo版本:3.6
  • 官網文件
    • 安裝文件:
      https://docs.mongodb.com/manual/tutorial/install-mongodb-enterprise-on-red-hat/
    • 配置引數文件:
      https://docs.mongodb.com/manual/reference/configuration-options/index.html
  • 參考文章:
    • Mongo許可權配置:http://blog.csdn.net/wangdatao_/article/details/78077774
    • SELinux狀態檢視: http://blog.51cto.com/bguncle/957315

安裝MongoDB資料庫

  • 配置MongoDB的原始檔
[[email protected] ~]# sudo vim /etc/yum.repos.d/mongodb-enterprise.repo

然後鍵入以下內容

[mongodb-enterprise]
name=MongoDB Enterprise Repository
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/3.6/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
  • 下載安裝包,使用yum install進行安裝
[[email protected] ~]# sudo yum install -y mongodb-enterprise

執行MongoDB資料庫

  • 配置SELinux

    如果你使用了SELinux,那麼必須配置SELinux,不然會影響MongoDB的啟動。
    If you are using SELinux, you must configure SELinux to allow MongoDB to start on Red Hat Linux-based systems (Red Hat Enterprise Linux or CentOS Linux).

使用/usr/sbin/sestatus -v 命令檢視SELinux的狀態

[[email protected] ~]# /usr/sbin/sestatus -v 
SELinux status:                 disabled

如果是disabled的話就可以略過這一步了
如果是開啟狀態,那麼關閉SELinux即可

[[email protected] ~]# vim /etc/selinux/config

然後找到SELINUX改為disabled
`bash SELINUX=disabled

  • 執行MongoDB
    好了,現在可以先把mongo執行起來了。
[[email protected] ~]# sudo service mongod start
Starting mongod:                                           [  OK  ]

檢視 /var/log/mongodb/mongod.log,如果有
[initandlisten] waiting for connections on port 27017
說明MongoDB已經成功執行起來了。

[[email protected] ~]# tail -f /var/log/mongodb/mongod.log
  • 停止MongoDB
[[email protected] ~]# sudo service mongod stop
Stopping mongod:                                           [  OK  ]
  • 重啟MongoDB
[[email protected] ~]# sudo service mongod restart
Stopping mongod:                                           [  OK  ]
Starting mongod:                                           [  OK  ]
  • 進入MongoDB shell
[[email protected] ~]# mongo
MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.2
Server has startup warnings: 
2018-02-02T16:27:11.395+0800 I STORAGE  [initandlisten] 
2018-02-02T16:27:11.395+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-02-02T16:27:11.395+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] 
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] 
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] 
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] 
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] 
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2018-02-02T16:27:11.927+0800 I CONTROL  [initandlisten] 
>

刪除解除安裝MongoDB

[[email protected] ~]# sudo service mongod stop
[[email protected] ~]# sudo yum erase $(rpm -qa | grep mongodb-enterprise)
[[email protected] ~]# sudo rm -r /var/log/mongodb
[[email protected] ~]# sudo rm -r /var/lib/mongo

配置MongoDB管理員使用者

必須先配置管理員使用者,才能開啟許可權

在MongoDB shell命令列中執行以下命令

> use admin ;
> db.createUser({
...     user:"adminUser",
...     pwd:"thisisyourpass",
...     roles:[{role:"root",db:"admin"}]
... });

修改配置檔案,允許外網連結

[[email protected] ~]# vim /etc/mongod.conf

配置檔案如下

# 將bindIp改為0.0.0.0
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

# 開啟許可權認證
security:
  authorization: enabled

修改後的完整檔案如下:

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

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.


security:
  authorization: enabled

重啟MongoDB。

[[email protected] ~]# sudo service mongod restart
Stopping mongod:                                           [  OK  ]
Starting mongod:                                           [  OK  ]

安裝配置完成,使用Robo3T測試連結

總結

  • 其實整個安裝過程並不複雜,但是各種配置實在頭疼。
  • 官方文件真的是最棒的!但是看不懂英文就很噁心。