1、配置redis的安装环境
- 配置安装环境
# 安装依赖包
$ yum -y install cpp binutils glibc-kernheaders glibc-common glibc-devel gcc make
# 修改内核参数
$ echo -e 'net.core.somaxconn = 1024\nvm.overcommit_memory = 1' >> /etc/sysctl.conf
$ sysctl -p
# 开启transparent_hugepage
$ echo never > /sys/kernel/mm/transparent_hugepage/enabled
# 添加到rc.local文件
$ echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
- 配置iptables防火墙
$ iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
$ iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 26379 -j ACCEPT
$ service iptables save
- 获取软件包
Redis 版本号采用标准惯例:主版本号.副版本号.补丁级别,一个副版本号就标记为一个标准发行版本,奇数的副版本号用来表示非标准版本,到官网下载需要的版本号。
$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz
2、编译安装redis程序
- 文件解压
$ tar -zxvf redis-3.2.8.tar.gz
$ cd redis-3.2.8
- 编译安装
# 编译
$ make
# 安装(安装完毕,常用工具会自动拷贝到/user/loca/bin目录下)
$ make install
- redis初始化配置
源码包中有个install_server.sh脚本,执行脚步根据提示输入即可完成初始化配置,脚本执行后程序已经启动。
$ ./utils/install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] /data/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /data/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
- 检查程序启动状况
$ ps -ef |grep redis
root 26989 1 0 19:06 ? 00:00:10 /usr/local/bin/redis-server 127.0.0.1:6379
- 生产环境移除无用软件包和解压包
# 将安装软件包移到备份目录
$ mv redis-3.2.8.tar.gz ../softback
# 删除用过的解压目录
$ rm -rf redis-3.2.8
3、修改配置
- 配置文件修改成如下配置
$ vi /etc/redis/6379.conf
bind 0.0.0.0
port 6379
databases 16
daemonize yes
requirepass "passwd@123"
protected-mode no
tcp-backlog 511
timeout 300
tcp-keepalive 300
supervised no
loglevel notice
logfile /var/log/redis_6379.log
pidfile /var/run/redis_6379.pid
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
# AOF持久化配置
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
# AOF重写配置
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-rewrite-incremental-fsync yes
aof-load-truncated yes
# RDB持久化配置
dir /data/redis/6379
dbfilename dump.rdb
rdbcompression yes
rdbchecksum yes
stop-writes-on-bgsave-error yes
save 900 1
save 300 10
save 60 10000
# 主从配置
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
由于我们要通过RDB文件迁移数据进来,因此上述配置没有使用AOF持久化。
redis程序配置AOF持久化后,在启动redis程序的时候,程序会优先从AOF的appendfile文件还原数据库,没有配置AOF持久化时才从RDB文件还原数据库。由于这一特性,配置文件是否启用AOF持久化还需要考虑是否需要进行数据迁移,如果不需要将之前的旧数据迁移进来,建议直接启用;如果需要将之前的旧数据迁移进来,根据迁移那种数据文件(AOF文件或RDB文件)类型来决定是否启用AOF持久化。如果迁移的是RDB文件,启用了AOF持久化,会出现什么现象?如果运行的redis程序没有启用AOF持久化,我们的DBA看到后,修改了配置文件,开启了AOF持久化,然后重启了redis程序,又会出现什么情况呢?这个问题值得我们考虑。
- 重启程序
$ /etc/init.d/redis_6379 restart