linux下的redis安装
官网
redis官方网站的地址是https://redis.io/。官网上有软件的下载地址以及相关文档。
安装步骤
-
获取redis文件。
从官网上获取最新的网址,使用命令:> wget https://download.redis.io/releases/redis-6.2.6.tar.gz
下载最新的软件。
注意:如果无法执行wget命令,可以安装wget。> yum install wget /**linux系统下**/ > apt-get install wget /**ubuntu系统下**/
-
解压文件
> tar zxvf redis-6.2.6.tar.gz
-
目录介绍
- deps redis安装的依赖功能
- src redis源代码目录
- tests redis测试用例的目录
- utils redis工具目录
-
执行编译
> cd redis-6.2.6/src > make
执行编译可以在
redis-6.2.6
目录下,命令会通过目录下的Makefile
命令找到srcMakefile
,最终执行src/Makefile
下的指令进行编译,如果编译报错,你会需要安装gcc
,Ubuntu系统下,可能还需要安装make
。> yum install gcc /**linux系统下**/ > apt-get install gcc /**ubuntu系统下**/ > apt-get install make /**ubuntu系统下**/
-
运行测试
运行完成后可以执行make test
命令检查编译的结果:> make test
这些测试的脚本均在tests目录下,涉及很多方面,如:各种数据类型的设值、取值、lua、sential等。当然运行时间比较长,如非生产环境,可以不运行。
-
进行安装
> make install
命令执行后默认会把程序安装到
/usr/local
目录下,你也可以通过命令参数指定安装目录:> make install PREFIX=/usr/local
-
测试安装
> cd /usr/local /**你安装的目录**/ > ./redis-server
这时一般会看到如下信息:
2070:C 01 Mar 2022 17:28:19.489 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=2070, just started 2070:C 01 Mar 2022 17:28:19.489 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 2070:M 01 Mar 2022 17:28:19.490 * Increased maximum number of open files to 10032 (it was originally set to 1024). 2070:M 01 Mar 2022 17:28:19.490 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 2070 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 2070:M 01 Mar 2022 17:28:19.492 # Server initialized 2070:M 01 Mar 2022 17:28:19.492 # 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. 2070:M 01 Mar 2022 17:28:19.493 * Ready to accept connections
如果看到这段就意味着redis安装成功了。
-
连接redis数据库
redis连接的命令为redis-cli
。我们在命令行执行该命令,通常都是连接到127.0.0.1:6379。> redis-cli
你也可以指定连接的端口和主机
> redis-cli -h 127.0.0.1 -p 6379
你也可以指定相应的数据库,默认为0号库(redis在默认情况下有16个数据)。
> redis-cli -n 1
redis数据库连接后可以进行简单的操作:
127.0.0.1:6379> set name test OK 127.0.0.1:6379> get name "test" 127.0.0.1:6379> keys * 1) "name"
如果上述命令可以按预期执行,那么就代表着redis已经安装成功了。
-
设置redis作为服务
大部分情况下,我们希望redis作为一个服务来启动与停止。当然这个的前提就是上述的安装时正确无误的。如果需要安装服务,我们需要设置环境变量。我们需要修改/etc/profile
文件,在文件最后增加两行后保存:export REDIS_HOME=/usr/local/bin/redis export PATH=$PATH:$REDIS_HOME/bin
执行
source
命令,用echo
命令进行验证:> source /etc/profile > echo $PATH
在软件安装目录设置到PATH中成功之后,我们便可以使用utils目录下的install_server.sh脚本。
> ./install_server.sh
执行命令后,会需要你设置相关参数,包含端口(默认6379),配置文件、日志、数据文件等,默认的情况下会看到如下信息:
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/bin/redis/bin/redis-server Cli Executable : /usr/local/bin/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... Success! Starting Redis server... Installation successful!
这时redis服务便安装完成了,我们可以根据如下命令启动和停止服务:
> service redis_6370 start > service redis_6370 stop
在windows下的redis安装
redis官方并未提供windows版本的支持,如果需要在windows系统中运行redis,可以下载 Redis on Windows,现在最新的版本为3.0,所以下载Redis-x64-3.0.504.zip文件,解压后便可以使用,你也可以下载msi的安装文件来安装redis。