Redis安装和配置

1、首先在官网下载好redis-6.0.9.tar.gzhttp://redis.io/

或者使用 wget 命令下载:wget http://download.redis.io/releases/redis-6.0.9.tar.gz

2、下载使用上传到阿里云

图1

3、进入到 /usr/localhost/java/ 目录,开始解压安装

tar -zxvf redis-6.0.9.tar.gz

#进入到redis-6.0.9目录执行make命令
make
#注意:这里执行make的时候可能会报错,这是可能没有gcc的原因,需要安装,命令为
yum install gcc-c++

#再次执行make命令,执行时先清除上次没安装成功的一些make内容
make distclean
make 
make install

#注意:这里gcc版本过低,也会导致安装不成功

#查看gcc版本
gcc -v  
#升级gcc版本,依次执行已下命令升级gcc
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
#以上为临时使用,需要长期使用需要执行一下命令
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

4、安装完成之后会默认在/usr/local/bin下生成一下redis的可执行文件,为了方便可以在redis-6.0.9建一个bin目录,把redis这些可执行文件都放到bin目录下,方便执行。

图2

执行./redis-server命令

出现Ready to accept connections,说明已经启动成功

图3

5、连接redis执行./redis-cli

图4

6、这时redis已经安装成功,我们来进行设置redis.conf配置文件:

  • redis默认是本机访问,其他地方无法连接,需要把这个注释掉#bind 127.0.0.1改成bind 0.0.0.0

    图5

  • 设置后台启动redis服务 设置为yes

图6

  • 执行后台启动./redis-server redis.conf

图7

  • 这是redis持久化默认为no,需要改为yes

图8

  • redis远程连接后,输入命令出现Error:Server closed the connection,不需要登陆密码,改为no

图9

#常用命令
#后台启动的两种方式
./redis-server redis.conf 
nohup ./redis-server redis.conf &
nohup ./redis-server  redis.conf  >> /usr/local/java/redis-6.0.9/logs/redis.log  2>&1 &
#后台验证redis是否在启动
ps -ef |grep redis 或 ps aux | grep redis
#查看端口是否在监听
netstat -lntp | grep 6379
#关闭客户端
redis-cli shutdown
  • Redis持久化报错
redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
#将stop-writes-on-bgsave-error设置为no
127.0.0.1:6379> config set stop-writes-on-bgsave-error no
或者进入redis.conf改no

图10

  • 注意:如果 redis 不设置登录密码在服务器上会出现key丢失情况,设置密码

图11

  • 在 redis 中所有的 key 都变成 backup 是因为 redis 暴漏在公网ip下,没有设置密码,遭到恶意请求

图12

  • 如果出现一下错误修改 protected-mode yes 改为:protected-mode no,密码太短也有可能会出现这个问题
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.43.129:6379
    at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:95)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:82)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:211)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
    at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:198)
    at com.leyou.test.RedisTest.testRedis(RedisTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
 
 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.43.129:6379
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
    at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:233)
    at io.lettuce.core.RedisClient.connectStandalone(RedisClient.java:253)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:202)
    at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:56)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:959)
    ... 41 more

Redis Docker 安装测试

docker官方文档安装方式:https://hub.docker.com/_/redis

redis.conf 配置文件修改

# 将bind 127.0.0.1 -::1修改为 bind 0.0.0.0,监听来自任意网络接口的连接。
bind 0.0.0.0

# 将protected-mode设置为no,关闭保护模式,接收远程连接。
protected-mode no

# appendonly 设置为yes redis持久化
appendonly yes

# 将requirepass设置为 adminer,客户端连接到节点的认证密码
requirepass admin123

启动命令

# 拉取最新镜像
docker pull redis:latest
# 启动
docker run --name myredis -p6379:6379 --restart=always -d redis:latest --requirepass "test123456"
# redis 使用命令行设置密码和持久化方式
docker run --name myredis -v /data/redis/data:/data \
-p 6379:6379 -d redis:latest --requirepass "test123456" --appendonly yes 

# 指定配置文件启动 如果要挂载配置文件,先把提前准备好的 redis.conf 配置文件放到 /data/redis/conf 目录下(或者先启动一个 redis 镜像,把容器内的配置文件复制到容器外部)
docker cp myredis:/etc/redis/redis.conf /data/redis/conf/redis.conf

docker run --name myredis -v /data/redis/conf/redis.conf:/etc/redis/redis.conf \
-v /data/redis/data:/data -p 6379:6379 --restart=always -d redis:latest redis-server /etc/redis/redis.conf --requirepass "test123456" --appendonly yes 

docker run --name myredis -v /data/redis/conf/redis.conf:/etc/redis/redis.conf \
-v /data/redis/data:/data -p 6379:6379 --restart=always -d redis:latest redis-server /etc/redis/redis.conf --appendonly yes --requirepass "test123456" 

# redis 使用自定义配置方式启动,如果要修改配置文件直接修改
docker run --name myredis \
-v /data/redis/conf/redis.conf:/etc/redis/redis.conf \
-v /data/redis/data:/data -p 6379:6379 -d redis:latest redis-server /etc/redis/redis.conf 
最后修改:2024 年 07 月 17 日
如果觉得我的文章对你有用,请随意赞赏