Linux安装
1.redis官网下载安装包
2.把安装包通过xftp放到服务器的home目录下:
3.把压缩包移到opt目录下再进行解压:
[root@izbp19p16mcz7awt4d8kahz home]# mv redis-5.0.10.tar.gz /opt
[root@izbp19p16mcz7awt4d8kahz home]# cd /opt
[root@izbp19p16mcz7awt4d8kahz opt]# ll
total 1944
-rw-r--r-- 1 root root 1990507 Nov 28 18:00 redis-5.0.10.tar.gz
[root@izbp19p16mcz7awt4d8kahz opt]# tar -zxvf redis-5.0.10.tar.gz
4.进入加压后的文件,我们可以看到redis.conf等文件:
[root@izbp19p16mcz7awt4d8kahz opt]# cd redis-5.0.10
[root@izbp19p16mcz7awt4d8kahz redis-5.0.10]# ll
total 284
-rw-rw-r-- 1 root root 123280 Oct 27 14:49 00-RELEASENOTES
-rw-rw-r-- 1 root root 53 Oct 27 14:49 BUGS
-rw-rw-r-- 1 root root 2381 Oct 27 14:49 CONTRIBUTING
-rw-rw-r-- 1 root root 1487 Oct 27 14:49 COPYING
drwxrwxr-x 6 root root 4096 Oct 27 14:49 deps
-rw-rw-r-- 1 root root 11 Oct 27 14:49 INSTALL
-rw-rw-r-- 1 root root 151 Oct 27 14:49 Makefile
-rw-rw-r-- 1 root root 6888 Oct 27 14:49 MANIFESTO
-rw-rw-r-- 1 root root 20555 Oct 27 14:49 README.md
-rw-rw-r-- 1 root root 61797 Oct 27 14:49 redis.conf
-rwxrwxr-x 1 root root 275 Oct 27 14:49 runtest
-rwxrwxr-x 1 root root 280 Oct 27 14:49 runtest-cluster
-rwxrwxr-x 1 root root 373 Oct 27 14:49 runtest-moduleapi
-rwxrwxr-x 1 root root 281 Oct 27 14:49 runtest-sentinel
-rw-rw-r-- 1 root root 9710 Oct 27 14:49 sentinel.conf
drwxrwxr-x 3 root root 4096 Oct 27 14:49 src
drwxrwxr-x 11 root root 4096 Oct 27 14:49 tests
drwxrwxr-x 8 root root 4096 Oct 27 14:49 utils
5.安装基本环境gcc,运行下面命令:
yum install gcc-c++
6.执行make命令,把所有需要的文件全部配置上:
[root@izbp19p16mcz7awt4d8kahz redis-5.0.10]# make
7.redis的默认安装路径/usr/local/bin
[root@izbp19p16mcz7awt4d8kahz usr]# cd /usr/local/bin
[root@izbp19p16mcz7awt4d8kahz bin]# ll
total 32796
-rwxr-xr-x 1 root root 4367048 Nov 28 18:23 redis-benchmark
-rwxr-xr-x 1 root root 8134328 Nov 28 18:23 redis-check-aof
-rwxr-xr-x 1 root root 8134328 Nov 28 18:23 redis-check-rdb
-rwxr-xr-x 1 root root 4808272 Nov 28 18:23 redis-cli
lrwxrwxrwx 1 root root 12 Nov 28 18:23 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8134328 Nov 28 18:23 redis-server
8.到之前redis安装包解压的目录/opt/redis-5.0.10
种把redis.conf文件复制到这里,在复制前首先建一个twyconfig
文件夹:
[root@izbp19p16mcz7awt4d8kahz bin]# cp /opt/redis-5.0.10/redis.conf twyconfig
[root@izbp19p16mcz7awt4d8kahz bin]# ll
total 32800
-rwxr-xr-x 1 root root 4367048 Nov 28 18:23 redis-benchmark
-rwxr-xr-x 1 root root 8134328 Nov 28 18:23 redis-check-aof
-rwxr-xr-x 1 root root 8134328 Nov 28 18:23 redis-check-rdb
-rwxr-xr-x 1 root root 4808272 Nov 28 18:23 redis-cli
lrwxrwxrwx 1 root root 12 Nov 28 18:23 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8134328 Nov 28 18:23 redis-server
drwxr-xr-x 2 root root 4096 Nov 28 18:32 twyconfig
[root@izbp19p16mcz7awt4d8kahz bin]# cd twyconfig
[root@izbp19p16mcz7awt4d8kahz twyconfig]# ll
total 64
-rw-r--r-- 1 root root 61797 Nov 28 18:32 redis.conf
9.redis默认不是后台启动的,这里我们要修改一下配置文件,daemonize 默认是no这里修改为yes:
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
10.启动redis,到目录usr/local/bin
下 ,运行一下命令redis-server twyconfig/redis.conf
通过指定的redis.conf来启动,再使用redis-cli
客户端连接测试:
[root@izbp19p16mcz7awt4d8kahz bin]# ll
total 32800
-rwxr-xr-x 1 root root 4367048 Nov 28 18:23 redis-benchmark
-rwxr-xr-x 1 root root 8134328 Nov 28 18:23 redis-check-aof
-rwxr-xr-x 1 root root 8134328 Nov 28 18:23 redis-check-rdb
-rwxr-xr-x 1 root root 4808272 Nov 28 18:23 redis-cli
lrwxrwxrwx 1 root root 12 Nov 28 18:23 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8134328 Nov 28 18:23 redis-server
drwxr-xr-x 2 root root 4096 Nov 28 18:39 twyconfig
[root@izbp19p16mcz7awt4d8kahz bin]# redis-sever twyconfig/redis.conf
-bash: redis-sever: command not found
[root@izbp19p16mcz7awt4d8kahz bin]# redis-server twyconfig/redis.conf
31253:C 28 Nov 2020 18:49:44.787 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31253:C 28 Nov 2020 18:49:44.787 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=31253, just started
31253:C 28 Nov 2020 18:49:44.787 # Configuration loaded
[root@izbp19p16mcz7awt4d8kahz bin]# redis-cli
127.0.0.1:6379> set name twy
OK
127.0.0.1:6379> get name
"twy"
127.0.0.1:6379>
11.ps -ef|grep redis
查看redis的进程是否开启:
[root@izbp19p16mcz7awt4d8kahz bin]# ps -ef|grep redis
root 31254 1 0 18:49 ? 00:00:00 redis-server 127.0.0.1:6379
root 31549 23838 0 18:57 pts/0 00:00:00 grep --color=auto redis
12.关闭redis服务,到redis-cli客户端,执行shutdown
命令:
[root@izbp19p16mcz7awt4d8kahz bin]# redis-cli
127.0.0.1:6379> shutdown
not connected>
not connected>
[root@izbp19p16mcz7awt4d8kahz bin]# ps -ef|grep redis
root 31741 23838 0 19:01 pts/0 00:00:00 grep --color=auto redis
使用docker安装
1. docker pull redis:latest //下载redis
2. docker images //查看
3. docker run -itd --name redis-twy -p 6379:6379 redis //运行redis容器
4. docker ps //查看是否运行
5. docker exec -it redis-twy /bin/bash //使用 docker exec -it redis /bin/bash进入redis
6. redis-cli //使用 redis-cli 可以测试连接
上面就是使用docker进行安装的命令,其中docker exec -it redis-twy /bin/bash
进入redis内部,redis-cli
连接测试:
redis-benchmark 性能测试
redis 性能测试工具可选参数如下所示:(引用菜鸟教程图片)
我们简单写一个命令来测试一下:
//50个并发连接 100000请求
redis-benchmark -h IP -p 6379 -c 50 -n 100000
如何查看这些分析?
//100000个请求要3.45秒
100000 requests completed in 3.45 seconds
//50个并行的客户端
50 parallel clients
//每次只写入3个字符串
3 bytes payload
//每次只有一台服务器连接
keep alive: 1
host configuration "save": 3600 1 300 100 60 10000
host configuration "appendonly": no
multi-thread: no
44.81% <= 1 milliseconds
99.02% <= 2 milliseconds
99.99% <= 3 milliseconds
100.00% <= 3 milliseconds
//每秒处理29231.22 次
29231.22 requests per second
基本知识
(1)redis有16个数据库,默认使用第0个数据库,可以使用select进行切换,下面我们切换到第4个数据库:
127.0.0.1:6379> select 4 //切换数据库
OK
127.0.0.1:6379[4]> select 0
OK
127.0.0.1:6379> dbsize //查看DB大小
(integer) 10 //说明里面有10个值
127.0.0.1:6379>
(2)清空当前数据库数据flushdb
,清除全部数据库数据flushall
(3)redis是单线程的,它是基于内存操作的,CPU不是redis的性能瓶颈,redis的瓶颈是机器的内存和网络的带宽。
(4)redis单线程为什么还这么快?
redis是将所有的数据都放在内存中,因为多线程(CPU上下文切换,比较耗时),所有说使用单线程操作效率更高。