Linux--软件安装之Redis

  Redis是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,事务等。

下载/安装

  • 1、官网下载:

http://redis.io/download #第一种:官方下载后上传
wget http://download.redis.io/releases/redis-3.0.6.tar.gz #第二种:命令字节下载:(软件本身不大,看网速)

  • 2、解压编译源码安装
1
2
3
tar -zxvf redis-3.0.6.tar.gz -C /usr/local/src   #解压缩到指定目录
cd /usr/local/src/redis-3.0.1/   #到解压的目录去编译源码
make PREFIX=/usr/local/redis install   #安装到指定目录

:如果make失败,一般是你们系统中还未安装gcc,那么可以通过yum安装:yum install gcc

出现下面信息表示编译安装成功!

1
2
3
4
5
6
7
8
Hint: It's a good idea to run 'make test' ;)

INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/usr/local/src/redis-3.0.6/src'

将redis做成服务

1
2
3
4
5
6
7
8
9
####将redis_init_script复制到/etc/rc.d/init.d/同时改名为redis
cp /usr/local/src/redis-3.0.1/utils/redis_init_script /etc/rc.d/init.d/redis
vim /etc/rc.d/init.d/redis

####修改下面4行
> #chkconfig: 2345 80 90 ##注意:这个在上面蓝色字体第二行
> EXEC=/usr/local/redis/bin/redis-server  ##第七行
> CLIEXEC=/usr/local/redis/bin/redis-cli  ##第八行
> $EXEC $CONF &    ##第二十行

:后面的那个“&”,即是将服务转到后面运行的意思

修改后,文件如下

配置redis

1
2
3
4
5
###配置文件拷贝到/etc/redis/${REDISPORT}.conf 
mkdir /etc/redis
cp /usr/local/src/redis-3.0.6/redis.conf /etc/redis/6379.conf
###这样,redis服务脚本指定的CONF就存在了。
###默认情况下,Redis未启用认证,可以通过开启6379.conf的requirepass 指定一个验证密码。

注册redis服务:

1
chkconfig --add redis

然后将Redis的命令所在目录添加到系统参数PATH中

1
2
3
vi /etc/profile    #编辑环境变量
export PATH="$PATH:/usr/local/redis/bin" #添加环境变量(记得指定到自己的目录)
source /etc/profile   #重启配置

redis安装就完成了!

简单操作

启动redis

  • 通过我们注册的服务启动redis
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    [root@iZ94r8hgrjcZ home]# service redis start
    Starting Redis server...
    [root@iZ94r8hgrjcZ home]# _._
    _.-``__ ''-._
    _.-`` `. `_. ''-._ Redis 3.0.6 (00000000/0) 64 bit
    .-`` .-```. ```\/ _.,_ ''-._
    ( ' , .-` | `, ) Running in standalone mode
    |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
    | `-._ `._ / _.-' | PID: 4028

    `-._ `-._ `-./ _.-' _.-'
    |`-._`-._ `-.__.-' _.-'_.-'|
    | `-._`-._ _.-'_.-' | http://redis.io
    `-._ `-._`-.__.-'_.-' _.-'

    |`-._`-._ `-.__.-' _.-'_.-'|
    | `-._`-._ _.-'_.-' |
    `-._ `-._`-.__.-'_.-' _.-'

    `-._ `-.__.-' _.-'
    `-._ _.-'
    `-.__.-'


    4028:M 30 Apr 11:06:22.282 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    4028:M 30 Apr 11:06:22.282 # Server started, Redis version 3.0.6
    4028:M 30 Apr 11:06:22.283 # 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.
    4028:M 30 Apr 11:06:22.283 # 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.
    4028:M 30 Apr 11:06:22.283 * The server is now ready to accept connections on port 6379
    ^C
    [root@iZ94r8hgrjcZ home]# ps -ef|grep redis ##任然在后台运行
    root 4028 1 0 11:06 pts/0 00:00:00 /usr/local/redis/bin/redis-server *:6379
    root 4034 1079 0 11:06 pts/0 00:00:00 grep redis

客户端

1
2
3
4
##redis客户端
redis-cli [-h 127.0.0.1] [-p 6379]
##关闭
redis-cli shutdown
  • 实例代码:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    [root@localhost ~]# redis-cli    ##连接到服务端
    127.0.0.1:6379> set blog blog.xiaoxiaomo.com ##设置值
    OK
    127.0.0.1:6379> get blog ##获取值
    "blog.xiaoxiaomo.com"
    127.0.0.1:6379> shutdown ##关闭redis
    4227:M 27 Apr 04:29:56.025 # User requested shutdown...
    4227:M 27 Apr 04:29:56.025 * Saving the final RDB snapshot before exiting.
    4227:M 27 Apr 04:29:56.039 * DB saved on disk
    4227:M 27 Apr 04:29:56.039 # Redis is now ready to exit, bye bye...
    not connected> get blog
    Could not connect to Redis at 127.0.0.1:6379: Connection refused
    not connected>
    [1]+ Done redis-server
    [root@localhost ~]# ps -ef|grep redis ##已关闭
    root 4248 2765 0 04:30 pts/2 00:00:00 grep redis

    ##注:dump.rdb文件保存到了跟目录

附:另一种快速安装

  • 1、 解压redis然后cd到目录并安装

    1
    2
    3
    #tar -zxvf redis-3.0.6.tar.gz -C /usr/local/src/
    #cd /usr/local/src/redis-3.0.6/
    #make && install
  • 2、 修改配置文件redis.conf

    1
    2
    ##改为下面的参数,可以后台运行
    daemonize yes
  • 3、 让redis在后台运行

    1
    2
    #加上`&` 使redis以后台程序方式运行
    ./redis-server &
  • 4、通过加载配置的方式

    1
    2
    redis-server redis.conf路径 ##eg: redis-server ../redis.conf
    ##我们可以把配置文件cp到/etc/下面方便操作
  • 注意这两种启动方式:
    在那个目录启动,dump.rdb持久化文件就会在哪儿生成rdb文件,所以最好是在同一个目录下启动redis,以免数据丢失

  • redis持久化查看博客http://blog.xiaoxiaomo.com/2016/04/28/Redis-持久化方案及备份/

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器