访问 https://redis.io/download/#redis-downloads 地址,根据需要选择合适版本下载。
本教程以 5.0.14 版本为例,下面将使用 wget 下载 Redis,如下:
hxstrive@hxstrive:~$ wget https://download.redis.io/releases/redis-5.0.14.tar.gz --2023-02-01 13:08:16-- https://download.redis.io/releases/redis-5.0.14.tar.gz Resolving download.redis.io (download.redis.io)... 45.60.125.1 Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 2000179 (1.9M) [application/octet-stream] Saving to: ‘redis-5.0.14.tar.gz’ redis-5.0.14.tar.gz 100%[==========================>] 1.91M 345KB/s in 15s 2023-02-01 13:08:32 (133 KB/s) - ‘redis-5.0.14.tar.gz’ saved [2000179/2000179]
使用 tar -xvzf 命令解压安装包,如下:
hxstrive@hxstrive:~$ tar -xvzf redis-5.0.14.tar.gz redis-5.0.14/ redis-5.0.14/.gitignore redis-5.0.14/00-RELEASENOTES redis-5.0.14/BUGS redis-5.0.14/CONTRIBUTING redis-5.0.14/COPYING redis-5.0.14/INSTALL redis-5.0.14/MANIFESTO ...
使用 make 命令编译 Redis 源码,如下:
hxstrive@hxstrive:~$ cd redis-5.0.14/ hxstrive@hxstrive:~/redis-5.0.14$ make cd src && make all make[1]: Entering directory '/home/hxstrive/redis-5.0.14/src' CC Makefile.dep rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark (cd ../deps && make distclean) make[2]: Entering directory '/home/hxstrive/redis-5.0.14/deps' (cd hiredis && make clean) > /dev/null || true (cd linenoise && make clean) > /dev/null || true ... INSTALL redis-check-aof Hint: It's a good idea to run 'make test' ;) make[1]: Leaving directory '/home/hxstrive/redis-5.0.14/src'
注意:在 Ubuntu 中,如果 gcc 使用国内软件源安装会报错,需要切换成国外软件源(Ubuntu默认软件源)。如下:
# 卸载gcc sudo apt remove gcc # 修改和更新软件源 sudo vi sources.list sudo apt-get update sudo apt-get upgrade # 重新安装gcc sudo apt install gcc
最后,进入到 src 目录,此时生成了 redis-server 和 redis-cli 可执行文件。
启动 redis 服务,如下:
$ ./redis-server
注意这种方式启动 redis 使用的是默认配置。如下:
hxstrive@hxstrive:~/redis-5.0.14$ ./redis-server 12257:C 01 Feb 2023 13:37:14.094 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12257:C 01 Feb 2023 13:37:14.094 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=12257, just started 12257:C 01 Feb 2023 13:37:14.094 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf 12257:M 01 Feb 2023 13:37:14.095 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 5.0.14 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 12257 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 12257:M 01 Feb 2023 13:37:14.095 # Server initialized 12257:M 01 Feb 2023 13:37:14.095 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 12257:M 01 Feb 2023 13:37:14.095 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 12257:M 01 Feb 2023 13:37:14.096 * Ready to accept connections
也可以通过启动参数告诉 redis 使用指定配置文件使用下面命令启动,如下:
$ ./redis-server redis.conf
redis.conf 是一个默认的配置文件,我们可以根据需要使用自己的配置文件。
启动 redis 服务进程后,就可以使用测试客户端程序 redis-cli 和 redis 服务交互了。例如:
hxstrive@hxstrive:~/redis-5.0.14$ ./redis-cli 127.0.0.1:6379> set url www.hxstrive.com OK 127.0.0.1:6379> get url "www.hxstrive.com" 127.0.0.1:6379>
上面输出可知,成功链接到 Redis 服务,并且写入和读取键 url。
注意
在 Ubuntu 中可以使用如下命令安装 redis 服务,如下:
(1)更新软件源
sudo apt-get update(2)安装 redis 服务
sudo apt-get install redis-server(3)启动 redis 服务
redis-server(4)查看 redis 是否启动?
redis-cli以上命令将打开以下终端:
redis 127.0.0.1:6379> redis 127.0.0.1:6379> ping PONG以上说明我们已经成功安装了redis。