Rdies基础 安装、常用命令、持久化

一、NoSQL Redis概述

  • NoSQL概述
    RDBMS (Relational Database Management System)关系数据库管理系统
    按照预先设置的组织结构,将数据存储在物理介质上
    数据之间可以做关联操作
  • RDBMS软件
    主流的RDBMS软件
    MySQL
    MariaDB
    Oracle
    DB2
    SQL Server
  • NoSQL (NoSQL = Not Only SQL)意思是“不仅仅是SQL”
    泛指非关系型数据库
    不需要预先定义数据存储结构
    每条记录可以有不同的数据类型和字段个数
  • NoSQL软件
    主流软件
    Memcached
    Redis
    MongoDB
    CouchDB-Neo4j
    FlockDB
  • Redis介绍
    Remote Dictionary Server (远程字典服务器)
    是一款高性能的(Key/Values)分布式内存数据库
    支持数据持久化(定期把内存里数据存储到硬盘)
    支持多种数据类型string.list、hash .....
    支持 master-salve模式数据备份
    中文网站www.redis.cn
  • 初始配置
    配置服务运行参数
    ]# ./utils/install_server.sh //初始化
    默认端口 6379
    主配置文件 /etc/redis/6379.conf
    日志文件 /var/log/redis_6379.log
    数据库目录 /var/lib/redis/6379
    服务启动程序 /usr/local/bin/redis-server
    命令行连接命令 /usr/local/bin/redis-cli
  • 管理服务
    ]# /etc/init.d/redis_6379 stop //停止服务
    ]# /etc/init.d/redis_6379 start //启动服务
    ]# ps -C redis-server //查看进程
    ]# netstat -utnlp | grep :6379 //查看端口
  • 连接服务
    访问redis服务
    redis-cli 默认连接本机的redis服务
    Redis Desktop Manager(RDM)常用可视化管理工具
二、Redis安装部署
案例1:搭建Redis服务器
具体要求如下∶
-在主机192.168.4.155上安装并启动redis服务
-设置变量school,值为tarena
-查看变量school值
软件安装
1)安装源码redis软件
[root@redis ~]# yum -y install gcc [root@redis ~]# tar -xf redis-5.0.10.tar.gz//先从官网下载redis安装包 [root@redis ~]# cd redis-5.0.10/ [root@redis redis-5.0.10]# ls 00-RELEASENOTESCOPYINGMakefileredis.confruntest-moduleapisrc BUGSdepsMANIFESTOruntestruntest-sentineltests CONTRIBUTINGINSTALLREADME.mdruntest-clustersentinel.confutils[root@redis redis-5.0.10]# make [root@redis redis-5.0.10]# make install [root@redis redis-5.0.10]# cd utils/ [root@redis utils]# ./install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379 Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port: 6379//端口号 Config file: /etc/redis/6379.conf//配置文件目录 Log file: /var/log/redis_6379.log//日志目录 Data dir: /var/lib/redis/6379//数据库目录 Executable: /usr/local/bin/redis-server//启动程序的目录 Cli Executable : /usr/local/bin/redis-cli//命令行的连接工具 Is this ok? Then press ENTER to go on or Ctrl-C to abort.//回车完成配置 Copied /tmp/6379.conf => /etc/init.d/redis_6379//服务启动脚本 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server...//提示服务已经启动 Installation successful!//提示安装成功

2)查看服务状态 监听的端口
[root@redis utils]# /etc/init.d/redis_6379 status Redis is running (5541) [root@redis utils]# ss -anput|grep :6379//查看端口 默认6379 tcpLISTEN0128127.0.0.1:6379*:*users:(("redis-server",pid=5541,fd=6)) [root@redis utils]# ps -aux|grep redis//查看进程 root55410.10.5 1538887652 ?Ssl15:520:03 /usr/local/bin/redis-server127.0.0.1:6379

3)停止服务
[root@redis utils]# /etc/init.d/redis_6379 stop Stopping ... Redis stopped

4)连接redis
[root@redis utils]# /etc/init.d/redis_6379 start /var/run/redis_6379.pid exists, process is already running or crashed[root@redis utils]# redis-cli 127.0.0.1:6379> ping//提示PONG表示连接成功 PONG 127.0.0.1:6379>

5)存储变量school,值为tarena,查看变量school的值
127.0.0.1:6379> set school tarena OK 127.0.0.1:6379> get school "tarena"

三、常用命令
  • set key名 key值 //存储1个key值
  • mset key名列表 //存储多个key值
  • get key名 //获取key值
  • mget //获取多个key值
  • select 数据库编号0-15 //切换库
  • keys * //显示所有key名
  • keys a? //显示指定key名
  • exists key名 //测试key名是否存在
  • ttl key名 //查看key生存时间
  • type key名 //查看key类型
Redis默认有16个库;默认使用的是0号库;
  • move key名库编号 //移动key到指定库
  • expire key名数字 //设置key有效时间
  • del key名 //删除指定的key
  • flushall //删除内存里所有key
  • flushdb //删除所在库的所有key
  • save //保存所有key到硬盘
  • shutdown //停止服务
案例2∶常用命令练习
练习如下命令的使用:
set mset get mget keys type
exists ttl expire move 、select
del flushdb flushall save shutdown
1)命令set 、 mset 、 get 、 mget
127.0.0.1:6379> set name bob OK 127.0.0.1:6379> mset age 19 sex boy OK 127.0.0.1:6379> get name "bob" 127.0.0.1:6379> get sex "boy" 127.0.0.1:6379> mget age sex 1) "19" 2) "boy"

2)命令keys 、 type 、 exists 、 del
127.0.0.1:6379> keys * 1) "age" 2) "name" 3) "sex" 4) "school" 127.0.0.1:6379> keys ??? 1) "age" 2) "sex" 127.0.0.1:6379> keys a* 1) "age" 127.0.0.1:6379> type age//使用set命令存储的变量都是字符类型 string 127.0.0.1:6379> del age (integer) 1 127.0.0.1:6379> exists age//变量不存储返回值0 (integer) 0 127.0.0.1:6379> exists sex//变量存在 返回值1 (integer) 1 127.0.0.1:6379>

3)命令ttl 、 expire 、 move 、 flushdb 、flushall 、save、shutdown、select
127.0.0.1:6379> ttl sex//返回值-1 表示变量永不过期 (integer) -1 127.0.0.1:6379> expire sex 20 //设置变量过期时间为 20 秒 (integer) 1 127.0.0.1:6379> ttl sex//还剩7秒过期 (integer) 7 127.0.0.1:6379> ttl sex//返回值-2 表示已经过期 (integer) -2 127.0.0.1:6379> exists sex//变量已经不存在 (integer) 0 127.0.0.1:6379> move name 1//把变量name移动到1号库里 (integer) 1 127.0.0.1:6379> select 1//切换到1号库 OK 127.0.0.1:6379[1]> keys *//查看 1) "name" 127.0.0.1:6379[1]> select 0//切换到0号库 OK 127.0.0.1:6379> keys *//查看 1) "school" 127.0.0.1:6379> select 1//切换到1号库 OK 127.0.0.1:6379[1]> keys * 1) "name" 127.0.0.1:6379[1]> flushdb OK 127.0.0.1:6379[1]> keys* (empty list or set) 127.0.0.1:6379[1]> flushall OK 127.0.0.1:6379[1]> save OK 127.0.0.1:6379[1]> shutdown//提示连接断开 not connected> exit [root@redis utils]# [root@redis utils]# ps aux|grep redis//没有进程信息 [root@redis utils]# /etc/init.d/redis_6379 start//启动服务 Starting Redis server... [root@redis utils]# ps aux|grep redis root56250.00.5 1538887688 ?Ssl17:060:00 /usr/local/bin/redis-server 127.0.0.1:6379

四、常用配置及内存管理
  • 6379.conf 配置文件解析 模块分类
    NETWORK 网络
    GENERAL 常规
    SNAPSHOTTING 快照
    REPLICATION 复制
    SECURITY 安全
    CLIENTS 客户端
    MEMORY MANAGEMENT 内存管理
  • 常用配置
    port 6379 //端口
    bind 127.0.0.1 //IP地址
    daemonize yes //守护进程方式运行
    databases 16 //数据库个数
    logfile /var/log/redis_6379.log //日志文件
    maxclients 10000 //并发连接数量
    dir /var/lib/redis/6379 //数据库目录
内存管理
  • 内存清除策略
    volatile-Iru //最近最少使用(针对设置了TTL的key )
    allkeys-lru //删除最少使用的key(针对所有的key)
    alkeys-lfu //从所有key中清除使用频率最少的key
    volatile-lfu //从所有配置了过期时间的key中清除使用频率最少的key
    volatile-random //在设置了TTL的key里随机移除
    allkeys-random //随机移除key
    volatile-ttl (minor TTL) //移除最近过期的key
    noeviction //不删除,写满时报错
  • 优化设置
    maxmemory //最大内存
    maxmemory-policy //定义使用策略
    maxmemory-samples //选取key模板的个数(针对lru和ttl策略)
连接密码
设置密码、ip地址、端口
]# vim /etc/redis/6379.conf requirepass 123456bind 192.168.4.50port 6350 :wq ]# redis-cli -h192.168.4.50 -p6350//连接服务192.168.4.50:6350> ping (error) NOAUTH Authentication required.192.168.4.50:6350> auth 123456//输入密码OK 192.168.4.50:6350> pingPONG ]# redis-cli -h 192.168.4.50 -p 6350 -a 123456//连接时加密码 ]# redis-cli -h 192.168.4.64 -p 6364 -a 123456 shutdown//停止服务

案例3∶修改Redis服务运行参数
对Redis服务器192.168.4.155做如下配置:
-.端口号6350
-.IP地址192.168.4.155-连接密码123456-测试配置
1)修改配置文件
[root@redis utils]# cp /etc/redis/6379.conf /root/6379.conf//可以先备份一份,防止修改错误没法还原 [root@redis utils]# /etc/init.d/redis_6379 stop Stopping ... Redis stopped [root@redis utils]# vim /etc/redis/6379.conf bind 192.168.4.155//设置服务使用的ip port 6360//更改端口号 requirepass 123456//设置密码

2)修改启动脚本
[root@redis utils]# vim +43 /etc/init.d/redis_6379 43$CLIEXEC -h 192.168.4.50 -p 6360 -a 123456 shutdown

3)启动服务
[root@redis utils]# /etc/init.d/redis_6379 start Starting Redis server... [root@redis utils]# ps aux|grep redis root57120.00.5 1538887688 ?Ssl17:130:00 /usr/local/bin/redis-server 192.168.4.155:6360 root57170.00.0 112720984 pts/1S+17:130:00 grep --color=auto redis

4)测试配置 访问服务存取数据
[root@redis utils]# redis-cli -h 192.168.4.155 -p 6360 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.4.155:6360> ping PONG

五、持久化 RDB、AOF
RDB介绍
Redis数据库文件,全称Redis DataBase
-数据持久化方式之一
-数据持久化默认方式
-按照指定时间间隔,将内存中的数据集快照写入硬盘
-快照术语叫Snapshot
-恢复时,将快照文件直接读入内存
定义RDB文件名
vim /etc/redis/6379.conf
dbfilename "dump.rdb"//文件名
使用RDB文件恢复数据
·备份数据
-备份dump.rdb文件到其他位置
]# cp 数据库目录/dump.rdb备份目录
·恢复数据
-拷贝备份文件到数据库目录,重启redis服务
]#cp 备份目录/dump.rdb数据库目录/
优化设置
数据从内存保存到硬盘的频率
  • save 900 1 //15分钟且有1个key改变
  • save 300 10 //5分钟且有10个key改变
  • save 60 10000 //1分钟且有10000个key改变
·手动存盘
  • save //阻塞写存盘
    save 命令是同步方式生成快照,会造成Redis阻塞,所有后续到达的命令要等待save完成以后才能执行。
  • bgsave //不阻塞写存盘
    bgsave 命令采用异步方式生成快照,Redis会fork出一个子进程进行RDB文件的生成。
    Redis只有在fork子进程时被阻塞,子进程完成快照生成的同时,Redis可以正常工作。
RDB优点与缺点
·RDB优点
-高性能的持久化实现 创建一个子进程来执行持久化,先将数据写入临时文件,持久化过程结束后,再用这个临时文件替换上次持久化好的文件; 过程中主进程不做任何IO操作
-比较适合大规模数据恢复,且对数据完整性要求不是非常高的场合
RDB的缺点
-意外宕机时,丢失最后一次持久化的所有数据
案例4∶使用RDB文件恢复数据
要求如下︰
-启用RDB
-设置存盘间隔为120秒且10个key改变数据自动存盘
-备份RDB文件
-删除数据
-使用RDB文件恢复数据
[root@redis ~]# vim /etc/redis/6379.conf save 900 1 #save 300 10//注释原有设置 save 120 10//时间修改为 120秒 save 60 10000[root@redis ~]# /etc/init.d/redis_6379 stop//停止服务 [root@redis ~]# rm -fr /var/lib/redis/6379/*//清空数据库目录 [root@redis ~]# ll /var/lib/redis/6379///此时,查看数据库目录下没有 dump.rdb文件 总用量 0 [root@redis ~]# /etc/init.d/redis_6379 start//启动服务[root@redis ~]# redis-cli -h 192.168.4.155 -p 6360 -a 123456//连接服 务,在200秒内存储10个变量,就会自动在数据库目录下创建dump.rdb 文件 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.4.155:6360> ping PONG 192.168.4.155:6360> set v1 k1 OK 192.168.4.155:6360> set v2 k2 OK 192.168.4.155:6360> set v3 k3 OK 192.168.4.155:6360> set v4 k4 OK 192.168.4.155:6360> set v5 k5 OK 192.168.4.155:6360> set v6 k6 OK 192.168.4.155:6360> set v7 k7 OK 192.168.4.155:6360> set v8 k8 OK 192.168.4.155:6360> set v9 k9 OK 192.168.4.155:6360> set v10 k10 OK 192.168.4.155:6360> set v11 k11 OK 192.168.4.155:6360> keys * 1) "v5" 2) "v1" 3) "v10" 4) "v3" 5) "v7" 6) "v8" 7) "v2" 8) "v11" 9) "v9" 10) "v4" 11) "v6" 192.168.4.155:6360> exit[root@redis ~]# ls /var/lib/redis/6379///此时,查看数据库目录下有 dump.rdb文件 dump.rdb[root@redis ~]# cp /var/lib/redis/6379/dump.rdb/tmp///备份dump.rdb [root@redis ~]# redis-cli -h 192.168.4.155 -p 6360 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.4.155:6360> flushall OK 192.168.4.155:6360> keys * (empty list or set) 192.168.4.155:6360> exit [root@redis ~]# /etc/init.d/redis_6379 stop Stopping ... Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Redis stopped[root@redis ~]# rm -fr /var/lib/redis/6379/*//确保数据库被精空 [root@redis ~]# cp /tmp/dump.rdb /var/lib/redis/6379///拷贝备份文件到数据库目录下 [root@redis ~]# /etc/init.d/redis_6379 start Starting Redis server... [root@redis ~]# redis-cli -h 192.168.4.155 -p 6360 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.4.155:6360> keys *//查看数据 1) "v6" 2) "v2" 3) "v10" 4) "v4" 5) "v11" 6) "v9" 7) "v8" 8) "v3" 9) "v5" 10) "v1" 11) "v7"192.168.4.155:6360>

AOF介绍
Append Only File
追加方式记录写操作的文件
记录redis服务所有写操作
不断的将新的写操作,追加到文件的末尾
默认没有启用
使用cat命令可以查看文件内容
启用AOF
> config set appendonly yes//启用 > config rewrite//写进配置文件

使用AOF文件恢复数据
备份数据
备份appendonly.aof文件到其他位置
]# cp 数据库目录/applendonly.aof备份目录
恢复数据
拷贝备份文件到数据库目录
重启redis服务
]# cp 备份目录/appendonly.aof 数据库目录
]#/etc/redis/redis_端口 start
通过配置启动AOF
定义文件名
appendonly yes //启用aof 默认no
appendfilename "appendonly.aof" //指定文件名
AOF文件记录写操作的方式
appendfsync always //时时记录,并完成磁盘同步
appendfsync everysec //每秒记录一次,并完成磁盘同步
appendfsync no //写入aof ,不执行磁盘同步
日志文件会不断增大,何时触发日志重写?
auto-aof-rewrite-min-size 64mb //首次重写触发值
auto-aof-rewrite-percentage 100 //再次重写,增长百分比
修复AOF文件
一把文件恢复到最后一次的正确操作
[root@redis ~]# redis-check-aof --fix appendonly.aofox83: Expected rln, got: 6166 AOF analyzed: size=160, ok_up_to=123, diff=37 This will shrink the AOF from i60 bytes, with 37 bytes, to 123bytes Continue? [y/N]: y Successfully truncated AOF

AOF优点与缺点
AOF优点
可以灵活设置持久化方式
出现意外宕机时,仅可能丢失1秒的数据
AOF缺点
持久化文件的体积通常会大于RDB方式
执行fsync策略时的速度可能会比RDB方式慢
案例5︰使用AOF文件恢复数据
要求如下:
-启用AOF
-备份AOF文件
-删除数据
-使用AOF文件恢复数据
1)修改配置文件
[root@redis ~]# redis-cli -h 192.168.4.155 -p 6360 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.4.155:6360> config set appendonly yes//启用aof,默认no OK 192.168.4.155:6360> config rewrite//写进配置文件 OK 192.168.4.155:6360> save OK 192.168.4.155:6360> exit

2)备份AOF文件
[root@redis ~]# cd /var/lib/redis/6379/ [root@redis 6379]# ls appendonly.aofdump.rdb [root@redis 6379]# cp appendonly.aof /tmp/ [root@redis 6379]# redis-cli -h 192.168.4.155 -p 6360 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.4.155:6360> flushall OK 192.168.4.155:6360> keys * (empty list or set) 192.168.4.155:6360> exit

3)删除数据
[root@redis 6379]# /etc/init.d/redis_6379 stop Stopping ... Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Redis stopped [root@redis 6379]# rm -fr /var/lib/redis/6379/*//确定数据库被清空

【Rdies基础 安装、常用命令、持久化】4) 使用AOF文件恢复数据
[root@redis 6379]# cp /tmp/appendonly.aof /var/lib/redis/6379///拷贝备份文件到目录 [root@redis 6379]# ls appendonly.aof [root@redis 6379]# /etc/init.d/redis_6379 start Starting Redis server... [root@redis 6379]# redis-cli -h 192.168.4.155 -p 6360 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.4.155:6360> keys *//查看数据 1) "v9" 2) "v3" 3) "v4" 4) "v5" 5) "v2" 6) "v7" 7) "v1" 8) "v6" 9) "v8" 10) "v11" 11) "v10" 192.168.4.155:6360>

    推荐阅读