十五章NoSQL-MongoDB核心技术

本文详细介绍了MongoDB的安装部署,包括系统准备、配置文件设置、用户及权限管理。深入讲解了复制集RS的原理与配置过程,包括基本原理、配置过程和管理操作。此外,还探讨了MongoDB的分片集群,包括Sharding Cluster的规划、配置和管理。最后,讨论了备份恢复策略,涉及mongodump、mongorestore的使用及高级应用。
摘要由CSDN通过智能技术生成

第一章:逻辑结构

Mongodb 逻辑结构                         MySQL逻辑结构
库database                                 库
集合(collection)                          表
文档(document)                            数据行
选择之所以称之为选择,肯定是痛苦的!
                   

在这里插入图片描述

第二章:安装部署

1、系统准备

(1)redhat或centos6.2以上系统
(2)系统开发包完整(c写的)
(3)ip地址和hosts文件解析正常
(4)iptables防火墙&SElinux关闭
(5)关闭大页内存机制
########################################################################
关闭大业内存
root用户下
在vi /etc/rc.local最后添加如下代码=======开机自启动
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

启动
[root@db01 data]# sh /etc/rc.local 
查看
[root@db01 data]# cat  /sys/kernel/mm/transparent_hugepage/enabled  
always madvise [never]
[root@db01 data]# cat /sys/kernel/mm/transparent_hugepage/defrag  
always madvise [never]

其他系统关闭参照官方文档:   

https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/
---------------
为什么要关闭?
Transparent Huge Pages (THP) is a Linux memory management system 
that reduces the overhead of Translation Lookaside Buffer (TLB) 
lookups on machines with large amounts of memory by using larger memory pages.
However, database workloads often perform poorly with THP, 
because they tend to have sparse rather than contiguous memory access patterns. 
You should disable THP on Linux machines to ensure best performance with MongoDB.
############################################################################    

2、mongodb安装

创建所需用户和组

useradd mongod
passwd mongod

创建mongodb所需目录结构

mkdir -p /mongodb/conf
mkdir -p /mongodb/log
mkdir -p /mongodb/data

上传并解压软件到指定位置

下载地址:wget https://www.mongodb.com/download-center/community/releases
[root@db01 data]# cd   /data
[root@db01 data]# tar xf mongodb-linux-x86_64-rhel70-3.6.12.tgz ======二进制包
启动文件推送到目录
[root@db01 data]#  cp -r /data/mongodb-linux-x86_64-rhel70-3.6.12/bin/ /mongodb

mongodb下载地址

设置目录结构权限

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 

使用配置文件

YAML模式
====不支持tab
====有严格缩进
NOTE:
YAML does not support tab characters for indentation: use spaces instead.

--系统日志有关  
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>                       -- 监听地址
   port: <port>                       -- 端口号,默认不配置端口号,是27017
   
-- 安全验证有关配置      
security:
  authorization: enabled              --是否打开用户名密码验证
  
------------------以下是复制集与分片集群有关----------------------  

replication:
 oplogSizeMB: <NUM>
 replSetName: "<REPSETNAME>"
 secondaryIndexPrefetch: "all"
 
sharding:
   clusterRole: <string>
   archiveMovedChunks: <boolean>
      
---for mongos only
replication:
   localPingThresholdMs: <int>

sharding:
   configDB: <string>
---
++++++++++++++++++++++
YAML配置文件例子
cat >  /mongodb/conf/mongo.conf <<EOF
systemLog:
   destination: file
   path: "/mongodb/log/mongodb.log"
   logAppend: true
storage:
   journal:
      enabled: true
   dbPath: "/mongodb/data/"
processManagement:
   fork: true
net:
   port: 27017
   bindIp: 10.0.0.51,127.0.0.1
EOF
关闭数据库
mongod -f /mongodb/conf/mongo.conf --shutdown
启动数据库
mongod -f /mongodb/conf/mongo.conf   

配置文件里面的bind ip必须设置

mongodb的关闭方式

mongod -f mongo.conf  --shutdown

mongodb的启动方式

mongod -f mongo.conf  

mongodb 使用systemd管理

注意:
root用户下使用systemctl管理
[root@db01 ~]# cat > /etc/systemd/system/mongod.service <<EOF
[Unit]
Description=mongodb 
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongod
Type=forking
ExecStart=/mongodb/bin/mongod --config /mongodb/conf/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/mongodb/bin/mongod --config /mongodb/conf/mongo.conf --shutdown
PrivateTmp=true  
[Install]
WantedBy=multi-user.target
EOF

[root@db01 ~]# systemctl restart mongod
[root@db01 ~]# systemctl stop mongod
[root@db01 ~]# systemctl start mongod

登录mongodb

普通用户执行mongo命令
[mongod@db01 ~]$ mongo

3、mongodb常用基本操作

3.0 mongodb 默认存在的库

test:登录时默认存在的库
管理MongoDB有关的系统库
admin库:系统预留库,MongoDB系统管理库
local库:本地预留库,存储关键日志
config库:MongoDB配置信息库

show databases/show dbs
show tables/show collections
use admin 
db/select database()

mongodb调用都是一些函数的命令
里面的库表不需要提前创建
主键在mongodb里面自动生成,不需要创建

3.1 命令种类

db 对象相关命令

db.[TAB][TAB]
db.help()
db.oldboy.[TAB][TAB]
db.oldboy.help()

rs 复制集有关(replication set):

rs.[TAB][TAB]
rs.help()

sh 分片集群(sharding cluster)

sh.[TAB][TAB]
sh.help()

4. mongodb对象操作

mongo         mysql
库    ----->  库
集合  ----->  表
文档  ----->  数据行

4.1 库的操作

> use test
>db.dropDatabase()   
{
    "dropped" : "test", "ok" : 1 }

4.2 集合的操作

app> db.createCollection('a')
{
    "ok" : 1 }
app> db.createCollection('b')
方法2:当插入一个文档的时候,一个集合就会自动创建。

use oldboy
db.test.insert({
   name:"zhangsan"})
db.stu.insert({
   id:101,name:"zhangsan",age:20,gender:"m"})
show tables;
db.stu.insert({
   id:102,name:"lisi"})
db.stu.insert({
   a:"b",c:"d"})
db.stu.insert({
   a:1,c:2})


测试
> db
oldjiang
> db.test.insert({
   id:100,name:mmz,age:22})
uncaught exception: ReferenceError: mmz is not defined :
@(shell):1:24
> db.test.insert({
   id:100,name:"wmz",age:22})
WriteResult({
    "nInserted" : 1 })
> show tables
test
> db.test.find()
{
    "_id" : ObjectId("60e1a22654c6f41bad8843cc"), "id" : 100, "name" : "wmz", "age" : 22 }
> db.test.find().pretty()
{
   
	"_id" : ObjectId("60e1a22654c6f41bad8843cc"),
	"id" : 100,
	"name" : "wmz",
	"age" : 22
}



测试
> db.test.insert({
   id:333,name:"jjj",age:24})
WriteResult({
    "nInserted" 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值