[一]linux下redis安装

Posted zibb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[一]linux下redis安装相关的知识,希望对你有一定的参考价值。

一.redis是什么

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)

更多的介绍就不copy了,直接看官网https://redis.io/topics/introduction

二.yum安装redis

1.安装前环境说明,系统基于CentOS-6.7-x86_64-minimal.iso

2.安装epel源

[[email protected] ~]# rpm -ivh http://mirrors.ustc.edu.cn/centos/6/extras/x86_64/Packages/epel-release-6-8.noarch.rpm

3.安装redis,目前版本为redis-2.4.10,比较低

[[email protected] ~]# yum install -y reids

4.启动redis

[[email protected] ~]# /etc/init.d/redis start
Starting redis-server:                                     [  OK  ]

查看启动日志

[[email protected] ~]# cat /var/log/redis/redis.log 

[1425] 07 Dec 17:41:12 * Server started, Redis version 2.4.10
[1425] 07 Dec 17:41:12 # 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.
[1425] 07 Dec 17:41:12 * DB loaded from disk: 0 seconds
[1425] 07 Dec 17:41:12 * The server is now ready to accept connections on port 6379

修复报错信息

[[email protected] ~]# echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf 
[[email protected] ~]# sysctl vm.overcommit_memory=1
vm.overcommit_memory = 1

#重启服务
[[email protected] ~]# /etc/init.d/redis restart
Stopping redis-server:                                     [  OK  ]
Starting redis-server:                                     [  OK  ]

#再次查看日志,已经没有报错了,第一次的日志没有提供出来
[[email protected] ~]# cat /var/log/redis/redis.log 
[1425] 07 Dec 17:45:33 # Received SIGTERM, scheduling shutdown...
[1425] 07 Dec 17:45:33 # User requested shutdown...
[1425] 07 Dec 17:45:33 * Saving the final RDB snapshot before exiting.
[1425] 07 Dec 17:45:33 * DB saved on disk
[1425] 07 Dec 17:45:33 * Removing the pid file.
[1425] 07 Dec 17:45:33 # Redis is now ready to exit, bye bye...
[1446] 07 Dec 17:45:33 * Server started, Redis version 2.4.10
[1446] 07 Dec 17:45:33 * DB loaded from disk: 0 seconds
[1446] 07 Dec 17:45:33 * The server is now ready to accept connections on port 6379

5.redis一些基本的配置

[[email protected] ~]# grep -vE "^$|#" /etc/redis.conf
daemonize yes                    #以deamon形式运行在后台
pidfile /var/run/redis/redis.pid
port 6379                        #监听端口
bind 127.0.0.1                   #绑定地址
timeout 0
loglevel notice
logfile /var/log/redis/redis.log #redis日志
databases 16                     #创建的数据库数据
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb              #数据文件名
dir /var/lib/redis/              #数据目录

6.客户端连接数据测试

[[email protected] ~]# redis-cli 
redis 127.0.0.1:6379> set name jack
OK
redis 127.0.0.1:6379> get name
"jack"
redis 127.0.0.1:6379> exit

三.源码安装redis

0.卸载前文yum安装的包

[[email protected] ~]# /etc/init.d/redis stop
Stopping redis-server:                                     [  OK  ]
[[email protected] ~]# yum remove -y redis

1.下载源码包,版本选择访问http://download.redis.io/releases

[[email protected] ~]# cd /usr/local/src
[[email protected] src]# wget http://download.redis.io/releases/redis-3.2.4.tar.gz

2.安装redis

# 详细安装内容可以参考readme
[[email protected] src]# cd /usr/local/src/
[[email protected] src]# tar zxvf redis-3.2.4.tar.gz
[[email protected] src]# cd redis-3.2.4
[[email protected] redis-3.2.4]# more README.md

#minimal系统缺少组件gcc
[[email protected] ~]# yum install -y gcc
#安装到自定义路径,方便卸载 [[email protected] redis
-3.2.4]# make PREFIX=/usr/local/redis install #有个报错无法还原了,大概是关于Selecting a non-default memory allocator when building Redis is done by setting the `MALLOC` environment variable #需要在make时添加make MALLOC=libc

#拷贝配置文件
[[email protected] redis-3.2.4]# mkdir /etc/redis
[[email protected] redis-3.2.4]# cp redis.conf /etc/redis/6379.conf
[[email protected] redis-3.2.4]# vim /etc/redis/6379.conf
daemonize yes                        #no改成yes
logfile "/var/log/redis_6379.log"  #日志文件
dir /var/lib/redis                   #数据目录
requirepass mima                     #开启密码认证

#服务启动脚本
[[email protected] redis-3.2.4]# cp /usr/local/src/redis-3.2.4/utils/redis_init_script /etc/init.d/redis
[[email protected] redis-3.2.4]# chmod +x /etc/init.d/redis
[[email protected] redis-3.2.4]# vim /etc/init.d/redis
  7 EXEC=/usr/local/redis/bin/redis-server
  8 CLIEXEC=/usr/local/redis/bin/redis-cli
 30                 $CLIEXEC -p $REDISPORT -a mima shutdown

3.启动服务,并修复报错

[[email protected] ~]# /etc/init.d/redis start
[[email protected] ~]# cat /var/log/redis_6379.log 
1613:M 07 Dec 18:25:47.132 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ‘‘-._                                             
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._                                   
 (          ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|` _.-|     Port: 6379
 |    `-._   `._    /     _.-    |     PID: 1613
  `-._    `-._  `-./  _.-    _.-                                   
 |`-._`-._    `-.__.-    _.-_.-|                                  
 |    `-._`-._        _.-_.-    |           http://redis.io        
  `-._    `-._`-.__.-_.-    _.-                                   
 |`-._`-._    `-.__.-    _.-_.-|                                  
 |    `-._`-._        _.-_.-    |                                  
  `-._    `-._`-.__.-_.-    _.-                                   
      `-._    `-.__.-    _.-                                       
          `-._        _.-                                           
              `-.__.-                                               

1613:M 07 Dec 18:25:47.134 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1613:M 07 Dec 18:25:47.134 # Server started, Redis version 3.2.4
1613:M 07 Dec 18:25:47.134 # 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.
1613:M 07 Dec 18:25:47.134 * DB loaded from disk: 0.000 seconds
1613:M 07 Dec 18:25:47.134 * The server is now ready to accept connections on port 6379

#修复报错,yum安装方式中已修复一个To fix this issue add ‘vm.overcommit_memory = 1‘ to /etc/sysctl.conf
[[email protected] ~]# echo 512 > /proc/sys/net/core/somaxconn
[[email protected] ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[[email protected] ~]# echo "echo 512 >  /proc/sys/net/core/somaxconn" >> /etc/rc.local
[[email protected]alhost ~]# echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local

4.重启redis测试读写

[[email protected] ~]# /etc/init.d/redis restart
Please use start or stop as first argument
[[email protected] ~]# /etc/init.d/redis stop
Stopping ...
Redis stopped
[[email protected] ~]# /etc/init.d/redis start
Starting Redis server...

#读写测试 [[email protected]
~]# /usr/local/redis/bin/redis-cli 127.0.0.1:6379> auth mima OK 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set name jack OK 127.0.0.1:6379> get name "jack" 127.0.0.1:6379> exit

 






























以上是关于[一]linux下redis安装的主要内容,如果未能解决你的问题,请参考以下文章

linux下安装与配置Redis

Linux下安装Redis

Linux下安装redis

在linux环境下安装redis并且搭建自己的redis集群

linux下redis的安装启动关闭和卸载

Linux下Redis服务器搭建