Redis简介

Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库,它可以用作数据库、缓存和消息中间件。

Redis 与其他 key-value 缓存产品有以下三个特点:

  • Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。

  • Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。

  • Redis支持数据的备份,即master-slave模式的数据备份。

Redis安装

本次安装redis系统环境是:CentOS 7.4,redis版本是4.0.14,进行介绍。

[root@localhost tmp]# yum install gcc-c++ tcl
[root@localhost tmp]# wget http://download.redis.io/releases/redis-4.0.14.tar.gz
[root@localhost tmp]# tar -zxvf redis-4.0.14.tar.gz
[root@localhost tmp]# cd ./redis-4.0.14
[root@localhost redis-4.0.14]# make

make完成之后,出现以下提示表示已经编辑完成。
image.png

Redis部署

将redis文件中的配置文件、常用命令、数据文件及日志存放到同一目录中。

1、创建redis目录

[root@localhost redis-4.0.14]# mkdir -p /data/redis/bin   //存放redis命令文件
[root@localhost redis-4.0.14]# mkdir -p /data/redis/conf  //存放redis配置文件
[root@localhost redis-4.0.14]# mkdir -p /data/redis/data  //存放redis数据文件
[root@localhost redis-4.0.14]# mkdir -p /data/redis/logs  //存放redis日志文件

2、redis命令与配置文件

[root@localhost redis-4.0.14]# cd ./src/
[root@localhost src]# mv redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server /data/redis/bin/
[root@localhost src]# cd ..
[root@localhost redis-4.0.14]# cp redis.conf /data/redis/conf/

编辑redis配置文件,修改logs与data路径,并启用与配置redis访问密码。

[root@localhost redis-4.0.14]# vi /data/redis/conf/redis.conf //修改redis.conf内容如下:
       logfile "../logs/redis.log"   //配置logs路径
       dir ../data/    //配置data路径
       requirepass password    //取消注释#,并设置redis访问密码。

3、启动与关闭redis服务命令

1)、启动redis命令:

[root@localhost bin] /data/redis/bin/redis-server /data/redis/conf/redis.conf

注意:默认redis服务是在前台运行,关闭当前会话redis服务也将关闭,如果使用supervisor守护进程时,需要redis服务在前台运行,如果想改为后台运行,
需要修改redis.conf配置文件,将daemonize属性改为yes(即表示在后台运行)。

2)、关闭redis命令:

方式一: kill -9 端口号
方式二: pkill redis-server

注:查看redis的运行情况和端口号:

[root@localhost bin]# ps -ef | grep redis
root      6635  5888  0 11:19 pts/1    00:00:00 vi redis.log
root      6636  2678  0 11:20 pts/0    00:00:00 ./redis-server 127.0.0.1:6379
root      6652  6518  0 11:31 pts/2    00:00:00 grep --color=auto redis

3)、查看redis的版本命令:redis-server --version 或 redis-server -v

[root@localhost bin]# redis-server --version
Redis server v=4.0.14 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=9ea79324388746c3

4)、redis启动时警告信息

[root@iz2ze9ujl2isnlkqx0qff7z logs]# tail -f redis.log 
23276:M 15 Nov 10:28:32.267 # Server initialized
23276:M 15 Nov 10:28:32.267 # 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.
23276:M 15 Nov 10:28:32.267 # 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.
23276:M 15 Nov 10:28:32.267 * Ready to accept connections
23276:signal-handler (1573784941) Received SIGINT scheduling shutdown...
23276:M 15 Nov 10:29:01.515 # User requested shutdown...
23276:M 15 Nov 10:29:01.515 * Saving the final RDB snapshot before exiting.
23276:M 15 Nov 10:29:01.518 * DB saved on disk
23276:M 15 Nov 10:29:01.518 * Removing the pid file.
23276:M 15 Nov 10:29:01.518 # Redis is now ready to exit, bye bye...

以上两个警告信息解决方法:

警告一: 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.

overcommit_memory 它是内存分配策略的参数,可选值:0、1、2,其中分别表示:
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存。

解决方法:

[root@iz2ze9ujl2isnlkqx0qff7z /]# vi /etc/sysctl.conf  //编辑systctl.conf文件,并添加vm.overcommit_memory = 1,然后执行 sysctl -p 使配置文件生效。

警告二: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.

意思是:你使用的是透明大页,可能导致redis延迟和内存使用问题。执行echo never > /sys/kernel/mm/transparent_hugepage/enabled修复该问题。

解决方法:

[root@iz2ze9ujl2isnlkqx0qff7z /]#vi /etc/rc.local //将以下内容写入rc.local文件中,重启机器生效。

iftest -f /sys/kernel/mm/redhat_transparent_hugepage/enabled; then

echo never > /sys/kernel/mm/redhat_transparent_hugepage/enabled

fi
上一篇 下一篇