1,MongoDB 简介及安装部署
MongoDB 逻辑结构:
MongoDB 逻辑结构 MySQL 逻辑结构
库 database 库
集合 (collection) 表
文档 (document) 数据行
2, 安装前准备
(1)RedHat 或 cnetos6.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
其他系统关闭参照官方文档:
为什么要关闭?
- 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.
##############################################
3,MongoDB 安装
(1) 创建所需用户和组
- groupadd -g 800 mongod
- useradd -u 801 -g mongod mongod
- passwd mongod
(2) 创建 MongoDB 所需目录结构
- mkdir -p /MongoDB/bin
- mkdir -p /MongoDB/conf
- mkdir -p /MongoDB/log
- mkdir -p /MongoDB/data
(3) 上传并解压软件到指定位置
上传到:
cd /server/tools/
解压:
tar xf MongoDB-Linux-x86_64-rhel70-3.2.16.tgz
拷贝目录下 bin 程序到 / MongoDB/bin
cp -a /server/tools/MongoDB-Linux-x86_64-rhel70-3.2.16/bin/*/MongoDB/bin
(4) 设置目录结构权限
chown -R mongod:mongod /MongoDB
(5) 设置用户环境变量
- su - mongod
- vi .bash_profile
- export PATH=/MongoDB/bin:$PATH
- source .bash_profile
(6) 启动 MongoDB
mongod --dbpath=/MongoDB/data --logpath=/MongoDB/log/MongoDB.log --port=27017 --logappend --fork
(7) 登录 MongoDB
[mongod@server2 ~]$ mongo
(8) 使用配置文件
- VIM /MongoDB/conf/MongoDB.conf
- logpath=/MongoDB/log/MongoDB.log
- dbpath=/MongoDB/data
- port=27017
- logappend=true
- fork=true
- +++++++++++++++++++
关闭 MongoDB
mongod -f /MongoDB/conf/MongoDB.conf --shutdown
使用配置文件启动 MongoDB
- mongod -f /MongoDB/conf/MongoDB.conf
- (YAML 模式:)
- NOTE:
- YAML does not support tab characters for indentation: use spaces instead.
-- 系统日志有关
- 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> -- 监听地址, 如果不配置这行是监听在 0.0.0.0
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>
- .........
- ++++++++++++++++++++++
YAML 配置文件例子
- VIM /MongoDB/conf/mongo.conf
- systemLog:
- destination: file
- path: "/mongodb/log/mongodb.log"
- logAppend: true
- storage:
- journal:
- enabled: true
- dbPath: "/mongodb/data/"
- processManagement:
- fork: true
- net:
- port: 27017
- mongod -f /MongoDB/conf/mongo.conf --shutdown
- mongod -f /MongoDB/conf/mongo.conf
- ++++++++++++++++++++++
(9)MongoDB 的关闭方式
mongod -f MongoDB.conf --shutdown
来源: http://www.bubuko.com/infodetail-2963110.html