1、下载安装包!redis-6.2.4.tar.gz
$ cd /home/wlk $ wget http://download.redis.io/releases/redis-6.0.6.tar.gz
2、解压安装包到 /opt下,可以看到redis.conf文件
$ tar xzf redis-6.0.6.tar.gz -C /opt $ cd redis-6.0.6 或者 cp redis-6.0.6.tar.gz /opt tar -zxvf redis-6.0.6.tar.gz
3、修改Linux的镜像源
cp /etc/apt/sources.list /etc/apt/sources.list.bak vi /etc/apt/sources.list 查看win10子系统Ubuntu的版本名 lsb_release -c “eoan”代表ubuntu19.10,“xenial”代表ubuntu16.04,“bionic”代表ubuntu18.04,“disco”代表ubuntu19.04,“focal”代表ubuntu20.04 deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse 更换镜像源之后: sudo apt update 或者 apt-get update sudo apt upgrade 或者 apt-get upgrade --fix-missing
4、安装gcc环境
安装gcc环境 sudo apt-get install build-essential 执行 make 执行 make test You need tcl 8.5 or newer in order to run the Redis test. 安装Redis,运行make test的时候出错: You need tcl 8.5 or newer in order to run the Redis test make: *** [test] Error 1 安装tcl就行了: 复制代码 wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz sudo tar xzvf tcl8.6.1-src.tar.gz -C /usr/local/ cd /usr/local/tcl8.6.1/unix/ sudo ./configure sudo make sudo make install
5、make命令安装redis
$ cd redis-6.0.6 $ make $ make install #此命令是检查make命令执行之后,是否成功 ## 默认安装路径是 /usr/local/bin root@LAPTOP-Q063DEUM:/usr/local# cd bin/ root@LAPTOP-Q063DEUM:/usr/local/bin# ll total 52628 drwxr-xr-x 1 root root 4096 Jun 20 13:38 ./ drwxr-xr-x 1 root root 4096 Jun 20 10:55 ../ -rwxr-xr-x 1 root root 7100584 Jun 20 13:38 redis-benchmark* -rwxr-xr-x 1 root root 11863856 Jun 20 13:38 redis-check-aof* -rwxr-xr-x 1 root root 11863856 Jun 20 13:38 redis-check-rdb* -rwxr-xr-x 1 root root 7103192 Jun 20 13:38 redis-cli* lrwxrwxrwx 1 root root 12 Jun 20 13:38 redis-sentinel -> redis-server* -rwxr-xr-x 1 root root 11863856 Jun 20 13:38 redis-server* -rwxr-xr-x 1 root root 16888 Jun 20 11:03 tclsh8.6* root@LAPTOP-Q063DEUM:/usr/local/bin# pwd /usr/local/bin
6、备份redis.conf文件到redis的安装目录下,/usr/local/bin
$ cd /opt/redis-6.0.6 $ cp redis.conf /usr/local/bin/wlkconfig/
7、reids默认不是后台启动,修改配置文件 redis.conf
daemonize yes
8、启动redis服务
redis-server wlkconfig/redis.conf
通过指定配置文件来启动
root@LAPTOP-Q063DEUM:/usr/local/bin# redis-server wlkconfig/redis.conf 18616:C 20 Jun 2021 14:05:45.681 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 18616:C 20 Jun 2021 14:05:45.681 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=18616, just started 18616:C 20 Jun 2021 14:05:45.681 # Configuration loaded
9、使用redis-cli连接
root@LAPTOP-Q063DEUM:/usr/local/bin# redis-cli -h localhost -p 6379 localhost:6379> ping PONG localhost:6379> set menu new Date (error) ERR syntax error localhost:6379> set menu 2021-06-20 OK localhost:6379> keys (error) ERR wrong number of arguments for 'keys' command localhost:6379> keys * 1) "menu" localhost:6379> get menu "2021-06-20" localhost:6379>
10、查看redis进程是否开启ps -ef | grep redis
root@LAPTOP-Q063DEUM:/usr/local/bin# ps -ef | grep redis root 18617 1 0 14:05 ? 00:00:00 redis-server 127.0.0.1:6379 root 18630 85 0 14:11 pts/0 00:00:00 redis-cli -p 6379 root 18632 14241 0 14:11 pts/1 00:00:00 grep --color=auto redis
11、关闭redis服务 shutdown
./redis-cli shutdown
redis-benchmark是一个压力测试工具,官方自带的性能测试工具
redis-benchmark命令行参数
$ redis-benchmark -h localhost -p 6379 -c 100 -n 100000 ## 100个并发,10万请求 ====== SET ====== 100000 requests completed in 1.74 seconds 100 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 900 1 300 10 60 10000 host configuration "appendonly": no multi-thread: no 75.03% <= 1 milliseconds 100.00% <= 1 milliseconds 57504.31 requests per second 参数项解读 1、对10万个请求进行写入测试 2、100个并发客户端 3、每次写入3个字节 4、Keep alive 保持连接只有一个 5、所有请求在1毫秒内处理完成 6、每秒处理57504次请求。
redis默认有16个数据库,默认使用第0个,可以使用select进行切换
使用 DBSIZE命令查看当前数据库使用大小
查看当前数据库所有的key keys *
清除当前数据库 flushdb
清除全部的数据库的内容 flushall
redis是单线程的
redis是很快的,官方表示,redis是基于内存操作的,CPU不是redis性能瓶颈,redis的瓶颈是根据机器内存和网络带宽,既然可以使用单线程来实现,就使用单线程了
1、误区1:高性能的服务器一定是多线程的
2、误区2:多线程(CPU上下文切换)一定比单线程效率高
核心:
读写速度:CPU > 内存 > 硬盘
redis是将所有的数据放在内存中的,所以说使用单线程去操作效率是最高的,多线程(CPU上下文切换:耗时的操作),对于内存系统来说,如果没有上下文切换效率就是最高的
127.0.0.1:6379> keys * ## 查看所有的key 1) "menu" 2) "key:{tag}:__rand_int__" 127.0.0.1:6379> set name 韦立凯 ## 设置一个key OK 127.0.0.1:6379> keys * 1) "name" 2) "menu" 3) "key:{tag}:__rand_int__" 127.0.0.1:6379> get name ## 查看一个key "\xe9\x9f\xa6\xe7\xab\x8b\xe5\x87\xaf" 127.0.0.1:6379> expire name 10 ## 设置key的过期时间,单位是秒 (integer) 1 127.0.0.1:6379> ttl name ## 查看key的剩余时间 (integer) 7 127.0.0.1:6379> ttl name (integer) 4 127.0.0.1:6379> ttl name (integer) 2 127.0.0.1:6379> ttl name (integer) 0 127.0.0.1:6379> keys * 1) "menu" 2) "key:{tag}:__rand_int__" 127.0.0.1:6379> type menu ## 查看key的类型 string 127.0.0.1:6379> type key:{tag}:__rand_int__ string 127.0.0.1:6379> 127.0.0.1:6379> exists menu ## 判断key是否存在 (integer) 1 127.0.0.1:6379> exists name (integer) 0 127.0.0.1:6379> set name wlk OK 127.0.0.1:6379> keys * 1) "name" 2) "menu" 3) "key:{tag}:__rand_int__" 127.0.0.1:6379> move name 3 ## 将key移到别的库(共有16个库) (integer) 1 127.0.0.1:6379> keys * 1) "menu" 2) "key:{tag}:__rand_int__" 127.0.0.1:6379> 127.0.0.1:6379> 127.0.0.1:6379> select 3 ## 切换数据库 OK 127.0.0.1:6379[3]> keys * 1) "name" 127.0.0.1:6379[3]> get name "wlk"
127.0.0.1:6379> keys * ## 查看所有的key 1) "name" 2) "menu" 3) "key:{tag}:__rand_int__" 127.0.0.1:6379> set age 28 ## 设置值 OK 127.0.0.1:6379> get menu ## 获取值 "2021-06-20,2021-06-21" 127.0.0.1:6379> exists age ## 判断一个key是否存在 (integer) 1 127.0.0.1:6379> strlen menu ## 获取key的长度 (integer) 21 127.0.0.1:6379> append menu ",2021-06-21 23:01" ## append命令追加字符串,如果key不存在,则set key (integer) 38 127.0.0.1:6379> get menu "2021-06-20,2021-06-21,2021-06-21 23:01" 127.0.0.1:6379> get age "28" 127.0.0.1:6379> incr age ## incr,增加1 (integer) 29 127.0.0.1:6379> incrby age 2 ## 指定步长,增加2 (integer) 31 127.0.0.1:6379> get age "31" 127.0.0.1:6379> decr age ## decr,减少1 (integer) 30 127.0.0.1:6379> decrby age 3 ## 指定步长,减少3 (integer) 27 127.0.0.1:6379> get age "27" 127.0.0.1:6379> ######################################################## 字符串范围 127.0.0.1:6379> set key1 "hello,chenFeng" OK 127.0.0.1:6379> get key1 "hello,chenFeng" 127.0.0.1:6379> GETRANGE key1 0 3 ## 截取字符串 [0,3] "hell" 127.0.0.1:6379> GETRANGE key1 0 -1 ## 获取整个完整的key值 "hello,chenFeng" ######################################################### 替换 127.0.0.1:6379> set key2 abcdefg OK 127.0.0.1:6379> get key2 "abcdefg" 127.0.0.1:6379> SETRANGE key2 1 xx ## 替换指定位置开始的字符串 (integer) 7 127.0.0.1:6379> get key2 "axxdefg" ########################################################## setex (set with expire) ## 设置过期时间 setnx (set if not exist) ## 如果不存在,则set值 (在分布式锁中会常常使用到) 127.0.0.1:6379> setex key3 30 "chenfeng,i like you" ## 设置key3的值 30秒后过期 OK 127.0.0.1:6379> get key3 "chenfeng,i like you" 127.0.0.1:6379> ttl key3 ## 查看key3的剩余过期时间 (integer) 23 127.0.0.1:6379> setnx key4 "mongoDb" ## 如果key4不存在,则创建key4,值为 "mongoDb" (integer) 1 127.0.0.1:6379> get key3 (nil) 127.0.0.1:6379> get key4 "mongoDb" 127.0.0.1:6379> setnx key4 "redis" ## 如果key4存在,创建失败,且不会替换原有的值 (integer) 0 127.0.0.1:6379> get key4 "mongoDb" ######################################################### mset 批量设置值 mget 批量获取值 127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 ## 批量设置值 OK 127.0.0.1:6379> keys * 1) "k1" 2) "k3" 3) "k2" 127.0.0.1:6379> mget k1 k2 k3 ## 批量获取值 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379> MSETNX k1 v1 k4 v4 ## 此操作是原子性操作,因前面k1 已存在,所以k4 也是设置失败 (integer) 0 127.0.0.1:6379> keys * 1) "k1" 2) "k3" 3) "k2" ##################################################### # 对象 设置一个user:1 对象,值为一个JSON字符串 127.0.0.1:6379> set user:1 {name:zhangsan,age:25} OK 127.0.0.1:6379> keys * 1) "user:1" 127.0.0.1:6379> get user:1 "{name:zhangsan,age:25}" 127.0.0.1:6379> mset user:1:name wangwu user:1:age 28 ## 这里的key是个巧妙的设计,user:{id}:{filed},如此设计在redis中完全OK的 OK 127.0.0.1:6379> keys * 1) "user:1:age" 2) "user:1:name" 3) "user:1" 127.0.0.1:6379> get user:1 "{name:zhangsan,age:25}" 127.0.0.1:6379> 127.0.0.1:6379> get user:1:name "wangwu" 127.0.0.1:6379> get user:1:age "28"