本文主要对MongoDB的基础安装步骤进行简要说明,分别为tgz包安装、RPM安装
基础材料:CentOS7.7 MongoDB4.2.1
tgz包安装:
安装前置包
[root@mongodb ~]# yum install libcurl openssl
官网下载并解压安装包
[root@mongodb ~]# tar -zxvf mongodb-linux-x86_64-rhel70-4.2.1.tgz
[root@mongodb ~]# mv mongodb-linux-x86_64-rhel70-4.2.1 /opt/mongodb
创建用户及数据、日志文件夹、PID文件夹
[root@mongodb ~]# groupadd mongod
[root@mongodb ~]# useradd -g mongod mongod
[root@mongodb ~]# mkdir -p /var/log/mongodb
[root@mongodb ~]# mkdir -p /data/mongodb
[root@mongodb ~]# mkdir -p /var/run/mongodb
[root@mongodb ~]# chown mongod:mongod /var/log/mongodb
[root@mongodb ~]# chown mongod:mongod /data/mongodb
[root@mongodb ~]# chown mongod:mongod /var/run/mongodb
禁用transparent_hugepage
[root@mongodb ~]# chmod 755 /etc/rc.d/rc.local
[root@mongodb ~]# echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.d/rc.local
[root@mongodb ~]# echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag" >> /etc/rc.d/rc.local
[root@mongodb ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@mongodb ~]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
配置环境变量
[root@mongodb ~]# echo "export PATH=/opt/mongodb/bin:$PATH">>/etc/profile
[root@mongodb ~]# source /etc/profile
[root@mongodb ~]# vi /etc/mongod.conf
# mongod.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /data/mongodb
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
启动mongodb
[root@mongodb ~]# mongod --config /etc/mongod.conf
RPM安装:
安装前置包
[root@mongodb ~]# yum install libcurl openssl
安装mongodb的4个RPM包
[root@mongodb ~]# rpm -ivh mongodb-org-server-4.2.1-1.el7.x86_64.rpm
[root@mongodb ~]# rpm -ivh mongodb-org-mongos-4.2.1-1.el7.x86_64.rpm
[root@mongodb ~]# rpm -ivh mongodb-org-shell-4.2.1-1.el7.x86_64.rpm
[root@mongodb ~]# rpm -ivh mongodb-org-tools-4.2.1-1.el7.x86_64.rpm
创建数据存储路径(默认存储在/var/lib/mongo下)
[root@mongodb ~]# mkdir -p /data/mongodb
[root@mongodb ~]# chown mongod:mongod /data/mongodb
禁用transparent_hugepage
[root@mongodb ~]# chmod 755 /etc/rc.d/rc.local
[root@mongodb ~]# echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.d/rc.local
[root@mongodb ~]# echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag" >> /etc/rc.d/rc.local
[root@mongodb ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@mongodb ~]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
修改配置文件中的默认数据存储路径以及绑定的IP地址
[root@mongodb ~]# sed -i "s/dbPath: \/var\/lib\/mongo/dbPath: \/data\/mongodb/g" /etc/mongod.conf
[root@mongodb ~]# sed -i "s/bindIp: 127.0.0.1/bindIp: 0.0.0.0/g" /etc/mongod.conf
启动mongodb
[root@mongodb ~]# systemctl start mongod
服务验证:
启动MongoDB命令行,启动时会有一些警告,因为在配置时未开启权限控制,本次只是简单安装可以忽略
[root@mongodb ~]# mongo
查看现有数据库
> show dbs
创建新数据库
>use testdb
插入数据
>db.testcollection.insert({a:1,b:2,c:3})
查看数据
> db.testcollection.find()
删除collection
> db.testcollection.drop()
删除数据库
> db.dropDatabase()