redis从入门到放弃 - 单机版安装
linux 机器安装
下载解压及安装前的工作
# download
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
# Installation
tar xzf redis-5.0.3.tar.gz
# 建立了一个redis目录的软链接,这 样做是为了不把redis目录固定在指定版本上,有利于Redis未来版本升级, 算是安装软件的一种好习惯
ln -s redis-5.0.3 redis
cd redis
make
# 创建文件夹
mkdir /usr/local/redis/conf
mkdir /usr/local/redis/data
mkdir /usr/local/redis/logs
修改linux参数
# warning 1 > 提示修改 linux内核参数
# WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
echo 1024 >/proc/sys/net/core/somaxconn
# warn 2 > 提示如下
# 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.
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl vm.overcommit_memory=1
# warning 3
# WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled
echo never > /sys/kernel/mm/transparent_hugepage/enabled
# 云服务器要注意ip要写对,端口要开放
# 虚拟机要注意防火墙要关闭 systemctl stop firewalld.service
准备配置文件,并保存在创建的conf文件夹
不同的版本的配置文件略有不同,想了解不同版本配置文件的区别, 请移步.
以下配置文件参考自:redis.conf.
# 配置文件进行了精简,完整配置可自行和官方提供的完整conf文件进行对照。端口号自行对应修改
#后台启动的意思
daemonize yes
#端口号(如果同一台服务器上启动,注意要修改为不同的端口)
port 6379
# IP绑定,redis不建议对公网开放,直接绑定0.0.0.0没毛病
bind 0.0.0.0
# 这个文件会自动生成(如果同一台服务器上启动,注意要修改为不同的端口)
pidfile /var/run/redis_6379.pid
编译安装
# 编译
make
# 指定位置安装
make PREFIX=/usr/local/redis install
启动运行
- 默认配置
$ redis-server
12040:C 11 Jun 17:28:39.464 # Warning: no config file specified, using the
default config. In order to specify a config file use ./redis-server /path/ to/redis.conf
12040:M 11 Jun 17:28:39.470 # Server started, Redis version 5.0.3
12040:M 11 Jun 17:28:39.470 * The server is now ready to accept connections on
port 6379
- redis-server加上要修改配置名和值(可以是多对),没有设置的配置将
使用默认配置
redis-server --configKey1 configValue1 --configKey2 configValue2
- 配置文件启动
redis-server conf/redis.conf
redis可执行文件说明
可执行文件 | 说明 |
---|---|
redis-server | 启动Redis |
redis-cli | Redis命令行客户端 |
redis-benchmark | Redis基准测试工具 |
redis-check-rdb | Redis RDB持久化文件检测和修复工具 |
redis-check-aof | Redis AOF持久化文件检测和修复工具 |
redis-sentinel | 启动Redis sentinel |
docker安装
获取镜像
docker pull redis
本地创建配置文件
# 创建目录和文件
mkdir -p /opt/redis/config && touch /opt/redis/config/redis.conf
# 进入编辑模式
vi /opt/redis/config/redis.conf
# 写入以下配置文件
bind 127.0.0.1 #注释掉这部分,这是限制redis只能本地访问
protected-mode no #默认yes,开启保护模式,限制为本地访问
daemonize no #默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程(可选),改为yes会使配置文件方式启动redis失败
dir /opt/redis/data #输入本地redis数据库存放文件夹(可选)
appendonly yes #redis持久化(可选)
创建redis数据挂载目录
mkdir /opt/redis/data
启动redis
docker run -p 6379:6379 --name redis -v /opt/redis/config/redis.conf:/etc/redis/redis.conf -v /opt/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes
命令说明
- -p 6379:6379 端口映射:前表示主机部分,:后表示容器部分。
- –name myredis 指定该容器名称,查看和进行操作都比较方便。
- -v 挂载目录,规则与端口映射相同。
- -d redis 表示后台启动redis
- redis-server /etc/redis/redis.conf 以配置文件启动redis,加载容器内的conf文件,最终找到的是挂载的目录/usr/local/docker/redis.conf
- appendonly yes 开启redis 持久化