CentOS安装Redis

CentOS7安装redis

# 安装redis编译的c环境
yum install -y gcc
#查看gcc是否安装成功
rpm -q gcc
#下载redis安装包
wget http://download.redis.io/releases/redis-6.2.2.tar.gz
#解压安装包
tar -zxvf redis-6.2.2.tar.gz
#进入redis-6.2.2解压完的文件夹
cd redis-6.2.2
#编译redis
make
#安装编译后的文件到安装到指目录
#注意:PREFIX必须大写、同时会自动为我们创建redis目录,并将结果安装此目录
make PREFIX=/usr/local/redis install

配置redis

#在安装目录中创建文件夹etc
mkdir /usr/local/redis/etc
#复制解压文件夹中的配置文件到安装目录中的etc文件夹
cp /usr/local/redis-6.2.2/redis.conf /usr/local/redis/etc
#打开安装目录中的配置文件
cd /usr/local/redis/etc
vim redis.conf

配置redis为后台启动

daemonize no 修改为 daemonize yes

修改前:

修改后:

开启外网访问

注释掉 bind 127.0.01

修改前:

修改后:

配置密码

requirepass 设置密码

开放服务端口

#查看6379端口是否开启
firewall-cmd --query-port=6379/tcp
#开启6379端口
firewall-cmd --permanent --add-port=6379/tcp
#重新启动防火墙
firewall-cmd --reload

Redis启动

前台启动

cd /usr/local/redis/bin
./redis-server

后台启动

 cd /usr/local/redis/bin
 ./redis-server ../etc/redis.conf

设置开机启动

添加开机启动服务

vi /etc/systemd/system/redis.service

在新建的文件中写入下面内容

[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

注意:ExecStart配置成自己的路径

设置开机启动

systemctl daemon-reload
systemctl start redis.service
systemctl enable redis.service

创建 redis 命令软链接

ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis

RedisDeskManager无法连接到Redis主机

1.用telnet连接redis

telnet 192.168.10.10 6379

2.结果如下

-DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. 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) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

3.错误原因

由于redis的保护模式开启了,并且没有绑定ip地址,没有密码认证

4.解决方案

  • 方案一:

    使用 设置CONFIG SET protected-mode no

    步骤:

    1. 在redis服务器上输入redis,进入redis控制台

      redis
      
    2. 临时关闭保护模式

      CONFIG SET protected-mode no
      
    3. 再次用客户端连接redis发现已经可以连接上了

    缺点:

    ​ 该种方式只是短暂性有效,如果redisServer重新启动后还是不能正常链接

  • 方案二:

    编辑redis配置文件,将保护模式关闭

    步骤:

    ​ 由于我是直接使用redis-server 命令启动的,所以说其服务是按照其默认设置进行启动,修改/usr/local/etc/redis.conf

如果没有请从下载的redis中复制一个redis.conf到这里,

​ 将bind 后面绑定的ip后面加入你本机外网的ip

bind 127.0.0.1 192.168.21.4

​ 然后保存,再使用redis-server /usr/local/etc/redis.conf启动

​ 问题:

  1.redis-cli不能直接使用,必须输入绑定的ip+d端口号才能正常使用

  2.不安全

​ 2). 将redis.conf中安全模式关闭

​ 将该保护模式改为no再试

​ 缺点:1.保护模式关闭后不安全

3)设置密码访问模式

在redis.conf中加入一行密码设置

require 你的密码

保存,再重新启动redis客户端

以上四种方式解决该错误都可以,但是根据上面的优缺点,

选择第四种方式最佳,安全性最高

posted @ 2021-05-16 12:54  求财若渴  阅读(74)  评论(0)    收藏  举报