Redis
简介
Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库。
Redis 与其他 key - value 缓存产品有以下三个特点:
-
Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
-
Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
-
Redis支持数据的备份,即master-slave模式的数据备份。
安装
docker安装
简易配置
查看可用版本
[root@localhost ~]# docker search redis NAME DESCRIPTION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 10543 [OK] grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2 83 sameersbn/redis 83 [OK] rediscommander/redis-commander Alpine image for redis-commander - Redis man… 74 [OK] redislabs/redisearch Redis With the RedisSearch module pre-loaded… 50 ........... ...........
取最新镜像
[root@localhost ~]# docker pull redis:latest
运行
#1、启动容器 [root@localhost ~]# docker run -itd --name redis-gzga -p 6379:6379 redis:latest 6d94a2efb38cd9d32c21b991bbb75dec8a56942ba9f2464ace747f168a392427 #2、查看容器日志 [root@localhost redis]# docker logs -f redis-gzga 1:C 16 Feb 2022 06:10:14.825 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 16 Feb 2022 06:10:14.825 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 16 Feb 2022 06:10:14.825 # Configuration loaded 1:M 16 Feb 2022 06:10:14.827 * monotonic clock: POSIX clock_gettime 1:M 16 Feb 2022 06:10:14.829 # Warning: Could not create server TCP listening socket ::1:6379: bind: Cannot assign requested address 1:M 16 Feb 2022 06:10:14.830 * Running mode=standalone, port=6379. 1:M 16 Feb 2022 06:10:14.831 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1:M 16 Feb 2022 06:10:14.831 # Server initialized 1:M 16 Feb 2022 06:10:14.831 # 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. 1:M 16 Feb 2022 06:10:14.833 * Ready to accept connections
测试及设置密码
#1、测试是否能连接 [root@localhost ~]# docker exec -it redis-gzga /bin/bash root@6d94a2efb38c:/data# redis-cli 127.0.0.1:6379> ping PONG #2、修改密码 127.0.0.1:6379> config get requirepass 1) "requirepass" 2) "" 127.0.0.1:6379> config set requirepass 123456
详细配置
创建配置文件目录
# 这里我们在 /home/docker 下创建 mkdir /home/docker/redis/{conf,data} -p cd /home/docker/redis
修改配置文件redis.conf
# 获取 redis 的默认配置模版 # 这里主要是想设置下 redis 的 log / password / appendonly # redis 的 docker 运行参数提供了 --appendonly yes 但没 password # 网上下载redis免安装包,获取redis.conf文件 # 直接替换编辑 sed -i 's/logfile ""/logfile "access.log"/' conf/redis.conf sed -i 's/# requirepass foobared/requirepass 123456/' conf/redis.conf sed -i 's/appendonly no/appendonly yes/' conf/redis.conf sed -i 's/daemonize yes/daemonize no/' conf/redis.conf
常用配置属性
命令 | 功能 |
---|---|
appendonly yes | 启动Redis持久化功能 (默认 no , 所有信息都存储在内存 [重启丢失] 。 设置为 yes , 将存储在硬盘 [重启还在]) |
protected-mode no | 关闭protected-mode模式,此时外部网络可以直接访问 (docker默认自动开启了) |
bind 0.0.0.0 | 设置所有IP都可以访问 (docker默认自动开启了) |
requirepass 密码 | 设置密码 |
启动脚本
echo "停止容器" docker stop redis-gzga echo "移除容器" docker rm redis-gzga echo "部署并启动容器" docker run --name redis-gzga -d -p 6379:6379 -v /home/docker/redis/conf/redis.conf:/etc/redis/redis.conf -v /home/docker/redis/data:/data redis:latest redis-server /etc/redis/redis.conf --appendonly yes --restart=always #--appendonly yes 开启AOF #--restart=always 开机自启 #--requirepass 设置密码
源码安装
redis命令
连接命令
#本地执行 $ redis-cli #远程服务器执行 #语法 5.x $ redis-cli -h host -p port -a password #实例 C:\Users\dell>redis-cli -h 192.168.1.177 -p 6379 192.168.1.177:6379>auth 123456 #语法6.x auth命令需要输入用户及密码两个参数 C:\Users\dell>redis-cli -h 192.168.1.177 -p 6379 192.168.1.177:6379> auth default 123456
查看用户
192.168.1.177:6379> acl list #列出所有的acl规则 192.168.1.177:6379> acl list #列出所有的用户
Java使用Redis
-
配置redis.properties文件
-
引入jedis-2.9.0.jar、commons-pool2-2.4.2.jar
-
配置JedisFactory连接
-
编写RedisUtil工具类
注意事项
-
redis.conf配置文件里面的daemonize yes需改成no,否则无法启动
-
5.x的auth命令无需用户名,6.x的auth命令必须指定用户名 auth username password