# Install MongoDB Community Edition
# 先確認 SELINUX 跟 firewalld.service 都關掉了.
vi /etc/yum.repos.d/mongodb-org-3.6.repo # 新增一個 repository, 內容如下, 以 3.6 版本為例
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
yum install mongodb-org # 安裝 mongodb
mkdir -p /mgdata/appdb/run # 假設 mongodb 存放的資料路徑是 /mgdata/appdb, PID 路徑是 /mgdata/appdb/run
chown -R mongod:mongod /mgdata
vi /etc/mongod.conf # 修改各 path, appdb 為資料庫名稱
# path: /mgdata/appdb/mongod.log # systemLog 部份
# dbPath: /mgdata/appdb # storage 部份
# pidFilePath: /mgdata/appdb/run/mongod.pid # location of pidfile # processManagement 部份
# net: bindIpAll: true # 加 bindIpAll: true 然後 mark 掉 bindIp: https://www.linkedin.com/redir/invalid-link-page?url=127%2e0%2e0%2e1 ..... 這句
vi /usr/lib/systemd/system/mongod.service # check the path of PIDFile is the same as "pidFilePath" in /etc/mongod.conf
#### Disable Transparent Huge Pages (THP) ##############################################################################################
# Step 1: vi /etc/init.d/disable-transparent-hugepages # 內容如下
#!/bin/bash
### BEGIN INIT INFO
# Provides: disable-transparent-hugepages
# Required-Start: $local_fs
# Required-Stop:
# X-Start-Before: mongod mongodb-mms-automation-agent
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description: Disable Linux transparent huge pages, to improve
# database performance.
### END INIT INFO
case $1 in
start)
if [ -d /sys/kernel/mm/transparent_hugepage ]; then
thp_path=/sys/kernel/mm/transparent_hugepage
elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
thp_path=/sys/kernel/mm/redhat_transparent_hugepage
else
return 0
fi
echo 'never' > ${thp_path}/enabled
echo 'never' > ${thp_path}/defrag
re='^[0-1]+$'
if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
then
# RHEL 7
echo 0 > ${thp_path}/khugepaged/defrag
else
# RHEL 6
echo 'no' > ${thp_path}/khugepaged/defrag
fi
unset re
unset thp_path
;;
esac
# Step 2: chmod 755 /etc/init.d/disable-transparent-hugepages # 將 script 改為可執行權限
# Step 3: chkconfig --add disable-transparent-hugepages # 讓 script 在開機階段被執行
# tuned and ktune are dynamic kernel tuning tools available on Red Hat and CentOS that can disable transparent huge pages.
# Step 4: mkdir /etc/tuned/no-thp # 創建一個資料夾
# Step 5: vi /etc/tuned/no-thp/tuned.conf # 內容如下
[main]
include=virtual-guest
[vm]
transparent_hugepages=never
# Step 6: tuned-adm profile no-thp # enable the new profile
# Step 7: cat /sys/kernel/mm/transparent_hugepage/enabled # check the status of THP support, 結果要是 always madvise [never]
cat /sys/kernel/mm/transparent_hugepage/defrag # check the status of THP support, 結果要是 [always] madvise never
####################################################################################################################################
systemctl start mongod # 啟動 mongodb
mongo # run mongodb-org-shell
## 建立管理帳號 admin 及密碼, 設定完要下上 db.
> use admin
> db.createUser(
{
user: "admin",
pwd: "admin",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
> exit
vi /etc/mongod.conf # 打開 security 項目讓 authorization: enabled 生效, 若沒有 authorization: enabled 這行則自行於下一行補上, 例如:
security:
authorization: enabled
## 然後下上 db 讓設定生效
systemctl stop mongod
systemctl enable mongod # 讓開機自動啟動 mongod.service
systemctl daemon-reload
systemctl start mongod
######################################################################
This document was created by 陳兆康 (Chao-Kang Chen) (國泰世華銀行 DBA)
留言列表