Redis之Linux下的安装配置
Redis下载
Linux下下载源码安装配置
-
方式一
官网下载:https://redis.io/download
其他版本下载:https://download.redis.io/releases/
-
方式二(推荐)
GitHub下载:https://github.com/redis/redis/releases
-
方式三(推荐)
服务器wget直接下载
wget https://download.redis.io/releases/redis-7.2.2.tar.gz
上传服务器,解压安装
我这里下载的是7.2.2版本
-
将下载的【redis-7.2.2.tar.gz】上传到服务器
-
解压
# 解压缩 tar -zxvf redis-7.2.2.tar.gz # ll [root@localhost opt]# ll redis* -rw-r--r--. 1 root root 3422479 5月 7 15:03 redis-7.2.2.tar.gzredis-7.2.2: 总用量 248 -rw-rw-r--. 1 root root 19674 10月 18 2023 00-RELEASENOTES -rw-rw-r--. 1 root root 51 10月 18 2023 BUGS -rw-rw-r--. 1 root root 5027 10月 18 2023 CODE_OF_CONDUCT.md
-
编译
由于redis是c语言编写,需要先安装gcc环境
# 安装gcc编译环境 yum install gcc-c++ # 进入到redis目录,进入redis-7.2.2目录,然后执行make cd /opt/redis-7.2.2/ # 执行make make # 如果执行make报错:致命错误:jemalloc/jemalloc.h:没有那个文件或目录,则执行 make MALLOC=libc
出现下面方为正确:
-
安装
# 进行安装,PREFIX指定安装目录 make PREFIX=/usr/local/redis install # 查看redis安装后的目录结构 cd /usr/local/redis/bin # ll [root@localhost bin]# ll 总用量 12024 -rwxr-xr-x. 1 root root 1069400 5月 7 15:38 redis-benchmark lrwxrwxrwx. 1 root root 12 5月 7 15:38 redis-check-aof -> redis-server lrwxrwxrwx. 1 root root 12 5月 7 15:38 redis-check-rdb -> redis-server -rwxr-xr-x. 1 root root 1790952 5月 7 15:38 redis-cli lrwxrwxrwx. 1 root root 12 5月 7 15:38 redis-sentinel -> redis-server -rwxr-xr-x. 1 root root 9437728 5月 7 15:38 redis-server
启动
-
启动
# 此方式前台启动,关闭窗口或者结束命令,程序即停止 /usr/local/redis/bin/redis-server
-
后台启动
nohup /usr/local/redis/bin/redis-server &
测试
-
测试
[root@localhost bin]# /usr/local/redis/bin/redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379>
指定配置文件启动
-
构建配置文件
默认安装完成没有配置文件,启动是redis默认的一个,在redis的源码目录有一个redis.conf,我们把它拷贝一份
# 创建redis配置文件文件夹 mkdir /usr/local/redis/conf # 拷贝配置文件 cp /opt/redis-7.2.2/redis.conf /usr/local/redis/conf/
-
启动
# 此方式前台启动,关闭窗口或者结束命令,程序即停止 /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
-
后台启动
nohup /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf &
设置密码
如果不设置密码,通过真实IP连接会出现下面提示:
[root@localhost bin]# ./redis-cli -h 192.168.126.130
192.168.126.130:6379> ping
(error) DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Set up an authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
意思是不安全的,需要关掉保护模式,或者设置密码,那么建议设置密码:
# 编辑redis.conf
vi /usr/local/redis/conf/redis.conf
# 找到requirepass,添加一行,requirepass后是密码
requirepass redis