修改配置文件

设置后台启动

redis.conf配置文件中里面的daemonize no 改成 yes

daemonize yes

设置远程访问

redis.conf配置文件中里面的bind 那一行注释掉

#bind 127.0.0.1 -::1

关闭保护模式

将本机访问保护模式设置no

protected-mode no

设置密码

requirepass 密码
举例:设置密码为123456
requirepass 123456

设置Redis最大内存

一般推荐Redis设置内存为最大物理内存的四分之三,单位是字节(byte)
#这里设置为1GB
maxmemory 1073741824

添加防火墙规则

如果是从网上租的服务器,需要添加添加防火墙规则

这里以腾讯云服务器举例

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fCDcCL0R-1651752084988)(C:\Users\zq553379889\Desktop\Note\image-20220505195500880.png)]
在这里插入图片描述

防火墙开放端口

进入linux将6379防火墙放开,然后重新连接试试

#检查防火墙状态
firewall-cmd --state

#开启防火墙
systemctl restart firewalld.service

#开启6379端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent

#重新载入配置
firewall-cmd --reload

#查看已开放的端口
firewall-cmd --list-ports

后台启动redis

切换到redis-server所在的目录下,执行以下命令来启动redis

redis-server /usr/local/redis-6.2.7/redis.conf

这里的/usr/local/redis-6.2.7/redis.conf是我的redis配置文件路径,要改成自己的

测试连接

package com.zq.jedis;

import redis.clients.jedis.Jedis;

public class JedisDemo1 {
    public static void main(String[] args) {
        Jedis jedis=new Jedis("这里填你的服务器ip地址",6379);
        #密码,requirepass里面设置的,这里填自己的密码
        jedis.auth("123456");
        String ping = jedis.ping();
        System.err.println(ping);
    }
}

运行结果:
在这里插入图片描述

完整配置文件

#bind 127.0.0.1 -::1
protected-mode no
requirepass 123456
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 20
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
maxmemory 1073741824
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐