MongoDB 概述
软件介绍
• 介于关系数据库和非关系数据库之间的产品
– 一个基于分布式文件存储的数据库。
– 由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
– MongoDB 将数据存储为一个文档,数据结构由键值(key=>value) 对组成。
– MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。
软件特点
– 安装简单
– 面向文档存储,操作比较简单容易
– 支持丰富的查询表达
– 可以设置任何属性的索引
– 支持主流编程语言 RUBY|PYTHON|JAVA|PHP|C++
– 支持副本集,分片 //副本集(集群)
是一个开源软件 https://www.mongodb.com/
一台服务器,可以支持
部署mongodb 服务器:
1.装包:
[root@host51 ~]# mkdir - p /usr/local/mongodb
[root@host51 ~]# tar -zxf mongodb-linux-x86_64-rhel70-3.6.3.tgz
[root@host51 ~]# cd mongodb-linux-x86_64-rhel70-3.6.3/
[root@host51 mongodb-linux-x86_64-rhel70-3.6.3]# ls
bin GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES
[root@host51 mongodb-linux-x86_64-rhel70-3.6.3]# ls bin/
bsondump mongo mongodump mongofiles mongoperf mongorestore mongostat
install_compass mongod mongoexport mongoimport mongoreplay mongos mongotop
[root@host51 ~]# cp -r mongodb-linux-x86_64-rhel70-3.6.3/bin /usr/local/mongodb/
[root@host51 ~]# cd /usr/local/mongodb/
[root@host51 mongodb]# ls
bin
[root@host51 mongodb]# ls bin/
bsondump mongo mongodump mongofiles mongoperf mongorestore mongostat
install_compass mongod mongoexport mongoimport mongoreplay mongos mongotop
[root@host51 mongodb]# mkdir etc logs
[root@host51 mongodb]# mkdir -p data/db
[root@host51 mongodb]# ls
bin data etc log
[root@host51 bin]# vim /etc/profile
export PATH=/usr/local/mongodb/bin:$PATH
:wq
[root@host51 bin]# source /etc/profile
[root@host51 bin]# echo $PATH
/usr/local/mongodb/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
创建主配置文件:
[root@host51 mongodb]# vim /usr/local/mongodb/etc/mongodb.conf
logpath=/usr/local/mongodb/logs/mongodb.log
logappend=true# 追加的方式记录日志信息
dbpath=/usr/local/mongodb/data/db # 数据库目录
fork=true# 守护进程方式运行
:wq
守护进程方式运行:服务启动就在内存等者客户来访问
启动服务:
[root@host51 mongodb]# mongod -f /usr/local/mongodb/etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3097
child process started successfully, parent exiting
查看进程:
[root@host51 mongodb]# ps -C mongod
PID TTY TIME CMD
3097 ? 00:00:00 mongod
查看端口:
[root@host51 mongodb]# ss -ntulp | grep mongod
tcp LISTEN 0 128 127.0.0.1:27017 *:* users:(("mongod",pid=3097,fd=11))
停止服务:
[root@host51 mongodb]# mongod -f /usr/local/mongodb/etc/mongodb.conf --shutdown
killing process with pid: 3097
设置别名:
[root@host51 mongodb]# alias stopmgo='mongod -f /usr/local/mongodb/etc/mongodb.conf --shutdown'
[root@host51 mongodb]# alias startmgo='mongod -f /usr/local/mongodb/etc/mongodb.conf'
连接mongo服务器:(不修改ip和端口)
[root@host51 mongodb]# mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
指定IP和端口:
[root@host51 ~]# stopmgo //停止服务
killing process with pid: 3434
[root@host51 ~]# vim /usr/local/mongodb/etc/mongodb.conf
bind_ip=192.168.4.51 //指定IP
port=27051 //指定端口
:wq
[root@host51 ~]# startmgo //启动服务
about to fork child process, waiting until server is ready for connections.
forked process: 3760
child process started successfully, parent exiting
查看端口:
[root@host51 ~]# ss -ntulp | grep mongod
tcp LISTEN 0 128 192.168.4.51:27051 *:* users:(("mongod",pid=3760,fd=11))
查看帮助:
[root@host51 ~]# mongo -h | more //分页显示
连接mongo服务:
[root@host51 ~]# mongo --host 192.168.4.51 --port 27051