源码编译安装Redis

环境:

CentOS7.2

redis version:4.0.11

安装步骤:

# tar zxf redis-4.0.11.tar.gz

# cd redis-4.0.11

# make

# make install PREFIX=/usr/local/redis //指定安装目录

# mkdir /usr/local/redis/conf //建配置文件目录

# cp redis.conf /usr/local/redis/conf

# mkdir /usr/local/redis/log //建日志文件目录

# touch /usr/local/redis/log/redis_6379.log //建日志文件

# vi /usr/local/redis/conf/redis.conf

修改如下内容:

bind 0.0.0.0 //允许任何连接

daemonize yes

logfile /usr/local/redis/log/redis_6379.log  //指定日志文件

dir /usr/local/redis/bin //工作目录,备份文件也保存在此目录

requirepass <设置密码> //设置认证密码

#vi /etc/profile

export PATH=”$PATH:/usr/local/redis/bin” 

# 保存退出

# 让环境变量立即生效: 

source /etc/profile

配置启动脚本:

# 复制脚本文件到init.d目录下 

# cp /usr/local/src/redis-4.0.11/utils/redis_init_script /etc/init.d/redis

vi /etc/init.d/redis

修改如下内容:

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli

CONF=”/usr/local/redis/conf/redis.conf”

$CLIEXEC -a “password” -p $REDISPORT shutdown

#此处passowrd应与上述设置密码保持一致。

注:若修改了redis.conf文件中的bind地址之后,重启redis出现Could not connect to Redis at 127.0.0.1:6379: Connection refused错误,需要修改脚本(例如/etc/init.d/redis),找到如下字符串$CLIEXE -p $REDISPORT shutdown,修改为$CLIEXEC -h ip -a “password” -p $REDISPORT shutdown(即添加-h ip,ip应与上述bind地址保持一致)

# chmod +x /etc/init.d/redis

# chkconfig –add redis

# chkconfig –level 2345 redis on

# systemctl start redis #或者 /etc/init.d/redis start

# systemctl stop redis #或者 /etc/init.d/redis stop

附录1:日志中存在告警及解决

告警1:

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

解决:

修改/etc/sysctl.conf文件,增加一行:

net.core.somaxconn = 1024

然后执行命令:sysctl -p

告警2:

2865:M 01 Nov 17:36:36.055 # 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.

解决:

修改/etc/sysctl.conf文件,增加一行:

vm.overcommit_memory = 1

然后执行命令:sysctl -p

告警3:

2865:M 01 Nov 17:36:36.055 # 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.

解决:

Create following file:

vi /etc/systemd/system/disable-thp.service

and paste there following content:

[Unit]

Description=Disable Transparent Huge Pages (THP)

[Service]

Type=simple

ExecStart=/bin/sh -c “echo ‘never’ > /sys/kernel/mm/transparent_hugepage/enabled && echo ‘never’ > /sys/kernel/mm/transparent_hugepage/defrag”

[Install]

WantedBy=multi-user.target

Save the file and reload SystemD daemon:

systemctl daemon-reload

you can start the script and enable it on boot level:

systemctl start disable-thp

systemctl enable disable-thp

附录2:Redis 数据备份与恢复

备份数据:

127.0.0.1:6379> save

OK

该命令将在 redis 安装目录中创建dump.rdb文件。

另创建 redis 备份文件也可以使用命令 BGSAVE,该命令在后台执行。

127.0.0.1:6379> BGSAVE

Background saving started

恢复数据:

如果需要恢复数据,只需将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务即可。获取 redis 目录可以使用 CONFIG 命令,如下所示:

redis 127.0.0.1:6379> CONFIG GET dir

1) “dir”

2) “/usr/local/redis/bin”

以上命令 CONFIG GET dir 输出的 redis 安装目录为 /usr/local/redis/bin。

附录3:Redis 性能测试

Redis 性能测试是通过同时执行多个命令实现的。

redis-benchmark [option] [option value]

以下实例同时执行 10000 个请求来检测性能:

$ redis-benchmark -n 10000  -q

以下实例使用了多个参数来测试 redis 性能:

$ redis-benchmark -h 127.0.0.1 -p 6379 -t set,lpush -n 10000 -q

SET: 146198.83 requests per second

LPUSH: 145560.41 requests per second

以上实例中主机为 127.0.0.1,端口号为 6379,执行的命令为 set,lpush,请求数为 10000,通过 -q 参数让结果只显示每秒执行的请求数。

附录4:Redis 客户端连接

最大连接数:maxclients 的默认值是 10000,可以在 redis.conf 中对这个值进行修改。

config get maxclients

1) “maxclients”

2) “10000”