Mongodb 初探

697次阅读
没有评论

共计 5129 个字符,预计需要花费 13 分钟才能阅读完成。

简介

Mongodb 初探

Mongodb是一个面向文档的非关系数据库,是非关系数据库中功能最丰富,又最像关系数据库的。它所存储的的数据格式是类似json的bson格式,所以可以存储复杂的数据模型。

Mongodb的基本概念

  • 文档: 文档是Mongodb中的基本数据单位,类似于Mysql 表中的行
    • Key: 对应Mysql中表中的字段
    • Value: 对应Mysql中表中的字段值
  • 集合: 集合是一组文档,类似于Mysql的表
    • 由多个文档组合成一个集合
    • 集合内的每个文档结构可以不一样
  • 数据库: 多个集合组成数据,和Mysql中的库类似
    • Admin 库 :一个权限数据库,如果创建用户的时候将该用户添加到admin 数据库中,那么该用户就自动继承了所有数据库的权限
    • Local 库:这个数据库永远不会被复制,可以用来存储本地单台服务器的任意集合
    • Config 库:当MongoDB 使用分片模式时,config 数据库在内部使用,用于保存分片的信息

适用场景

Mongddb是一个高性能高并发读写的非关系型数据库,通常应用于商城商品、评论或IoT等场景。

目前博主遇到的便是商城用户评论和物联网数据日志采集的o( ̄┰ ̄*)ゞ

Mongodb部署

相关工具

官网:https://www.mongodb.com/
安装包下载地址:https://www.mongodb.com/try/download/community
windows客户端工具:https://downloads.mongodb.com/compass/mongodb-compass-1.30.1-win32-x64.zip

初始化环境

# 取消大页缓存
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

安装Mongodb

# 创建用户和组
groupadd -g 800 mongod
useradd -u 801 -g mongod mongod
passwd mongod
# 创建mongodb所需目录结构
mkdir -p /mongodb/bin
mkdir -p /mongodb/conf
mkdir -p /mongodb/log
mkdir -p /mongodb/data
# 解压mongodb
tar xf /usr/local/src/mongodb-linux-x86_64-rhel70-3.2.16.tgz
cp -a /usr/local/src/mongodb-linux-x86_64-rhel70-3.2.16/bin/* /mongodb/bin
# 设置目录结构权限
chown -R mongod:mongod /mongodb
# 设置用户环境变量
su - mongod
vi .bash_profile

...
export PATH=/mongodb/bin:$PATH
...

# 加载环境变量
source .bash_profile

创建Mongodb实例

# 可直接命令行启动:mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork
# 建议使用配置文件方式启动,先切换用户
[root@db01 ~]# su - mongod
Last login: Sat Mar 30 21:08:40 CST 2019 on pts/0
[mongod@db01 ~]$ cat > /mongodb/conf/mongodb.conf<<EOF
logpath=/mongodb/log/mongodb.log
dbpath=/mongodb/data 
port=27017
logappend=true
fork=true
EOF

# 启动
[mongod@db01 ~]$ mongod -f /mongodb/conf/mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 3754
child process started successfully, parent exiting

查看实例状态


# 默认使用27017端口
[mongod@db01 ~]$ netstat -tunlp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      3754/mongod         
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::111                  :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
udp        0      0 0.0.0.0:1019            0.0.0.0:*                           -                   
udp        0      0 0.0.0.0:111             0.0.0.0:*                           -                   
udp6       0      0 :::1019                 :::*                                -                   
udp6       0      0 :::111                  :::*                                -  
# 登录
[mongod@db01 ~]$ mongo
MongoDB shell version v3.6.11-23-g0522785
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3262d22d-84b0-4c6c-aa12-470633b2eabf") }
MongoDB server version: 3.6.11-23-g0522785
Server has startup warnings: 
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] 
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] 
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] 
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] 
2019-03-30T21:10:28.597+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65536 files. Number of processes should be at least 32768 : 0.5 times number of files.
> 

# 关闭mongod
[mongod@db01 ~]$ mongod -f /mongodb/conf/mongodb.conf  --shutdown
killing process with pid: 3754

Mongodb配置文件

# 配置文件可以是YAML格式,例如:
systemLog:
   destination: file        
   path: "/mongodb/log/mongodb.log"    -- 日志位置
   logAppend: true					   -- 日志以追加模式记录

-- 数据存储有关   
storage:
   journal:
      enabled: true
   dbPath: "/mongodb/data"            -- 数据路径的位置
   
-- 进程控制  
processManagement:
   fork: true                         -- 后台守护进程
   pidFilePath: <string>			  -- pid文件的位置,一般不用配置,可以去掉这行,自动生成到data中
   
   
-- 网络配置有关   
net:			
   bindIp: <ip>                       -- 监听地址,如果不配置这行是监听在0.0.0.0
   port: <port>						  -- 端口号,默认不配置端口号,是27017
   
-- 安全验证有关配置      
security:
  authorization: enabled              -- 是否打开用户名密码验证
  
# 配置示例
[mongod@db01 /mongodb/conf]$ cat mongodb.conf 
systemLog:
  destination: file
  path: "/mongodb/log/mongodb.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: "/mongodb/data/"
processManagement:
  fork: true
net:
  bindIp: 10.0.0.51
  port: 27017

Mongodb简单操作

# DB级别命令
db.[TAB]  类似于linux中的tab功能
db.help() db级别的命令使用帮助
collection级别操作
db.Collection_name.xxx
document级别操作:
db.t1.insert()

# 复制集有关(replication set):
rs.xxx

# 分片集群(sharding cluster)
sh.xxxx

# 常用操作
# 查看版本
> db.version()
3.6.11-23-g0522785

# 切换库
> use local
switched to db local

# 查看表
> show tables;
startup_log

# tab命令补全
> db.sta
db.startup_log  db.stats(

# 查看库信息
> db.stats()
{
	"db" : "local",
	"collections" : 1,
	"views" : 0,
	"objects" : 3,
	"avgObjSize" : 1655.6666666666667,
	"dataSize" : 4967,
	"storageSize" : 36864,
	"numExtents" : 0,
	"indexes" : 1,
	"indexSize" : 36864,
	"fsUsedSize" : 15669313536,
	"fsTotalSize" : 50432839680,
	"ok" : 1
}
> 

# 查看当前连接
> db.getMongo()
connection to 10.0.0.51:27017

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2020-05-11发表,共计5129字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)