High performance——对数据库高并发读写需求
Huge Storage——对海量数据高效存储与访问需求
High Scalability && High Availability——对数据库高可扩展性与高可用性需求
[root@redis ~]# yum -y install gcc gcc-c++ make
[root@redis ~]# mount.cifs //192.168.200.60/ccc /mnt
Password for root@//192.168.23.1/ccc:
[root@redis ~]# cd /mnt/redis/
[root@redis redis]# tar zxvf redis-5.0.7.tar.gz -C /opt '//redis源码包可以直接到官网下载'
[root@redis redis]# cd /opt/redis-5.0.7/
[root@redis redis-5.0.7]# make '//直接进行make'
[root@redis redis-5.0.7]# make PREFIX=/usr/local/redis/ install '//指定redis目录并安装'
[root@redis redis-5.0.7]# cd /usr/local/redis/
[root@redis redis]# ls
bin
[root@redis redis]# cd bin/
[root@redis bin]# ls
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server '//redis-cli是连接终端'
[root@redis bin]# cd /opt/redis-5.0.7/utils/ '//回到redis源码包解压目录'
[root@redis utils]# ./install_server.sh '//执行脚本进行配置'
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] '//选择redis默认接口,直接回车'
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] '//选择redis默认配置文件名称,直接回车'
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] '//选择默认redis日志文件名称,直接回车'
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/redis/bin/redis-server '//选择redis可执行文件路径,需要手动输入此路径:/usr/local/redis/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/redis/bin/redis-server
Cli Executable : /usr/local/redis/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!
redis配置文件
bind:监听的主机地址
port:端口
daemonize yes:启用守护进程
pidfile:指定PID文件
loglevel notice:日志级别
logfile:指定日志文件
[root@redis utils]# ln -s /usr/local/redis/bin/* /usr/local/bin '//将redis命令创建软连接,便于系统识别'
[root@redis utils]# netstat -ntap |grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 26085/redis-server
[root@redis utils]# /etc/init.d/redis_6379 stop '//先关闭redis服务'
Stopping ...
Redis stopped
[root@redis utils]# /etc/init.d/redis_6379 start '//开启redis服务'
Starting Redis server...
[root@redis utils]# netstat -ntap |grep 6379 '//再次检查redis开启情况'
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 36566/redis-server
tcp 0 0 127.0.0.1:6379 127.0.0.1:60648 TIME_WAIT -
设置监听端口,启动服务
[root@redis utils]# vim /etc/redis/6379.conf '//修改配置文件'
bind 127.0.0.1 192.168.200.90 '//添加监听端口'
[root@redis utils]# /etc/init.d/redis_6379 restart '//重启redis服务'
Stopping ...
Redis stopped
Starting Redis server...
[root@redis utils]# redis-cli -h 192.168.200.90 -p 6379 '//成功登陆'
192.168.200.90:6379> exit '//退出'
[root@redis utils]#
redis-benchmark测试工具
-h:指定服务器主机名
-p:指定服务器端口
-c:指定并发连接数
-n:指定请求数
-d:以字节的形式指定SET/GET值的数据大
向IP地址为192.168.200.90、端口为6379的Redis服务器发送100个并发连接与100000个请求测试性能
[root@redis utils]# redis-benchmark -h 192.168.200.90 -p 6379 -c 100 -n 100000 '//压测ip地址192.168.200.90,端口为6379,并发量100,请求量100000个'
...省略内容
'//截取部分典型的结果展示'
====== SET ====== '//写入速度'
100000 requests completed in 1.32 seconds '//耗费1.32秒'
100 parallel clients
3 bytes payload
keep alive: 1
====== GET ====== '//读取速度'
100000 requests completed in 1.02 seconds '//耗费1.02秒'
100 parallel clients
3 bytes payload
keep alive: 1
...省略内容
'//压测结果想要标准,需要多压测几次,取平均值即可'
[root@redis utils]# redis-benchmark -h 192.168.200.90 -p 6379 -q -d 100 '//压测IP地址为192.168.200.90,端口为6379,-q:强制退出redis,-d:指定字节数量'
'//以字节形式指定set/get值的数据大小,同样仅展示部分结果'
...省略内容
SET: 53248.14 requests per second
GET: 46253.47 requests per second
...省略内容
测试存取大小为100字节的数据包的性能
[root@localhost redis]# /usr/local/redis/bin/redis-benchmark -h 192.168.200.90 -p 6379 -q -d 100
PING_INLINE: 120481.93 requests per second
PING_BULK: 120627.27 requests per second
SET: 118063.76 requests per second
GET: 118483.41 requests per second
INCR: 118623.96 requests per second
LPUSH: 115473.45 requests per second
RPUSH: 116822.43 requests per second
LPOP: 116009.28 requests per second
RPOP: 116686.12 requests per second
SADD: 119904.08 requests per second
HSET: 120481.93 requests per second
SPOP: 118623.96 requests per second
LPUSH (needed to benchmark LRANGE): 119474.31 requests per second
LRANGE_100 (first 100 elements): 52938.06 requests per second
LRANGE_300 (first 300 elements): 21810.25 requests per second
LRANGE_500 (first 450 elements): 12210.01 requests per second
LRANGE_600 (first 600 elements): 8411.14 requests per second
MSET (10 keys): 101626.02 requests per second
返回值1 为操作成功,返回值为0操作失败
redis-cli命令行工具
[root@localhost utils]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379>
连接远程数据库
[root@localhost utils]# redis-cli -h 192.168.10.161 -p 6379
192.168.10.161:6379>
redis-cli命令行工具
[root@localhost utils]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> help @list
BLPOP key [key ...] timeout
summary: Remove and get the first element in a list, or block until one is available
since: 2.0.0
……
127.0.0.1:6379> help set
SET key value [EX seconds] [PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
set:存放数据
get:获取数据
[root@localhost ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> set teacher zhanglong
OK
127.0.0.1:6379> get teacher
"zhanglong"
keys:获取符合规则的键值列表
127.0.0.1:6379> set cname tt
OK
127.0.0.1:6379> keys *
1) "cname"
exists:判断键值是否存在
127.0.0.1:6379[3]> set bbb aa
OK
127.0.0.1:6379[3]> exists bbb
(integer) 1
127.0.0.1:6379[3]>
del:删除当前数据库的指定key
127.0.0.1:6379> set cname aa
OK
127.0.0.1:6379> keys *
1) "cname"
127.0.0.1:6379> exists cname
(integer) 1
127.0.0.1:6379> del cname
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
type:获取key对应的value值类型
127.0.0.1:6379[3]> set ttyy aa
OK
127.0.0.1:6379[3]> type ttyy
string
rename(覆盖)/ renamenx(不覆盖):对已有的key进行重命名
127.0.0.1:6379[3]> rename ttyy zznn
OK
127.0.0.1:6379[3]> keys *
1) "zznn"
127.0.0.1:6379[3]>
dbsize:查看当前数据库中key的数目
127.0.0.1:6379[3]> keys *
1) "zznn"
127.0.0.1:6379[3]> dbsize
(integer) 1
127.0.0.1:6379[3]>
其他常用redis命令
flushdb '//清空库'
192.168.79.133:6379> move uuu 11 '//将uuu键值对移动到第12个库'
(integer) 1
综合
'//Redis的5种数据结构:string、list、hash、set和zset'
'//可以使用help string/list/hash/set/zset来查询命令帮助,命令可以使用tab键补全'
例如:
[root@redis utils]# redis-cli -h 192.168.79.133 -p 6379
192.168.200.90:6379> help set '//查看set命令帮助'
SET key value [expiration EX seconds|PX milliseconds] [NX|XX] '//set命令的格式'
summary: Set the string value of a key
since: 1.0.0
group: string
192.168.200.90:6379> set username abc '//创建键(username)值(abc)'
OK
192.168.200.90:6379> set user abc '//创建键(user)值(abc)'
OK
192.168.200.90:6379> KEYS * '//查看所有键'
1) "username"
2) "user"
192.168.200.90:6379> keys u??? '//查看u开头且占4个字节的key'
1) "user"
192.168.200.90:6379> get user '//查看user键的值'
"abc"
192.168.200.90:6379> EXISTS user '//查看user是否存在'
(integer) 1
192.168.200.90:6379> del user '//删除user键'
(integer) 1
192.168.200.90:6379> keys *
1) "username" '//删除成功'
192.168.200.90:6379> rename username uuu '//将username键重命名为uuu'
OK
192.168.200.90:6379>keys *
1) "uuu" '//重命名成功'
192.168.200.90:6379> exit
库序号从0-15
[root@redis utils]# redis-cli -h 192.168.200.90 -p 6379 '//连接数据库'
192.168.200.90:6379> keys * '//默认进入第一个库0'
1) "key:__rand_int__"
2) "myset:__rand_int__"
3) "counter:__rand_int__"
4) "mylist"
5) "uuu"
192.168.200.90:6379> select 11 '//进入第12个库'
OK
192.168.200.90:6379[11]> keys * '//查看所有键'
(empty list or set)
192.168.200.90:6379[11]> select 0 '//返回第一个库'
OK
192.168.200.90:6379> move uuu 11 '//将uuu键值对移动到第12个库'
(integer) 1
192.168.200.90:6379> select 11 '//进入第12个库'
OK
192.168.200.90:6379[11]> keys * '//查看所有键'
1) "uuu"
192.168.200.90:6379[11]> get uuu '//查看键值'
"ab"
192.168.200.90:6379[11]> flushdb '//清空库'
OK
192.168.200.90:6379[11]> keys *
(empty list or set)
192.168.200.90:6379[11]> exit '//退出'
概述
是Redis默认的持久化方式
默认文件名为dump.rdb
[root@redis utils]# cd /var/lib/redis/6379/
[root@redis 6379]# ls
dump.rdb
[root@redis 6379]# vim /etc/redis/6379.conf '//修改配置文件'
'//搜索save,找到如下'
save 900 1 '//900秒之内至少一次写操作'
save 300 10 '//300秒内至少10次写操作'
save 60 10000 '//60秒内至少10000次写操作'
'//只要满足其一都会触发快照操作,注释所有的save项表示关闭RDB'
dbfilename dump.rdb '//RDB文件名称'
dir /var/lib/redis/6379 '//RDB文件路径'
rdbcompression yes '//开启了压缩功能'
[root@redis 6379]# vim /etc/redis/6379.conf
no-appendfsync-on-rewrite no '//在日志进行BGREWRITEAOF时, 如果设置为yes表示新写操作不进行同步fsync,只暂存在缓冲区里,避免造成磁盘I0操作冲突,等重写完成后在写入。redis中默认为no'
auto-aof-rewrite-percentage 100 '//当前AOF文件大小是上次日志重写时AOF文件大小两倍时,发生BGREWRITEAOF操作'
auto-aof-rewrite-min-size 64mb '//当前AOF文件执行BGREWRITEAOF命令的最小值,避免刚开始启动Reids时由于文件尺寸较小导致频繁的BGREWRITEAOF.当AOF文件到达64M的时候,发生BGREWRITEAOF操作'
'//查看Redis内存使用'
[root@redis 6379]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> INFO memory
...省略内容