Create a gist now

Instantly share code, notes, and snippets.

@seiyamaeda /CLI
Last active Apr 9, 2018

Embed
What would you like to do?
How to install MongoDB in CentOS7
#############################################################################
# via: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ #
# via: https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/ #
# via: https://qiita.com/SOJO/items/dc5bf9b4375eab14991b #
#############################################################################
$ vi /etc/yum.repos.d/mongodb-org-3.6.repo
[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 -y mongodb-org
$ systemctl start mongod
$ systemctl status mongod
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2018-04-05 16:58:01 JST; 16s ago
Docs: https://docs.mongodb.org/manual
Process: 13242 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Process: 13241 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 13240 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 13239 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 13244 (mongod)
CGroup: /docker/**********/system.slice/mongod.service
└─13244 /usr/bin/mongod -f /etc/mongod.conf
‣ 13244 /usr/bin/mongod -f /etc/mongod.conf
$ mongo --version
MongoDB shell version v3.6.3
git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
$ systemctl enable mongod.service
$ systemctl status mongod.service
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2018-04-05 16:58:01 JST; 7min ago
Docs: https://docs.mongodb.org/manual
Main PID: 13244 (mongod)
CGroup: /docker/**********/system.slice/mongod.service
└─13244 /usr/bin/mongod -f /etc/mongod.conf
‣ 13244 /usr/bin/mongod -f /etc/mongod.conf
Apr 05 16:58:01 a7e77c30f6d8 systemd[1]: Starting High-performance, schema-free document-oriented database...
Apr 05 16:58:01 a7e77c30f6d8 mongod[13242]: about to fork child process, waiting until server is ready for connections.
Apr 05 16:58:01 a7e77c30f6d8 mongod[13242]: forked process: 13244
Apr 05 16:58:01 a7e77c30f6d8 systemd[1]: Started High-performance, schema-free document-oriented database.
# When I log in to MongoDB for the first time, I had encountered 3 errors, so I tried fix them
$ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Server has startup warnings:
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten]
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten]
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten]
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten]
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-04-05T16:58:01.971+0900 I CONTROL [initandlisten]
# Create disable-transparent-hugepages
$ 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
$ chmod 755 /etc/init.d/disable-transparent-hugepages
$ chkconfig --add disable-transparent-hugepages
$ chkconfig --list
disable-transparent-hugepages 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# Create Security Key
$ mkdir -p /usr/local/mongodb/conf
$ openssl rand -base64 741 > /usr/local/mongodb/conf/mongodb-keyfile
$ chmod 600 /usr/local/mongodb/conf/mongodb-keyfile
$ chown -R mongod.mongod /usr/local/mongodb/conf/mongodb-keyfile
$ vi /etc/mongod.conf
security:
keyFile: /usr/local/mongodb/conf/mongodb-keyfile
$ systemctl restart mongod
$ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
# to view log messages
$ tail -f /var/log/mongodb/mongod.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment