数据库运维
记录DBA学习成长历程

MongoDB安装部署及配置

安装前的准备

(1)redhat或centos6.2以上系统
(2)系统开发包完整
(3)ip地址和hosts文件解析正常
(4)iptables防火墙&SElinux关闭
(5)关闭大页内存机制
########################################################################
root用户下
在vi /etc/rc.local最后添加如下代码
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
[root@db01 ~]# sh /etc/rc.local
验证是否成功:
[root@db01 ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
[root@db01 ~]# cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]

其他系统关闭参照官方文档:

https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/
---------------
为什么要关闭大页内存机制?
Transparent Huge Pages (THP) is a Linux memory management system 
that reduces the overhead of Translation Lookaside Buffer (TLB) 
lookups on machines with large amounts of memory by using larger memory pages.
However, database workloads often perform poorly with THP, 
because they tend to have sparse rather than contiguous memory access patterns. 
You should disable THP on Linux machines to ensure best performance with MongoDB.
############################################################################

mongodb安装

创建所需用户和组

useradd mongod
passwd mongod

创建mongodb所需目录结构

mkdir -p /mongodb/conf
mkdir -p /mongodb/log
mkdir -p /mongodb/data

上传并解压软件到指定位置

cd /data
tar xf mongodb-linux-x86_64-rhel70-3.6.12.tgz
cp -r /data/mongodb-linux-x86_64-rhel70-3.6.12.tgz/bin/ /mongodb

设置目录结构权限

chown -R mongod:mongod /mongodb

设置用户环境变量

su - mongod
vi .bash_profile
export PATH=/mongodb/bin:$PATH
source .bash_profile

启动mongodb

mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork 
--logappend追加形式记录日志
--fork后台运行

mongodb配置文件

YAML模式

cat > /mongodb/conf/mongo.conf <<EOF
systemLog:
  destination: file
  path: "/mongodb/log/mongodb.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: "/mongodb/data/"
processManagement:
  fork: true
net:
  port: 27017
  bindIp: 10.0.0.200,127.0.0.1
EOF
mongod -f /mongodb/conf/mongo.conf --shutdown
mongod -f /mongodb/conf/mongo.conf

--系统日志有关

systemLog:
  destination: file 
  path: "/mongodb/log/mongodb.log" --日志位置
  logAppend: true --日志以追加模式记录

--数据存储有关

storage:
  journal:
    enabled: true
  dbPath: "/mongodb/data" --数据路径的位置

-- 进程控制

processManagement:
  fork: true --后台守护进程
  pidFilePath: <string> --pid文件的位置,一般不用配置,可以去掉这行,自动生成到data中

--网络配置有关

net: 
  bindIp: <ip> -- 监听地址
  port: <port> -- 端口号,默认不配置端口号,是27017

-- 安全验证有关配置

security:
  authorization: enabled --是否打开用户名密码验证

与分片集群有关的复制集

replication:
  oplogSizeMB: <NUM>
  replSetName: "<REPSETNAME>"
  secondaryIndexPrefetch: "all"

sharding:
  clusterRole: <string>
  archiveMovedChunks: <boolean>

---for mongos only
replication:
  localPingThresholdMs: <int>

sharding:
  configDB: <string>

mongodb 使用systemd管理

[root@db01 ~]# cat > /etc/systemd/system/mongod.service <<EOF
[Unit]
Description=mongodb 
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongod
Type=forking
ExecStart=/mongodb/bin/mongod --config /mongodb/conf/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/mongodb/bin/mongod --config /mongodb/conf/mongo.conf --shutdown
PrivateTmp=true 
[Install]
WantedBy=multi-user.target
EOF
[root@db01 ~]# systemctl restart mongod
[root@db01 ~]# systemctl stop mongod
[root@db01 ~]# systemctl start mongod
赞(1)
MySQL学习笔记 » MongoDB安装部署及配置