标签:union dbi timer smo utils 数据同步 信息 save unix
Redis最适合所有数据in-momory的场景,虽然Redis也提供持久化功能,但实际更多的是一个disk-backed(以写入磁盘的方式进行同步,实现持久化)的功能,跟传统意义上的持久化有比较大的差别
如果简单地比较Redis与Memcached的区别,大多数都会得到以下观点:
1 、Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
2 、Redis支持数据的备份,即master-slave模式的数据备份。
3 、Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。
1.新的Stream数据类型。
2.新的Redis模块API:Timers and Cluster API。
3. RDB现在存储LFU和LRU信息。
4.集群管理器从Ruby(redis-trib.rb)移植到C代码。可以在redis-cli中。查看`redis-cli —cluster help`了解更多信息。
5.新sorted set命令:ZPOPMIN / MAX和阻塞变量。
6.主动碎片整理V2。
7.增强HyperLogLog实现。
8.更好的内存统计报告。
9.许多带有子命令的命令现在都有一个HELP子命令。
10.客户经常连接和断开连接时性能更好。
11.错误修复和改进。
一、安装编译环境
# redis使用c语言编写,所以需要gcc编译环境 # yum install -y gcc gcc-c++
二、下载stable版,并上传
官网地址:http://download.redis.io/releases/redis-5.x.x.tar.gz
三、创建安装目录并解压
# mkdir -p /data/softwares # mkdir /data/module # cd /data/softwares # tar xfz redis-5.0.0.tar.gz -C /data/module
四、编译安装
# cd ../module/redis-5.0.0/ # make
五、为了方便管理,将Redis文件中的conf配置文件和常用命令移动到统一文件中
# mkdir etc bin # mv redis.conf etc/ # cd src/ # cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-sentinel redis-cli redis-server redis-trib.rb ../bin
六、修改环境变量
# echo "export PATH=$PATH:/data/module/redis-5.0.0/bin" >> /etc/profile # . /etc/profile
七、 编辑 redis.conf配置文件,开启redis远程访问服务
# sed -ri.ori -e ‘s@(^daemonize )no@\1yes@g‘ -e ‘s@(^bind 127.*)@#\1@g‘ -e ‘s@(^protected-mode ).*@\1no@g‘ -e ‘s@# (r.*)foobared@\1123456@‘ ../etc/redis.conf
服务启动
# redis-server etc/redis.conf 5106:C 31 Jan 2020 05:57:04.305 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5106:C 31 Jan 2020 05:57:04.305 # Redis version=5.0.0, bits=64, commit=00000000, modified=0, pid=5106, just started
查看启动情况
# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:6379 *:* users:(("redis-server",pid=5107,fd=7)) LISTEN 0 128 *:22 *:* users:(("sshd",pid=746,fd=3)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=828,fd=13)) LISTEN 0 128 :::6379 :::* users:(("redis-server",pid=5107,fd=6)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=746,fd=4)) LISTEN 0 100 ::1:25
停止服务
# redis-cli -h localhost localhost:6379> shutdown (error) NOAUTH Authentication required. localhost:6379> AUTH 123456
redis可执行文件作用
可执行文件名 | 命令作用 |
redis-cli | redis命令行工具 |
redis-server | redis服务启动命令 |
redis-trib.rb | redis集群管理工具(5.0以前是ruby,之后移植到了c) |
redis-benchmark | 基准测试工具 |
redis-check-rdb | RDB持久化文件检测工具和修复工具 |
redis-check-aof | AOF持久化文件检测工具和修复工具 |
redis-sentinel | 启动redis-sentinel(哨兵) |
redis提供两种连接服务器方式:交互式和命令行方式
(1)交互式
redis-cli -h {host} -p {port} -a {password} # redis-cli -h localhost -p 6379 -a 123456 localhost:6379> set ass oo OK
(2)命令行
[root@localhost redis-5.0.0]# redis-cli -h localhost -p 6379 -a 123456 set foo bar Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe. OK [root@localhost redis-5.0.0]# redis-cli -h localhost -p 6379 -a 123456 get foo Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe. "bar"
参数
-h <hostname> 指定服务端地址
-p <port> 指定服务连接端口
-a <password> 连接服务端时使用的密码
[root@localhost redis-5.0.0]# redis-cli -h localhost -p 6379 -a 123456 Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe. localhost:6379> set foo bar OK
可以看到,当在命令行输入密码的时候会有安全警告信息,这会影响到脚本获取数据,这种警告不能通过head、tail、sed等文本处理工具去除,因为这个警告是标准错误,而不是标准输出(1),所以将标准错误定向到空即可
[root@localhost redis-5.0.0]# redis-cli -h localhost -p 6379 -a 123456 2>/dev/null localhost:6379>
-r <repeat> 将命令重复执行多次
-i <interval> 每隔几秒(如果想用ms,如10ms则写0.01)执行一次命令,必须与-r一起使用
[root@localhost redis-5.0.0]# redis-cli -h localhost -p 6379 -a 123456 -r 2 ping Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe. PONG PONG
-n <db> 指定连接的数据库
[root@localhost redis-5.0.0]# redis-cli -h localhost -p 6379 -n 3 localhost:6379[3]> ##[3]当前所使用的数据库
-x 从标准输入读取数据作为该命令的最后一个参数
[root@localhost redis-5.0.0]# echo ‘bar‘|redis-cli -h localhost -p 6379 -a 123456 -x set foo Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe. OK
-c 连接集群结点时使用,此选项可防止moved和ask异常
--scan和--pattern 相当于scan命令,扫描所有键 SCAN cursor [MATCH pattern] [COUNT count]
[root@localhost redis-5.0.0]# redis-cli -a 123456 --scan --pattern ‘*ass*‘ Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe. mypasswd
--raw 和 --no-raw
(1)不展示数据额外类型信息 # redis-cli -h localhost -p 6379 -a 123456 2>/dev/null set num1 1 OK # redis-cli -h localhost -p 6379 -a 123456 2>/dev/null get num1 "3" # redis-cli -h localhost -p 6379 -a 123456 --raw 2>/dev/null get num1 3 # redis-cli -h localhost -p 6379 -a 123456 2>/dev/null incr num1 (integer) 3 # redis-cli -h localhost -p 6379 -a 123456 2>/dev/null --raw decr num1 2 可以看到,当返回数据到tty的时候,不加--raw,会在数据前显示数据类型 当redis返回数据不是打印到标准输出,而是重定向到文件里,redis会自动开启--raw参数 # redis-cli -h localhost -p 6379 -a 123456 2>/dev/null incr num1 > /tmp/redis.txt # cat /tmp/redis.txt 4 (2)显示中文 # redis-cli -h localhost -p 6379 -a 123456 2>/dev/null set hello "中国" OK # redis-cli -h localhost -p 6379 -a 123456 2>/dev/null get hello "\xe4\xb8\xad\xe5\x9b\xbd" # redis-cli -h localhost -p 6379 -a 123456 2>/dev/null --raw get hello 中国 当值含有中文的时候,如果不加--raw,返回的结果是Unicode编码
--stat 获取redis状态统计信息,比info更全
每隔10秒实时获取一次redis状态统计信息,其中:
keys 表示数据库的键数量
mem 表示服务内存使用量
clients 连接当前节点的客户端数量
requests 截止目前总的处理量
connections 截止到目前的总连接数
# redis-cli -h localhost -p 6379 -i 10 -a 123456 2>/dev/null --stat ------- data ------ --------------------- load -------------------- - child - keys mem clients blocked requests connections 5 1.82M 1 0 3162 (+0) 47 5 1.82M 1 0 3163 (+1) 47 5 1.82M 1 0 3164 (+1) 47
7、--slave
当当前客户端模拟成当前redis节点的从节点,可用来获取当前redis节点的更新操作。合理利用可用于记录当前连接redis节点的一些更新操作,这些更新可能是实开发业务时需要的数据。
8、--rdb
会请求redis实例生成并发送RDB持久化文件,保存在本地。可做定期备份。
# redis-cli -a 123456 2>/dev/null --rdb a.txt
9、--pipe
将命令封装成redis通信协议定义的数据格式,批量发送给redis执行。
10、--bigkeys
统计bigkey的分布,使用scan命令对redis的键进行采样,从中找到内存占用比较大的键,这些键可能是系统的瓶颈。
该方法有两个缺点:
11、--eval
用于执行lua脚本
12、--latency
有三个选项,--latency、--latency-history、--latency-dist。它们可检测网络延迟,展现的形式不同。看不明白
13、--stat
可实时获取redis的重要统计信息。info命令虽然比较全,但这里可看到一些增加的数据,如requests(每秒请求数)
14、--raw 和 --no-raw
--no-raw 要求返回原始格式。--raw 显示格式化的效果。
redis-cli 命令有很多。比如
连接操作相关的命令
index
用数字值指定,以 0
作为起始索引值
服务端相关命令
ip:port
的客户端CONFIG GET *
来列出redis.conf
文件进行改写
发布订阅相关命令
message
发送到指定的频道 channel 例如publish msg "good morning"
对KEY操作的命令
对String操作的命令
对List操作的命令
对Set操作的命令
对Hash操作的命令
获取慢查询
SLOWLOG GET 10
结果为查询ID、发生时间、运行时长和原命令 默认10毫秒,默认只保留最后的128条。单线程的模型下,一个请求占掉10毫秒是件大事情,注意设置和显示的单位为微秒,注意这个时间是不包含网络延迟的。
slowlog get 获取慢查询日志
slowlog len 获取慢查询日志条数
slowlog reset 清空慢查询
配置:
config set slow-log-slower-than 20000
config set slow-max-len 1000
config rewrite
标签:union dbi timer smo utils 数据同步 信息 save unix
原文地址:https://www.cnblogs.com/zh-dream/p/12094075.html