Linux 下如何安装 Redis?
Redis是一款高性能的key-value数据库,本文主要记录如何在Linux系统上进行安装,以及为Python开发安装对应的redis模块。
实验环境
Linux版本plainUbuntu 18.04 LTS && NeoKylin 3.2Python版本plainPython 2.7.15rc1 && Python 2.6.6Redis版本plainredis-5.0.4redis-py版本plainredis-3.2.1 && redis-2.10.6
下载
下载地址:http://redis.io/download,下载最新稳定版本源码。
本文使用的版本为 redis-5.0.4。
安装
Ubuntu
解压缩
首先要解压
Redis压缩包。进入压缩包下载的路径,执行:shelltar xzf redis-5.0.4.tar.gz使用
GCC编译源码shellcd redis-5.0.4 make安装
Redisshellmake install验证
shellroot@local:~/temp# redis-server -v Redis server v=5.0.4 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=3dcf53963ddc396a root@local:~/temp# whereis redis-server redis-server: /usr/local/bin/redis-server
至此,Redis安装完成。
CentOs
编译安装
shellmake $$ make install此时报错
shellCC adlist.o /bin/sh: cc: command not found make[1]: *** [adlist.o] Error 127 make[1]: Leaving directory `/root/temp/redis-5.0.4/src' make: *** [all] Error 2安装 gcc
shellyum install gcc -y重新 make
shellmake此时报错
shellIn file included from adlist.c:34: zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory zmalloc.h:55:2: error: #error "Newer version of jemalloc required" make[1]: *** [adlist.o] Error 1 make[1]: Leaving directory `/root/temp/redis-5.0.4/src' make: *** [all] Error 2在构建
Redis时选择非默认内存分配器是通过设置MALLOC环境变量完成的, 默认情况下 Redis 是使用malloc为libc编译和链接的。 而libc并不是Linux上默认的分配器,默认的是jemalloc, 因为jemalloc被证明比libc有更少的碎片问题(fragmentation problems)。 但是如果你没有jemalloc而只有libc当然make出错。 所以有两种解决办法:方法一(不推荐)
shellmake MALLOC=libc- 方法二
shellcd deps/ make hiredis jemalloc linenoise lua geohash-int原因参见: 浅谈 redis 采用不同内存分配器 tcmalloc 和 jemalloc
对于
tcmalloc,jemalloc和libc对应的三个内存分配器。其性能和碎片率如何呢? 下面是一个简单测试结果,使用Redis自带的redis-benchmark写入等量数据进行测试,数据摘自采用不同分配器时Redis info信息。 我们可以看到,采用tcmalloc时碎片率是最低的,为1.01,jemalloc为1.02,而libc的分配器碎片率为1.31,编译安装
shellmake $$ make install验证
redis-server -v
Redis server v=5.0.4 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=b139020f90f1d493
whereis redis-server
redis-server: /usr/local/bin/redis-server至此,Redis安装完成。
配置
通过配置文件,设置Redis服务开机自启动。
设置自启动配置文件
切换目录
shellcd utils/复制脚本文件
shellcp redis_init_script /etc/init.d/redisd ## ①将
redis_init_script文件重新命名为redisd,作为系统启动服务名(以d结尾表示是自启动服务,约定俗成)。修改配置
shellvi /etc/init.d/redisd修改
redisd文件,注意要在文件头部加上两句注释来设定该服务的运行级别shell#!/bin/sh # chkconfig: 2345 90 10
设置
Redis控制脚本的配置文件切换目录
shellcd - cd ..在
redis安装目录下,找到redis.conf文件shellls如下
shell00-RELEASENOTES INSTALL runtest tests BUGS Makefile runtest-cluster utils CONTRIBUTING MANIFESTO runtest-sentinel COPYING README.md sentinel.conf deps redis.conf src复制配置文件并重命名
shellcp redis.conf /etc/redis/6379.conf ## ②编辑
Redis配置文件设置
daemonize为yes,使服务可以后台运行:shell# nu:136 daemonize yes设置
log文件路径:shell# nu:171 logfile /var/log/redis/redis-server.log设置持久化文件存放路径:
shell# nu:263 dir /var/lib/redis
保存退出,并创建相应的目录结构:
shellmkdir /var/log/redis touch /var/log/redis/redis-server.log mkdir /var/lib/redis
设置开机自启
Ubuntu
shell# 赋权 chmod +x /etc/init.d/redisd # 更新系统启动项 update-rc.d redisd defaultsCentOS
shell[root@master init.d]# chmod +x ./redisd [root@master init.d]# chkconfig redisd on
附:常用redis管理命令
- 启动
Redis服务:
service redisd start
[root@master init.d]# service redisd start
Starting Redis server...
# 验证
[root@master init.d]# ps aux|grep redis|grep -v grep
root 6728 0.1 0.4 55572 9820 ? Ssl 11:03 0:00 /usr/local/bin/redis-server 127.0.0.1:6379- 关闭服务:
[root@master init.d]# service redisd stop
Stopping ...
Redis stopped
[root@master init.d]# ps aux|grep redis|grep -v grep- 重启服务:
service redisd restart- 在控制台中登录
redis客户端:
[root@master init.d]# redis-cli
# 测试redis连通性
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"安装提供Python支持
pip 安装
pip install redis源码安装
去https://pypi.org/project/redis/下载源码,Ubuntu上使用最新版本redis 3.2.1
解压
shelltar -xzvf redis-3.2.1.tar.gz切换目录
shellcd redis-3.2.1安装
shellpython setup.py install验证
shellroot@local:~/temp/redis-3.2.1# python Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import redis >>> import redis >>> >>> r = redis.Redis(host='localhost', port=6379, db=0) >>> ret = r.get('hello') >>> print ret world
小插曲
关于 redis-py 的 Python 低版本支持
在CentOS上安装redis-3.2.1的时候由于python版本较低(2.6.6)出现以下问题
[root@master redis-3.2.1]# python setup.py install
Traceback (most recent call last):
File "setup.py", line 4, in <module>
from setuptools import setup
ImportError: No module named setuptools安装setuptools-0.6c9之后执行python setup.py install报错:
Traceback (most recent call last):
File "setup.py", line 7, in <module>
from redis import __version__
File "/root/temp/pyredis-3.2.1/redis/__init__.py", line 1, in <module>
from redis.client import Redis, StrictRedis
File "/root/temp/pyredis-3.2.1/redis/client.py", line 3046
return {decode(encode(k)): v for k, v in iteritems(data)}
^
SyntaxError: invalid syntax去官网查看,发现最新版版本支持为:
Meta
……
Tags: Redis, key-value store
Requires: Python >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*以及
Python Version Support redis-py 3.0 now supports Python 2.7 and Python 3.4+. Python 2.6 and 3.3 support has been dropped.
最后安装较低版本(redis-2.10.6)成功,步骤同上,不再赘述。
读者可以从 这里 获取历史版本:https://pypi.org/project/redis/#history
参考链接
①:内容见 redisd ②:内容见 6379.conf
