简介

MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。

Packages包说明

MongoDB官方源中包含以下几个依赖包: mongodb-org: MongoDB元数据包,安装时自动安装下面四个组件包:

1、mongodb-org-server: 包含MongoDB守护进程和相关的配置和初始化脚本。

2、mongodb-org-mongos: 包含mongos的守护进程。

3、mongodb-org-shell: 包含mongo shell。

4、mongodb-org-tools: 包含MongoDB的工具: mongoimport, bsondump, mongodump, mongoexport, mongofiles, mongooplog, mongoperf, mongorestore, mongostat, and mongotop。

实验环境

系统版本:centos7x3.10.0-514.el7.x86_64

mongodb版本:mongodb-linux-x86_64-rhel70-3.6.6

关闭防火墙并禁止开机自启 systemctl stop firewalld.service systemctl disable firewalld

关闭selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

重启 reboot

安装MongoDB

Mongodb官网

Mongodb使用教程

Mongodb源码包 注:根据需求下载源码包即可!

1、前往Mongodb官网下载安装包(以下截图为下载路径)

//进入Mongodb官网(一定要使用兼容的浏览器否则可能打不开,例如火狐、谷歌等)

//单击选择社区服务器和linux系统

//单击左下方“Version下拉菜单”选择适合的版本

//最后根据需求下载指定版本的二进制文件并上传到服务器即可

2、解压Mongodb

1)查看压缩包 ls

2)解压mongodb-linux-x86_64-rhel70-3.6.6.tgz到指定目录

tar zxf mongodb-linux-x86_64-rhel70-3.6.6.tgz -C /usr/local/

3)进入解压目录

cd /usr/local/mongodb-linux-x86_64-rhel70-3.6.6/

3、创建Mongodb数据存储目录和日志存放目录

mkdir db log 注:这两个目录在后边的配置文件中会用到!

4、编写MongoDB配置文件

vi /usr/local/mongodb-linux-x86_64-rhel70-3.6.6/bin/mongodb.conf

systemLog:
    destination: file
    path: "/usr/local/mongodb-linux-x86_64-rhel70-3.6.6/log/mongodb.log"
    logAppend: true
storage:
    dbPath: "/usr/local/mongodb-linux-x86_64-rhel70-3.6.6/db/"
    journal:
         enabled: true
processManagement:
    fork: true
    pidFilePath: "/usr/local/mongodb-linux-x86_64-rhel70-3.6.6/mongodb.pid"
net:
    port: 27017
setParameter:
    enableLocalhostAuthBypass: false 

注:这块可能有童鞋就会问了?你这mongodb.pid是什么文件?怎么没有呢?别着急这pid文件你可以理解成服务启动或者关闭时的进程代号。为啥没有找到这个文件呢?是因为这个文件默认源码安装不存在,我们提前写好路径是为了之后启动mongodb时,系统会自动按照这个路径去创建这个pid文件。

5、创建mongodb启动的脚本

vi /etc/init.d/mongodb

#!/bin/sh
#chkconfig: 2345 80 90
#description: mongodb
start() { /usr/local/mongodb-linux-x86_64-rhel70-3.6.6/bin/mongod -f /usr/local/mongodb-linux-x86_64-rhel70-3.6.6/bin/mongodb.conf }

stop() { /usr/local/mongodb-linux-x86_64-rhel70-3.6.6/bin/mongod -f /usr/local/mongodb-linux-x86_64-rhel70-3.6.6/bin/mongodb.conf --shutdown }

case "$1" in start) start ;; stop) stop ;; restart) stop start ;; *) echo $"Usage: $0 {start|stop|restart}"
exit 1 esac

6、给mongodb启动脚本执行权限

chmod +x /etc/init.d/mongodb

7、设置启动服务

1)将mongodb设置开机自启

chkconfig --level 35 mongodb on

2)将mongodb注册为系统服务

chkconfig --add mongodb

3)启动mongodb服务

/etc/init.d/mongodb start

4)停止mongodb服务

/etc/init.d/mongodb stop

5)重启mongodb服务

/etc/init.d/mongodb restart

8、开启防火墙,打开27017端口

1)开启防火墙

systemclt start firewalld

2)永久打开27017端口

firewall-cmd --add-port=27017/tcp --permanent

3)重启防火墙使配置生效

systemctl restart firewalld

4)查看是否生效

firewall-cmd --list-all

9、配置环境变量

1)编辑环境变量并在最后添加一行文件如下

vi /etc/profile

export PATH=$PATH:/usr/local/mongodb-linux-x86_64-rhel70-3.6.6/bin

2)执行环境变量

source /etc/profile

10、测试mongodb是否安装成功

1)使用mongo命令进入到mongodb控制台

2)切换数据库管理用户

use admin 注:到这里就完成了mongodb数据库的×××!