NUMA Settings linux
Running MongoDB on a system with Non-Uniform Access Memory (NUMA) can cause a number of operational problems, including slow performance for periods of time and high system process usage.mongodb
sysctl -w vm.zone_reclaim_mode=0
Kernel and File Systems數據庫
When running MongoDB in production on Linux, you should use Linux kernel version 2.6.36 or later, with either the XFS or EXT4 filesystem. If possible, use XFS as it generally performs better with MongoDB.vim
XFS
is the default file system for CentOS7.centos
DNS settingsapp
cat > /etc/resolv.conf << EOF nameserver 172.20.224.134 EOF
NTP Settingsdom
Use the Network Time Protocol (NTP) to synchronize time among your hosts. This is especially important in sharded clusters.ssh
yum -y install chrony #sync time from local time server sed -i '/^server*/d' /etc/chrony.conf sed -i -e '/^# Please consider*/a\server ntp01' /etc/chrony.conf systemctl enable chronyd && systemctl restart chronyd
Turn off Atimeide
Turn off atime for the storage volume containing the database files.
add mount option "noatime,nodiratime" in /etc/fstabthis
vim /etc/fstab /dev/mapper/VolGroup-lv_data /data xfs defaults,noatime,nodiratime 1 1
ulimt settings
Set the file descriptor limit, -n, and the user process limit (ulimit), -u, above 20,000, according to the suggestions in the ulimit reference.
#check ulimit ulimit -a #set ulimit for mongod cat >> /etc/security/limits.conf << EOF mongod soft nproc 65535 mongod hard nproc 65535 EOF
Disable Transparent Huge Pages
MongoDB performs better with normal (4096 bytes) virtual memory pages.
echo never > /sys/kernel/mm/transparent_hugepage/enabled; echo never > /sys/kernel/mm/transparent_hugepage/defrag; chmod +x /etc/rc.d/rc.local
For CentOS 6
, refer to Disable Transparent Huge Pages
Disable selinux
It's better to set selinux permissive
ranther than disabled
sed -i s/^SELINUX=.*/SELINUX=permissive/g /etc/selinux/config
Disable iptables
service iptables stop && chkconfig iptables off service ip6tables stop && chkconfig ip6tables off
sshd tunning
disable dns search when connecting to this server;
disable PasswordAuthentication confirm when ssh to other servers.
sed -i 's/^#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config sed -i 's/^#PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
Set the readahead setting
Setting a higher readahead benefits sequential I/O operations. However, since MongoDB disk access patterns are generally random, setting a higher readahead provides limited benefit or performance degradation.
For the WiredTiger storage engine, a readahead of 0 or 16 provides optimal MongoDB performance.
For the MMAPv1 storage enginem, A readahead of 32 (16 kB) often works well. #get the readahead settings of block device blockdev --report #change the readahead settings blockdev --setra <value> <device> blockdev --setra 256 /dev/xvda
設置安裝yum源
國內使用阿里雲鏡像源
cat > /etc/yum.repos.d/mongodb-org-3.4.repo << EOF [mongodb-org-3.4] name=MongoDB Repository #baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/ baseurl=https://mirrors.aliyun.com/mongodb/yum/redhat/\$releasever/mongodb-org/3.4/x86_64/ gpgcheck=0 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc EOF
安裝mongodb
yum -y install mongodb-org systemctl enable mongod systemctl start mongod
修改默認配置(可選)
同時註釋掉只監聽localhost的設置:
mkdir -pv /data/mongodb/storage chown -R mongod /data/mongodb/storage
vim /etc/mongod.conf
# Where and how to store data. storage: dbPath: /data/mongodb/storage #修改成數據存儲目錄,並確保mongod用戶可讀寫 journal: enabled: true # network interfaces net: port: 27017 # bindIp: 127.0.0.1 #默認監聽本地lo,註釋掉interfaces.
systemctl restart mongod
建立數據庫
#登陸數據庫 mongo #建立數據庫dashboarddb use dashboarddb #Create db user db.createUser( { user: "dashboarduser", pwd: "dbpassword", roles: [ {role: "readWrite", db: "dashboarddb"} ] })
centos6 安裝mongodb請參考
http://wiki.timanetwork.com/pages/viewpage.action?pageId=3507421