1. 重点¶
#1.复制技术
#2.分片技术
#3.备份恢复方式
2. nosql家族¶
#1.列存储 Hbase
#2.键值存储 Redis
#3.图像存储 Neo4J
#4.文档存储 MongoDB
3. nosql优势¶
#1.高可扩展性
#2.分布式计算
#3.没有复杂关系
#4.低成本
#5.架构灵活
#6.半结构化数据
4. mongodb的数据存储格式¶
#json: JavaScript ObjectNotation
-json数据库语句容易被解析
-web应用大量使用
-name-value配对
#bson: 二进制的json,json文档的二进制编码存储格式
-bson有json没有的date和bindata
-mongodb中document以bson形式存放
5. mongodb丰富的功能¶
#1.json文档模型
#2.动态的数据模式
#3.二级索引强大
#4.查询功能
#5.自动分片
#6.水平扩展
#7.自动复制
#8.高可用
#9.文本搜索
#10.企业级安全
#11.聚合框架MapReduce
#12.大文件存储GridFS
6. mongodb存储引擎¶
#1.MMAPv1 :类似MySQL中的MyISAM
#2.WiredTiger :类似于MySQL中的InnoDB
7. mongodb数据逻辑结构¶
#1.库 database 库
#2.集合 collection 表
#3.文档 document 数据行
MongoDB搭建¶
1. 安装准备¶
ip:192.168.178.151
#1.centos6.2以上系统
#2.关闭iptables&Selinux
#3.配置IP和hosts解析
#4.关闭hugepage大页内存
#5.创建mongodb所需用户
#6.创建所需目录并授权
2. 关闭大页内存¶
#root用户下
vim /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@151 ~]# vim /etc/rc.local
[root@151 ~]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/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@151 ~]# source /etc/rc.local
3. 创建mongodb用户¶
groupadd -g 800 mongod
useradd -u 801 -g mongod mongod
passwd mongod
#设置的密码为123
[root@151 ~]# groupadd -g 800 mongod
[root@151 ~]# useradd -u 801 -g mongod mongod
[root@151 ~]# passwd mongod
Changing password for user mongod.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
4. 创建mongodb所需目录¶
mkdir -p /mongodb/bin
mkdir -p /mongodb/conf
mkdir -p /mongodb/log
mkdir -p /mongodb/data
5. 下载软件¶
1.浏览器访问 https://www.mongodb.com/1
2.点击software选择社区版
3.选择需要下载的软件版本、平台、包类型,然后点击下载
6. 上传安装包¶
mkdir /server/tools
ls /server/tools
[root@151 ~]# ls /server/tools/
mongodb-linux-x86_64-rhel70-3.2.22.tgz
7. 解压缩¶
tar -zxvf mongodb*
[root@151 ~]# cd /server/tools/
[root@151 tools]# tar -zxvf mongodb*
mongodb-linux-x86_64-rhel70-3.2.22/README
mongodb-linux-x86_64-rhel70-3.2.22/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-3.2.22/MPL-2
mongodb-linux-x86_64-rhel70-3.2.22/GNU-AGPL-3.0
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongodump
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongorestore
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongoexport
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongoimport
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongostat
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongotop
mongodb-linux-x86_64-rhel70-3.2.22/bin/bsondump
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongofiles
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongooplog
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongoperf
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongosniff
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongod
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongos
mongodb-linux-x86_64-rhel70-3.2.22/bin/mongo
[root@151 tools]# ls
mongodb-linux-x86_64-rhel70-3.2.22 mongodb-linux-x86_64-rhel70-3.2.22.tgz
[root@151 tools]#
8. 拷贝bin目录下的程序到/mongodb/bin¶
cp -a /server/tools/mongodb-linux-x86_64-rhel70-3.2.22/bin/* /mongodb/bin
#1.查看
[root@151 tools]# cd mongodb-linux-x86_64-rhel70-3.2.22
[root@151 mongodb-linux-x86_64-rhel70-3.2.22]# ls
bin GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES
[root@151 mongodb-linux-x86_64-rhel70-3.2.22]# cd bin/
[root@151 bin]# ls
bsondump mongo mongod mongodump mongoexport mongofiles mongoimport mongooplog mongoperf mongorestore mongos mongosniff mongostat mongotop
#2.拷贝
[root@151 bin]# cp -a /server/tools/mongodb-linux-x86_64-rhel70-3.2.22/bin/* /mongodb/bin
[root@151 bin]# ls
bsondump mongo mongod mongodump mongoexport mongofiles mongoimport mongooplog mongoperf mongorestore mongos mongosniff mongostat mongotop
[root@151 bin]# ls /mongodb/bin/
bsondump mongo mongod mongodump mongoexport mongofiles mongoimport mongooplog mongoperf mongorestore mongos mongosniff mongostat mongotop
9. 更改/mongodb目录的属主属组¶
chown -R mongod:mongod /mongodb
10. 设置用户环境变量¶
su - mongod
vim .bash_profile
export PATH=/mongodb/bin:$PATH
source .bash_profile
#1.切换用户
[root@151 bin]# su - mongod
[mongod@151 ~]$ ls -a
. .. .bash_logout .bash_profile .bashrc
[mongod@151 ~]$
[mongod@151 ~]$ pwd
/home/mongod
#2.添加用户环境变量
[mongod@151 ~]$ echo "export PATH=/mongodb/bin:$PATH" >> /home/mongod/.bash_profile
[mongod@151 ~]$ cat /home/mongod/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export PATH=/mongodb/bin:/server/tools/redis/src:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/home/mongod/.local/bin:/home/mongod/bin
#3.使环境变量生效
[mongod@151 ~]$ source /home/mongod/.bash_profile
11. 启动mongodb¶
mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork
#参数说明
1.数据存储位置:--dbpath=/mongodb/data
2.日志存储位置:--logpath=/mongodb/log/mongodb.log
3.默认端口: --port=27017
4.日志以追加形式:--logappend
5.后台运行: --fork
#1.启动
[mongod@151 ~]$ mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork
about to fork child process, waiting until server is ready for connections.
forked process: 79752
child process started successfully, parent exiting
#2.查看
[mongod@151 ~]$ netstat -tunlp |grep 27017
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 79752/mongod
[mongod@151 ~]$
[mongod@151 ~]$ ps -ef|grep mongo
root 78236 74484 0 10:42 pts/1 00:00:00 su - mongod
mongod 78237 78236 0 10:42 pts/1 00:00:00 -bash
mongod 79752 1 0 10:46 ? 00:00:00 mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork
mongod 80037 78237 0 10:47 pts/1 00:00:00 ps -ef
mongod 80038 78237 0 10:47 pts/1 00:00:00 grep --color=auto mongo
[mongod@151 ~]$
12. 登录mongodb测试¶
mongo
[mongod@151 ~]$ mongo
MongoDB shell version: 3.2.22
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
>
> db
test
> show databases;
local 0.000GB
> quit
function quit() {
[native code]
}
> exit
bye
13. 编辑mongodb配置文件¶
#第一种配置文件(不推荐!!!)
vim /mongodb/conf/mongodb.conf
logpath=/mongodb/log/mongodb.log
dbpath=/mongodb/data
port=27017
logappend=true
fork=true
#第二种配置文件(推荐)
vim /mongodb/conf/mongo.conf
systemLog:
destination: file
path: "/mongodb/log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data/"
processManagement:
fork: true
net:
port: 27017
[mongod@151 ~]$ vim /mongodb/conf/mongo.conf
[mongod@151 ~]$ cat /mongodb/conf/mongo.conf
systemLog:
destination: file
path: "/mongodb/log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data/"
processManagement:
fork: true
net:
port: 27017
#补充:yaml配置文件相关内容
#1.yaml文件不支持tab键,只能使用空格
--系统日志相关
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 #是否打开用户名密码验证
#一下是复制集与分片集有关的参数
replication:
oplogSizeMB: <NUM>
replSetName: "<REPSETNAME>"
secondaryIndexPrefetch: "all"
sharding:
clusterRole: <string>
archiveMovedChunks: <boolean>
---for mongos only
replication:
localPingThresholdMs: <int>
sharding:
configDB: <string>
14. 重启mongodb¶
mongod -f /mongodb/conf/mongo.conf --shutdown
mongod -f /mongodb/conf/mongo.conf
#1.关闭mongodb
[mongod@151 ~]$ mongod -f /mongodb/conf/mongo.conf --shutdown
killing process with pid: 79752
#2.开启mongodb
[mongod@151 ~]$ mongod -f /mongodb/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 84440
child process started successfully, parent exiting
#3.查看
[mongod@151 ~]$ netstat -tunlp|grep 27017
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 84440/mongod
15. 再次登录mongo测试¶
mongo
[mongod@151 ~]$ mongo
MongoDB shell version: 3.2.22
connecting to: test
> db
test
> show databases;
local 0.000GB
> exit
bye
最后更新:
2022-02-20 11:21:58