1.1 MongoDB单实例搭建
1.1.1 系统环境准备
提示:虚拟机内存需要4G+
- redhat或2以上系统
[[email protected] ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [[email protected] ~]# uname -r 2.6.32-696.el6.x86_64
- 系统开发包完整
[[email protected] ~]# ll /server/tools/mongodb-linux-x86_64-3.2.8.tgz -rw-r--r-- 1 root root 75154873 Nov 29 10:22 /server/tools/mongodb-linux-x86_64-3.2.8.tgz
- ip地址和hosts文件解析正常
[[email protected] ~]# hostname -I 10.0.0.41 172.16.1.41
- iptables防火墙&SElinux关闭
[[email protected] ~]# service iptables status iptables: Firewall is not running. [[email protected] ~]# getenforce Disabled
1.1.2 关闭大页内存机制
[[email protected] ~]# cat >> /etc/rc.local << EOF 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 EOF [[email protected] ~]# reboot # 重启系统生效
提示:其他系统关闭参照官方文档:https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/
1.1.3 创建用户及用户组
[[email protected] ~]# useradd -u900 mongod # 用户需要使用密码登录,所以不设置成虚拟用户 [[email protected] ~]# id mongod uid=900(mongod) gid=900(mongod) groups=900(mongod)
1.1.4 设置mongod密码
[[email protected] ~]# passwd mongod Changing password for user mongod. New password: 123456 Retype new password: 123456 passwd: all authentication tokens updated successfully.
1.1.5 创建mongodb安装及功能目录
[[email protected] ~]# mkdir -p /usr/local/mongodb/{conf,log,data}
1.1.6 二进制安装mongodb
[[email protected] ~]# cd /server/tools/ [[email protected] tools]# tar xf mongodb-linux-x86_64-3.2.8.tgz [[email protected] tools]# cp -a mongodb-linux-x86_64-3.2.8/bin /usr/local/mongodb/
1.1.7 设置目录权限
[[email protected] ~]# chown -R mongod:mongod /usr/local/mongodb/
1.1.8 设置mongod用户的环境变量
[[email protected] ~]# su - mongod [[email protected] ~]$ echo "export PATH=/usr/local/mongodb/bin:$PATH" >> .bash_profile [[email protected] ~]$ source .bash_profile
1.1.9 运行mongodb服务端
1.1.9.1 配置文件方式
- 普通配置文件模板:
logptah= # 日志文件路径 dbpath= # 数据存放路径 port= # 启用端口号 logappend= # 日志输出方式 fork= # 在后台运行 auth= # 是否需要验证权限登录(用户名和密码) bind_ip= # 限制访问的ip
- 编写普通配置文件:
[[email protected] ~]$ vim /usr/local/mongodb/conf/mongod.conf logpath=/usr/local/mongodb/log/mongodb.log dbpath=/usr/local/mongodb/data port=27017 logappend=1 fork=true auth=true # 设置数据库密码时添加此项
- YML配置文件模板:
-- NOTE: YAML does not support tab characters for indentation: use spaces instead. -- systemLog: destination: file path: "/mongodb/log/mongod.log" logAppend: true storage: journal: enabled: true dbPath: "<PATH>" processManagement: fork: true pidFilePath: <string> net: bindIp: <ip> port: <port> setParameter: enableLocalhostAuthBypass: false security: authorization: enabled replication: oplogSizeMB: <NUM> replSetName: "<REPSETNAME>" secondaryIndexPrefetch: "all" sharding: clusterRole: <string> archiveMovedChunks: <boolean> ---for mongos only replication: localPingThresholdMs: <int> sharding: configDB: <string> --- .........
- 编写YML配置文件:
[[email protected] ~]$ vim /usr/local/mongodb/conf/mongod.yml systemLog: destination: file path: "/usr/local/mongodb/log/mongod.log" logAppend: true processManagement: fork: true storage: journal: enabled: true dbPath: "/usr/local/mongodb/data" net: port: 27017 security: # 设置数据库密码时添加此项 authorization: enabled
- 调用配置文件:
[[email protected] ~]$ mongod -f /usr/local/mongodb/conf/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 1681 child process started successfully, parent exiting # 或 [[email protected] ~]$ mongod -f /usr/local/mongodb/conf/mongod.yml about to fork child process, waiting until server is ready for connections. forked process: 1736 child process started successfully, parent exiting
1.1.9.2 命令行方式
[[email protected] ~]$ mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/log/mongodb.log --port=27017 --logappend --fork
1.1.10 编写服务端启动脚本(可选)
[[email protected] ~]# vim /etc/init.d/mongod #!/bin/bash # #chkconfig: 2345 80 90 #description:mongodb MONGODIR=/usr/local/mongodb MONGOD=$MONGODIR/bin/mongod #MONGO=$MONGODIR/bin/mongo #DBDIR=$MONGODIR/data/data27017 #LOGPATH=$MONGODIR/log/mongodb.log MONGOCONF=$MONGODIR/conf/mongod.conf start() { su - mongod -c "$MONGOD -f $MONGOCONF" } stop() { su - mongod -c "$MONGOD -f $MONGOCONF --shutdown" } case "$1" in start) start ;; stop) stop ;; restart) stop sleep 2 start ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 esac
1.1.11 运行mongodb客户端
[[email protected] ~]$ mongo MongoDB shell version: 3.2.8 connecting to: test >
1.1.12 关闭mongodb服务端
1.1.12.1 kill方法
kill -2 PID # -2表示向mongod进程发送SIGINT信号。 kill -4 PID # -4表示向mognod进程发送SIGTERM信号。
- mongod进程收到SIGINT信号或者SIGTERM信号,会做一些处理:
- 关闭所有打开的连接
- 将内存数据强制刷新到磁盘
- 当前的操作执行完毕
- ...
- 安全停止
注意:切记使用kill -9命令,否则数据库直接关闭可能导致数据丢失、数据文件损失等情况,并且修复数据库成本高,有风险。
1.1.12.2 内置模式
[[email protected] ~]$ mongo > use admin; > db.shutdownServer() # 或db.adminCommand({shutdown:1})
1.1.12.3 命令行模式
[[email protected] ~]$ mongod -f /usr/local/mongodb/conf/mongod.conf --shutdown
1.2 MongoDB多实例搭建
1.2.1 创建多实例目录
[[email protected] ~]$ su - mongod [m[email protected] ~]$ mkdir -p /usr/local/mongodb/27018/{conf,data,log}
1.2.2 创建配置文件
[[email protected] ~]$ vim /usr/local/mongodb/27018/conf/mongod.yml systemLog: destination: file path: "/usr/local/mongodb/27018/log/mongod.log" logAppend: true processManagement: fork: true storage: journal: enabled: true dbPath: "/usr/local/mongodb/27018/data" net: port: 27018
1.2.3 运行多实例
[[email protected] ~]$ mongod -f /usr/local/mongodb/27018/conf/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 2025 child process started successfully, parent exiting
1.2.4 连接多实例
[[email protected] ~]$ mongo 127.0.0.1:27018 MongoDB shell version: 3.2.8 connecting to: 127.0.0.1:27018/test > db.getMongo() connection to 127.0.0.1:27018

我的微信
如果有技术上的问题可以扫一扫我的微信