MongoDB基础(二)—搭建

1.1 MongoDB单实例搭建

1.1.1 系统环境准备

提示:虚拟机内存需要4G+
  • redhat或2以上系统
[root@db01 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@db01 ~]# uname -r
2.6.32-696.el6.x86_64
  • 系统开发包完整
[root@db01 ~]# 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文件解析正常
[root@db01 ~]# hostname -I
10.0.0.41 172.16.1.41
  • iptables防火墙&SElinux关闭
[root@db01 ~]# service iptables status
iptables: Firewall is not running.
[root@db01 ~]# getenforce
Disabled

1.1.2 关闭大页内存机制

[root@db01 ~]# 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
[root@db01 ~]# reboot           # 重启系统生效
提示:其他系统关闭参照官方文档:https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/

1.1.3 创建用户及用户组

[root@db01 ~]# useradd -u900 mongod     # 用户需要使用密码登录,所以不设置成虚拟用户
[root@db01 ~]# id mongod
uid=900(mongod) gid=900(mongod) groups=900(mongod)

1.1.4 设置mongod密码

[root@db01 ~]# passwd mongod
Changing password for user mongod.
New password: 123456
Retype new password: 123456
passwd: all authentication tokens updated successfully.

1.1.5 创建mongodb安装及功能目录

[root@db01 ~]# mkdir -p /usr/local/mongodb/{conf,log,data}

1.1.6 二进制安装mongodb

[root@db01 ~]# cd /server/tools/
[root@db01 tools]# tar xf mongodb-linux-x86_64-3.2.8.tgz
[root@db01 tools]# cp -a mongodb-linux-x86_64-3.2.8/bin /usr/local/mongodb/

1.1.7 设置目录权限

[root@db01 ~]# chown -R mongod:mongod /usr/local/mongodb/

1.1.8 设置mongod用户的环境变量

[root@db01 ~]# su - mongod
[mongod@db01 ~]$ echo "export PATH=/usr/local/mongodb/bin:$PATH" >> .bash_profile
[mongod@db01 ~]$ source .bash_profile

1.1.9 运行mongodb服务端

1.1.9.1 配置文件方式

  • 普通配置文件模板:
logptah=        # 日志文件路径
dbpath=         # 数据存放路径
port=           # 启用端口号
logappend=      # 日志输出方式
fork=           # 在后台运行
auth=           # 是否需要验证权限登录(用户名和密码)
bind_ip=        # 限制访问的ip
  • 编写普通配置文件:
[mongod@db01 ~]$ 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配置文件:
[mongod@db01 ~]$ 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
  • 调用配置文件:
[mongod@db01 ~]$ 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
# 或
[mongod@db01 ~]$ 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 命令行方式

[mongod@db01 ~]$ mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/log/mongodb.log --port=27017 --logappend --fork

1.1.10 编写服务端启动脚本(可选)

[root@db01 ~]# 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客户端

[mongod@db01 ~]$ 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信号,会做一些处理:
  1. 关闭所有打开的连接
  2. 将内存数据强制刷新到磁盘
  3. 当前的操作执行完毕
  4. 安全停止
注意:切记使用kill -9命令,否则数据库直接关闭可能导致数据丢失、数据文件损失等情况,并且修复数据库成本高,有风险。

1.1.12.2 内置模式

[mongod@db01 ~]$ mongo
> use admin;
> db.shutdownServer()       # 或db.adminCommand({shutdown:1})

1.1.12.3 命令行模式

[mongod@db01 ~]$ mongod -f /usr/local/mongodb/conf/mongod.conf --shutdown

1.2 MongoDB多实例搭建

1.2.1 创建多实例目录

[mongod@db01 ~]$ su - mongod
[mongod@db01 ~]$ mkdir -p /usr/local/mongodb/27018/{conf,data,log}

1.2.2 创建配置文件

[mongod@db01 ~]$ 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 运行多实例

[mongod@db01 ~]$ 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 连接多实例

[mongod@db01 ~]$ 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
温馨提示:本文最后更新于2022-12-20 20:57:50,已超过431天没有更新。某些文章具有时效性,若文章内容或图片资源有错误或已失效,请联系站长。谢谢!
转载请注明本文链接:https://blog.leonshadow.cn/763482/827.html
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享