Redis之环境安装
一、Redis简介
Redis是一个开放源代码(BSD许可)的内存中数据结构存储,用作数据库,缓存和消息代理。它支持数据结构,例如字符串,哈希,列表,集合,带范围查询的排序集合,位图,超日志,带有半径查询和流的地理空间索引。Redis具有内置的复制,Lua脚本,LRU逐出,事务和不同级别的磁盘持久性,并通过Redis Sentinel和Redis Cluster自动分区提供了高可用性。
二、Centos下安装Redis
2.1. 基础安装
# 下载Redis版本
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
# 解压并创建安装目录
tar -zxvf redis-5.0.5.tar.gz && mkdir redis-service
# 进入redis解压后的目录进行编译安装 *prefix需要大写*
make install PREFIX=/opt/redis-service
# 将解压保重的配置文件复制到安装目录
cp redis.conf /opt/redis-service/
# 启动
cd /opt/redis-service/bin && redis-server
2.2. 相关配置
# 69行, 将ip监控设置为 0.0.0.0
bind 0.0.0.0
# 88行 设置允许外界访问
protected-mode no
# 136行 yes 将用守护线程运行,即后台运行
daemonize yes
# 172行 设置日志文件
logfile /opt/redis-service/redis.log
# 508行 设置密码
requirepass 123456
# 700行, 设置AOF持久化, 将no改为yes
appendonly yes
2.3. 重新启动redis
# 需要进入安装目录的bin目录
bin/redis-server redis.conf
# 查看是否执行成功
ps -ef | grep -v grep | grep redis
root 65864 1 0 13:08 ? 00:00:00 bin/redis-server 0.0.0.0:6379
2.4. 使用Redis
root@long: bin/redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379> exit
2.5. 将redis-cli添加到环境变量
# 进入/etc/profile 这是文件是对所有用户都生效的
vim /etc/profile
# 添加
export PATH=/opt/redis-service/bin:$PATH
# 刷新环境变量让其生效
source /etc/profile
2.6. 设置redis为服务
# 进入systemctl目录
cd /lib/systemd/system
# 添加redis.service服务文件
vim redis.service
文件内容
[Unit]
Description=redis
After=network.target
[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/opt/redis-service/bin/redis-server /opt/redis-service/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
使用服务启动redis
# 重新刷新systemctl
systemctl daemon-reload
# 启动redis
systemctl start redis
# 关闭redis
systemctl stop redis
# 设置开机自启
systemctl enable redis.service
# 关闭开机自启
systemctl disable redis.service
2.7. 卸载redis
删除安装目录
删除所有redis相关命令脚本
删除redis解压文件夹
2.8. 如果无法访问,关闭防火墙
root@long:/opt/redis-service# systemctl status firewalld # 查看防火墙状态 running 说明运行
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-11-25 14:45:23 UTC; 10h ago
Docs: man:firewalld(1)
Main PID: 876 (firewalld)
Tasks: 2 (limit: 2276)
Memory: 25.2M
CGroup: /system.slice/firewalld.service
└─876 /usr/bin/python3 /usr/sbin/firewalld --nofork --nopid
root@long:/opt/redis-service# systemctl stop firewalld # 关闭防火墙 就可以访问了
root@long:/opt/redis-service# systemctl startfirewalld # 启动防火墙
root@long:/opt/redis-service# systemctl status firewalld # 再次查看防火墙 状态为dead 死亡状态
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2019-11-26 01:28:46 UTC; 1s ago
Docs: man:firewalld(1)
Process: 876 ExecStart=/usr/sbin/firewalld --nofork --nopid (code=exited, status=0/SUCCESS)
Main PID: 876 (code=exited, status=0/SUCCESS)
三、Docker下安装Redis
创建docker-compose容器目录,并创建yaml文件
version: '3'
services:
redis:
restart: always
image: redis:latest
container_name: redis-service
ports:
- "6379:6379"
command: redis-server --port 6379 --requirepass 123456 --appendonly yes
volumes:
- /home/docker-service/redis-service/data:/data
networks:
- esnet
networks:
esnet:
driver: bridge
将redis数据持久化文件挂载到本地/home/docker-service/redis-service/data
port 6379 设置端口
requirepass 123456 设置密码
appendonly yes 设置持久化为AOF
运行docker-compose
docker-compose up
docker-compose up -d # 后台运行
docker-compose down # 移除容器
docker-compose logs -f redis-compose # 查看redis容器运行日志
运行连接: