Redis 简介

  • Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。
  • Redis 与其他 key - value 缓存产品有以下三个特点:
  • Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。
  • Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
  • Redis支持数据的备份,即master-slave模式的数据备份。


Redis 优势

  • 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。
  • 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
  • 原子 – Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。
  • 丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。


Redis与其他key-value存储有什么不同?

Redis有着更为复杂的数据结构并且提供对他们的原子性操作,这是一个不同于其他数据库的进化路径。Redis的数据类型都是基于基本数据结构的同时对程序员透明,无需进行额外的抽象。

Redis运行在内存中但是可以持久化到磁盘,所以在对不同数据集进行高速读写时需要权衡内存,应为数据量不能大于硬件内存。在内存数据库方面的另一个优点是, 相比在磁盘上相同的复杂的数据结构,在内存中操作起来非常简单,这样Redis可以做很多内部复杂性很强的事情。 同时,在磁盘格式方面他们是紧凑的以追加的方式产生的,因为他们并不需要进行随机访问。

准备环境

  • 升级所有的包,防止出现版本过旧不兼容问题
yum -y update
  • 安裝 GCC 编译工具 不然会有编译不过的问题
yum install -y gcc g++ gcc-c++ make
  • 关闭防火墙 节点之前需要开放指定端口,为了方便,生产不要禁用
# centos 6.x
service iptables stop

# centos 7.x
systemctl stop firewalld.service


一、安装 Redis

1、下载,解压,编译安装

cd /data/packages/
wget http://download.redis.io/releases/redis-6.2.6.tar.gz
tar zxvf redis-6.2.6.tar.gz -C /data/apps/
cd /data/apps/redis-6.2.6/
make #如果提示有错误,编译失败,那么先执行 make distclean ,然后再重新执行 make
make PREFIX=/usr/local/redis install

安装完成后,在redis安装目录下会出现一个bin目录,bin目录下有几个可执行文件

redis-benchmark            ---性能测试工具
redis-check-aof ---AOF文件修复工具
redis-check-rdb ---RDB文件检测工具(快照持久化文件)
redis-cli ---命令行客户端
redis-sentinel -> redis-server ---redis哨兵
redis-server ---redis服务器启动命令


2、配置环境变量

grep 'REDIS_HOME' /etc/profile >> /dev/null
if [ $? -ne 0 ];then
cat << EOF >>/etc/profile
export REDIS_HOME=/usr/local/redis
export PATH=$REDIS_HOME/bin:$PATH
EOF
else
echo '存在!'
fi

source /etc/profile


二、创建配置

默认的 ​​redis.conf​​ 配置文件内容太多,很多配置,不太好,用的时候再往,自己定义的配置文件加

1、安装服务脚本,生成redis配置文件(默认回车即可)

[root@qsh redis-6.2.6]# cd /data/apps/redis-6.2.6/utils/
[root@qsh 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] /data/logs/redis/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/redis/bin/redis-server
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /data/logs/redis/redis_6379.log
Data dir : /data/redis/6379
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/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!

进行到./install_server.sh时报错:

Welcome to the redis service installer
This script will help you easily set up a running redis server

This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!

解决方法:

vi ./install_server.sh

注释下面的代码即可

#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi

然后重新运行 ​​./install_server.sh​​即可。


2、自定义配置文件

cat /dev/null > /etc/redis/6379.conf 
vi /etc/redis/6379.conf

//把如下 Redis.conf 配置 内容粘贴进去

################################# NETWORK #####################################
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300

################################# GENERAL #####################################
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile redis.log
databases 16

################################## SECURITY ###################################

requirepass abc123 #设置密码
# rename-command CONFIG ""

############################## APPEND ONLY MODE ###############################
appendonly yes

# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"

# appendfsync always
appendfsync everysec
# appendfsync no

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

################################RDB-SNAPSHOTTING ################################

#save 900 1 # 900秒内有至少1个键被更改则进行快照(RDB)
#save 300 10 # 300秒内有至少10个键被更改则进行快照
#save 60 10000 # 60秒内有至少10000个键被更改则进行快照
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb

dir /data/redis/6379/ # 修改dump文件目录

################################### LIMITS ####################################
# maxclients 10000
maxmemory 2gb
maxmemory-policy noeviction
# maxmemory-samples 5

################################ LUA SCRIPTING ###############################
lua-time-limit 5000
################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128
################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0
############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
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-rewrite-incremental-fsync yes

redis配置信息:

1、daemonize 如果需要在后台运行,把该项改为yes

2、pidfile 配置多个pid的地址 默认在/var/run/redis.pid

3、bind 绑定ip,设置后只接受来自该ip的请求

4、port 监听端口,默认是6379

5、loglevel 分为4个等级:debug verbose notice warning

6、logfile 用于配置log文件地址

7、databases 设置数据库个数,默认使用的数据库为0

8、save 设置redis进行数据库镜像的频率。

9、rdbcompression 在进行镜像备份时,是否进行压缩

10、dbfilename 镜像备份文件的文件名

11、Dir 数据库镜像备份的文件放置路径

12、Slaveof 设置数据库为其他数据库的从数据库

13、Masterauth 主数据库连接需要的密码验证

14、Requriepass 设置 登陆时需要使用密码

15、Maxclients 限制同时使用的客户数量

16、Maxmemory 设置redis能够使用的最大内存

17、Appendonly  开启AOF模式,将数据保存到磁盘上。

18、Appendfsync 设置对appendonly.aof文件同步的频率(对数据进行备份的第二种方式)

19、vm-enabled 是否开启虚拟内存支持 (vm开头的参数都是配置虚拟内存的)

20、vm-swap-file 设置虚拟内存的交换文件路径

21、vm-max-memory 设置redis使用的最大物理内存大小

22、vm-page-size 设置虚拟内存的页大小

23、vm-pages 设置交换文件的总的page数量

24、vm-max-threads 设置VM IO同时使用的线程数量

25、Glueoutputbuf 把小的输出缓存存放在一起

26、hash-max-zipmap-entries 设置hash的临界值

27、Activerehashing 重新hash


二、启动服务

1、关闭默认端口

killall redis-server

2、启动服务

/etc/init.d/redis_6379 start
##
service redis_6379 start
chkconfig --level 2345 redis_6379 on

2、检查服务

ps -ef | grep redis   #查看是否启动成功
netstat -tnlp | grep redis #可以看到redis监听端口


三、基本命令

停止

/etc/init.d/redis_6379 stop
##
service redis_6379 stop

重启

/etc/init.d/redis_6379 restart
##
service redis_6379 restart

问题

  • 关闭服务失败:
# /etc/init.d/redis_6379 restart
Stopping ...
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
  • 解决方法:(设置密码的原因)
sed -i 's#^CLIEXEC=.*$#CLIEXEC="/usr/local/redis/bin/redis-cli -a abc123"#g' /etc/rc.d/init.d/redis_6379

注:abc123为 redis.conf 配置中requirepass 密码字段。请根据实际情况调整


测试 Redis

启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了

比如:

[root@qsh redis-4.0.6]# redis-cli -h 127.0.0.1 -p 6379 -a abc123
172.17.17.19:6379> set 123 abc
OK
172.17.17.19:6379> get 123
"abc"
172.17.17.19:6379> exit