深入学习Redis:CentOS 7 安装Redis 4.0.11以及生产环境启动配置

标签: Redis   Linux  

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的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。
  • 丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。

Redis安装

本文以opt目录作为安装目录进行安装,详细安装过程如下:

1)准备工作

// 安装`gcc`编译工具
[root@localhost ~]# yum install gcc
[root@localhost ~]# yum install 

2)下载Redis 4.0.11

[root@localhost ~]# cd /opt
[root@localhost opt]# wget http://download.redis.io/releases/redis-4.0.11.tar.gz

3)解压redis-4.0.11.tar.gz

[root@localhost opt]# tar -zxvf redis-4.0.11.tar.gz

4)编译

[root@localhost opt]# cd redis-4.0.11/
[root@localhost redis-4.0.11]# make

进行编译时,有可能会报如下错误:

......
......
In file included from adlist.c:34:0:
zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
 #include <jemalloc/jemalloc.h>
                               ^
编译中断。
make[1]: *** [adlist.o] 错误 1
make[1]: 离开目录“/opt/redis-4.0.1/src”
make: *** [all] 错误 2

想要弄明白这个错误是怎么回事,我们得先看一下Redis的描述文档。打开Reids根目录下的README文件,找到这么一段话:

[root@localhost redis-4.0.11]# cat README.md
......
......
Allocator
---------
Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.
To force compiling against libc malloc, use:
    % make MALLOC=libc
To compile against jemalloc on Mac OS X systems, use:
    % make MALLOC=jemalloc
......
......

这是Redis在安装时关于内存分配器allocator的描述,如果指定了MALLOC这个环境变量,那么会用这个环境变量的去建立Redis。如果没有,那么就是用默认的分配器。Redis 2.4版本之后,默认使用jemalloc来做内存管理,因为jemalloc被证明解决fragmentation problems(内存碎片化问题)比libc更好。但是如果你又没有jemalloc而只有libc,当make出错时,你可以加这么一个参数即可 make MALLOC=libc。

如果我们就是想用jemalloc,那么安装jemalloc即可,该过程很简单,一般不会报错:

[root@localhost opt]# wget https://github.com/jemalloc/jemalloc/releases/download/5.0.1/jemalloc-5.0.1.tar.bz2
[root@localhost opt]# yum instll bzip2
[root@localhost opt]# bzip2 -d jemalloc-5.0.1.tar.bz2
[root@localhost opt]# tar -xvf jemalloc-5.0.1.tar
[root@localhost opt]# cd jemalloc-5.0.1/
[root@localhost jemalloc-5.0.1]# ./configure --prefix=/usr/local/jemalloc
[root@localhost jemalloc-5.0.1]# make && make install

jemalloc安装完成后,可查看安装结果:

[root@localhost jemalloc-5.0.1]# ll /usr/local/jemalloc/
总用量 0
drwxr-xr-x. 2 root root  62 8月  13 10:58 bin
drwxr-xr-x. 3 root root  22 8月  13 10:58 include
drwxr-xr-x. 3 root root 115 8月  13 10:58 lib
drwxr-xr-x. 4 root root  28 8月  13 10:58 share

解决完上边两个问题,再次对 Redis 进行编译,就不会报错了,因为我们打算采用jemalloc管理内存,所以编译时我们需要指定内存管理器:

[root@localhost jemalloc-5.0.1]# cd ../redis-4.0.11/
[root@localhost redis-4.0.11]# make MALLOC=/usr/local/jemalloc/lib
......
......
Hint: It's a good idea to run 'make test' ;)
make[1]: 离开目录“/opt/redis-4.0.11/src”

5)安装

[root@localhost redis-4.0.11]# cd src 
[root@localhost src]# make install
Hint: It's a good idea to run 'make test' ;)
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install

6)测试

安装完成后,我们一般还要对安装结果进行测试,测试的方式很简单,在安装结果中已经说明了,执行 make test 即可。不过在进行测试的过程中,我遇到了一个错误:

[root@localhost src]# make test
You need tcl 8.5 or newer in order to run the Redis test
make: *** [test] 错误 1

这是由于tcl的版本低于8.5导致的,安装最新的tcl即可:

[root@localhost opt]# wget https://sourceforge.net/projects/tcl/files/Tcl/8.6.7/tcl8.6.7-src.tar.gz
[root@localhost opt]# tar -zxvf tcl8.6.7-src.tar.gz  -C /usr/local/
[root@localhost opt]# cd /usr/local/tcl8.6.7/unix
[root@localhost unix]# ./configure
[root@localhost unix]# make
[root@localhost unix]# make install

tcl安装完成后,再次进行测试,完美通过:

[root@localhost redis-4.0.11]# make test
......
......
\o/ All tests passed without errors!
Cleanup: may take some time... OK
make[1]: 离开目录“/opt/redis-4.0.11/src”

7)查看安装版本

[root@localhost redis-4.0.1]# redis-server -v
Redis server v=4.0.11 sha=00000000:0 malloc=libc bits=64 build=3e25d8012744402f

到现在,单机Redis就算安装完成了。

Redis生产环境启动方案

1)准备目录

[root@localhost redis-4.0.11]# mkdir /etc/redis
[root@localhost redis-4.0.11]# mkdir -p /var/log/redis/
[root@localhost redis-4.0.11]# mkdir mkdir -p /var/redis/6379/

2)配置redis文件

在redis根目录,拷贝redis.conf文件到/etc/redis,并重命名

[root@localhost redis-4.0.11]# cp redis.conf /etc/redis/
[root@localhost redis-4.0.11]# cd /etc/redis/
[root@localhost redis]# mv redis.conf 6379.conf 

修改6379.conf部分配置

[root@localhost]# vi /etc/redis/6379.conf

修改如下:
daemonize    yes                         // 让redis以daemon进程运行
pidfile      /var/run/redis_6379.pid     // 设置redis的pid文件位置
port         6379                        // 设置redis的监听端口号
dir          /var/redis/6379             // 设置持久化文件的存储位置
logfle       /var/log/redis/6379.log     // 设置日志目录
bind         机器的ip地址                  // 修改绑定的ip地址
appendonly   yes                         // 开启AOF持久化 

如果要求安全性的话,可以配置:

requirepass redis_password
masterauth redis_password

3)开机自启动

redis-4.0.11/utils目录下,拷贝redis_init_script/etc/init.d并重命名

[root@localhost]# cd /opt/redis-4.0.11/utils
[root@localhost utils]# cp redis_init_script /etc/init.d/
[root@localhost utils]# cd /etc/init.d/
[root@localhost init.d]# mv redis_init_script redis_6379
[root@localhost init.d]# chmod 777 redis_6379

让redis跟随系统启动而自启动

redis_6379脚本中,最上面,加入两行注释

# chkconfig:   2345 90 10

# description:  Redis is a persistent key-value database

使用命令

[root@localhost init.d]# chkconfig redis_6379 on
[root@localhost init.d]# chkconfig --list

netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
redis_6379      0:off   1:off   2:on    3:on    4:on    5:on    6:off

最后启动redis

[root@localhost init.d]# ./redis_6379 start
Starting Redis server...

4)使用redis-cli连接客户端

redis-cli -h 127.0.0.1 -p 6379

注意-h要填写你在6379.confbind参数配置的值。

这样,就大功告成啦!

「真诚赞赏,手留余香」

请我喝杯咖啡?

使用微信扫描二维码完成支付

相关文章