Total Pageviews

Saturday 24 May 2014

redis的安装

这里简单记录一下redis的安装。
先从redis的官网下载最新稳定版本,官网下载地址为:
http://redis.io/download
目前最新的稳定版本是2.6.13,下载redis-2.6.13.tar.gz后进入到解压目录:redis-2.6.13 ,然后运行命令make,如果没问题,则会提示运行make test,强烈建议要运行该命令,运行make test后,有可能会报错:
You need tcl 8.5 or newer in order to run the Redis test
这是因为还需要安装一个依赖包TCL,该包的下载地址是:
http://www.linuxfromscratch.org/blfs/view/cvs/general/tcl.html
下载解压
tar -xf ../tcl8.6.0-html.tar.gz --strip-components=1
配置编译:
cd unix &&
./configure --prefix=/usr \
            --mandir=/usr/share/man \
            --without-tzdata \
            $([ $(uname -m) = x86_64 ] && echo --enable-64bit) &&
make
编译如果没问题,也可以运行 make test 来测试,没问题后,安装:
make install &&
make install-private-headers &&
ln -v -sf tclsh8.6 /usr/bin/tclsh &&
chmod -v 755 /usr/lib/libtcl8.6.so
安装成功后,再进入到redis目录,运行make test,如果没问题了,就安装好了!
启动redis服务,由于是在一台机上,而且也没有作任何其它的配置,所以服务启动后默认是单笠模式的,启动命令:
src/redis-server
默认服务端口是6379。
试着写入并读取数据:
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
一切OK,证明安装成功。
--------------


Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, HyperLogLogs, Bitmaps. 

This README is just a fast quick start document. You can find more detailed documentation at http://redis.io.

What is Redis?

Redis is often referred as a data structures server. What this means is that Redis provides access to mutable data structures via a set of commands, which are sent using a server-client model with TCP sockets and a simple protocol. So different processes can query and modify the same data structures in a shared way.
Data structures implemented into Redis have a few special properties:
  • Redis cares to store them on disk, even if they are always served and modified into the server memory. This means that Redis is fast, but that is also non-volatile.
  • Implementation of data structures stress on memory efficiency, so data structures inside Redis will likely use less memory compared to the same data structure modeled using an high level programming language.
  • Redis offers a number of features that are natural to find in a database, like replication, tunable levels of durability, cluster, high availability.
Another good example is to think of Redis as a more complex version of memcached, where the operations are not just SETs and GETs, but operations to work with complex data types like Lists, Sets, ordered data structures, and so forth.
If you want to know more, this is a list of selected starting points:

Building Redis

Redis can be compiled and used on Linux, OSX, OpenBSD, NetBSD, FreeBSD. We support big endian and little endian architectures, and both 32 bit and 64 bit systems.
It may compile on Solaris derived systems (for instance SmartOS) but our support for this platform is best effort and Redis is not guaranteed to work as well as in Linux, OSX, and *BSD there.
It is as simple as:
% make
You can run a 32 bit Redis binary using:
% make 32bit
After building Redis is a good idea to test it, using:
% make test

Fixing build problems with dependencies or cached build options

Redis has some dependencies which are included into the deps directory. make does not rebuild dependencies automatically, even if something in the source code of dependencies is changes.
When you update the source code with git pull or when code inside the dependencies tree is modified in any other way, make sure to use the following command in order to really clean everything and rebuild from scratch:
make distclean
This will clean: jemalloc, lua, hiredis, linenoise.
Also if you force certain build options like 32bit target, no C compiler optimizations (for debugging purposes), and other similar build time options, those options are cached indefinitely until you issue a make distclean command.

Fixing problems building 32 bit binaries

If after building Redis with a 32 bit target you need to rebuild it with a 64 bit target, or the other way around, you need to perform a make distclean in the root directory of the Redis distribution.
In case of build errors when trying to build a 32 bit binary of Redis, try the following steps:
  • Install the packages libc6-dev-i386 (also try g++-multilib).
  • Try using the following command line instead of make 32bitmake CFLAGS="-m32 -march=native" LDFLAGS="-m32"

Allocator

Selecting a non-default memory allocator when building Redis is done by setting the MALLOCenvironment 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

Verbose build

Redis will build with a user friendly colorized output by default. If you want to see a more verbose output use the following:
% make V=1

Running Redis

To run Redis with the default configuration just type:
% cd src
% ./redis-server
If you want to provide your redis.conf, you have to run it using an additional parameter (the path of the configuration file):
% cd src
% ./redis-server /path/to/redis.conf
It is possible to alter the Redis configuration passing parameters directly as options using the command line. Examples:
% ./redis-server --port 9999 --slaveof 127.0.0.1 6379
% ./redis-server /etc/redis/6379.conf --loglevel debug
All the options in redis.conf are also supported as options using the command line, with exactly the same name.

Playing with Redis

You can use redis-cli to play with Redis. Start a redis-server instance, then in another terminal try the following:
% cd src
% ./redis-cli
redis> ping
PONG
redis> set foo bar
OK
redis> get foo
"bar"
redis> incr mycounter
(integer) 1
redis> incr mycounter
(integer) 2
redis>
You can find the list of all the available commands at http://redis.io/commands.

Installing Redis

In order to install Redis binaries into /usr/local/bin just use:
% make install
You can use make PREFIX=/some/other/directory install if you wish to use a different destination.
Make install will just install binaries in your system, but will not configure init scripts and configuration files in the appropriate place. This is not needed if you want just to play a bit with Redis, but if you are installing it the proper way for a production system, we have a script doing this for Ubuntu and Debian systems:
% cd utils
% ./install_server.sh
The script will ask you a few questions and will setup everything you need to run Redis properly as a background daemon that will start again on system reboots.
You'll be able to stop and start Redis using the script named /etc/init.d/redis_<portnumber>, for instance /etc/init.d/redis_6379.

Code contributions

Note: by contributing code to the Redis project in any form, including sending a pull request via Github, a code fragment or patch via private email or public discussion groups, you agree to release your code under the terms of the BSD license that you can find in the COPYING file included in the Redis source distribution.
Please see the CONTRIBUTING file in this source distribution for more information.

Redis internals

If you are reading this README you are likely in front of a Github page or you just untarred the Redis distribution tar ball. In both the cases you are basically one step away from the source code, so here we explain the Redis source code layout, what is in each file as a general idea, the most important functions and structures inside the Redis server and so forth. We keep all the discussion at an high level without digging into the details since this document would be huge otherwise, and our code base changes continuously, but a general idea should be a good starting point to understand more. Moreover most of the code is heavily commented and easy to follow.

Source code layout

The Redis root directory just contains this README, the Makefile which actually calls the real Makefile inside the src directory, an example configuration for Redis and Sentinel. Finally you can find a few shell scripts that are used in order to execute the Redis, Redis Cluster and Redis Sentinel unit tests, which are implemented inside the tests directory.
Inside the root directory the are the following important directories:
  • src: contains the Redis implementation, written in C.
  • tests: contains the unit tests, implemented in Tcl.
  • deps: contains libraries Redis uses. Everything needed to compile Redis is inside this directory, your system needs to provide just the libc, a POSIX compatible interface, and a C compiler. Notably deps contains a copy of jemalloc, which is the default allocator of Redis under Linux. Note that under deps there are also things which started with the Redis project, but for which the main repository is not anitrez/redis. an exception to this rule is deps/geohash-intwhich is the low level geocoding library used by Redis: it originated from a different project, but at this point it diverged so much that it is developed as a separated entity directly inside the Redis repository.
There are a few more directories but they are not very important for our goals here. We'll focus mostly on src, where the Redis implementation is contained, exploring what there is inside each file. The order in which files are exposed is the logical one to follow in order to disclose different layers of complexity incrementally.
Note: lately Redis was refactored quite a bit. Function names and file names changed, so you may find that this documentation reflects the unstable branch more closely. For instance in Redis 3.0 the server.c and server.h files were renamed redis.c and redis.h. However the overall structure is the same. Keep in mind that all the new developments and pull requests should be performed against the unstable branch.
from https://github.com/antirez/redis
------------

redis的三种启动方式

Part I. 直接启动

下载



安装


Shell代码  收藏代码
  1. tar zxvf redis-2.8.9.tar.gz  
  2. cd redis-2.8.9  
  3. #直接make 编译  
  4. make  
  5. #可使用root用户执行`make install`,将可执行文件拷贝到/usr/local/bin目录下。这样就可以直接敲名字运行程序了。  
  6. make install  

 

启动


Shell代码  收藏代码
  1. #加上`&`号使redis以后台程序方式运行  
  2. ./redis-server &  

 

检测


Shell代码  收藏代码
  1. #检测后台进程是否存在  
  2. ps -ef |grep redis  
  3.   
  4. #检测6379端口是否在监听  
  5. netstat -lntp | grep 6379  
  6.   
  7. #使用`redis-cli`客户端检测连接是否正常  
  8. ./redis-cli  
  9. 127.0.0.1:6379> keys *  
  10. (empty list or set)  
  11. 127.0.0.1:6379> set key "hello world"  
  12. OK  
  13. 127.0.0.1:6379> get key  
  14. "hello world"  

 

停止


Shell代码  收藏代码
  1. #使用客户端  
  2. redis-cli shutdown  
  3. #因为Redis可以妥善处理SIGTERM信号,所以直接kill -9也是可以的  
  4. kill -9 PID  


Part II. 通过指定配置文件启动

配置文件

可为redis服务启动指定配置文件,配置文件redis.conf在Redis根目录下。

Shell代码  收藏代码
  1. #修改daemonize为yes,即默认以后台程序方式运行(还记得前面手动使用&号强制后台运行吗)。  
  2. daemonize no  
  3. #可修改默认监听端口  
  4. port 6379  
  5. #修改生成默认日志文件位置  
  6. logfile "/home/futeng/logs/redis.log"  
  7. #配置持久化文件存放位置  
  8. dir /home/futeng/data/redisData  


启动时指定配置文件


Shell代码  收藏代码
  1. redis-server ./redis.conf  
  2. #如果更改了端口,使用`redis-cli`客户端连接时,也需要指定端口,例如:  
  3. redis-cli -p 6380  


其他启停同直接启动方式。配置文件是非常重要的配置工具,随着使用的逐渐深入将显得尤为重要,推荐在一开始就使用配置文件。

Part III. 使用Redis启动脚本设置开机自启动

启动脚本

推荐在生产环境中使用启动脚本方式启动redis服务。启动脚本redis_init_script位于位于Redis的/utils/目录下。

Shell代码  收藏代码
  1. #大致浏览下该启动脚本,发现redis习惯性用监听的端口名作为配置文件等命名,我们后面也遵循这个约定。  
  2. #redis服务器监听的端口  
  3. REDISPORT=6379  
  4. #服务端所处位置,在make install后默认存放与`/usr/local/bin/redis-server`,如果未make install则需要修改该路径,下同。  
  5. EXEC=/usr/local/bin/redis-server  
  6. #客户端位置  
  7. CLIEXEC=/usr/local/bin/redis-cli  
  8. #Redis的PID文件位置  
  9. PIDFILE=/var/run/redis_${REDISPORT}.pid  
  10. #配置文件位置,需要修改  
  11. CONF="/etc/redis/${REDISPORT}.conf"  


配置环境

1. 根据启动脚本要求,将修改好的配置文件以端口为名复制一份到指定目录。需使用root用户。

Shell代码  收藏代码
  1. mkdir /etc/redis  
  2. cp redis.conf /etc/redis/6379.conf  
 2. 将启动脚本复制到/etc/init.d目录下,本例将启动脚本命名为redisd(通常都以d结尾表示是后台自启动服务)。

Shell代码  收藏代码
  1. cp redis_init_script /etc/init.d/redisd  
 3. 设置为开机自启动

此处直接配置开启自启动chkconfig redisd on将报错误:service redisd does not support chkconfig 参照此篇文章,在启动脚本开头添加如下两行注释以修改其运行级别:

Shell代码  收藏代码
  1. #!/bin/sh  
  2. # chkconfig:   2345 90 10  
  3. # description:  Redis is a persistent key-value database  
  4. #  
 再设置即可成功。


Shell代码  收藏代码
  1. #设置为开机自启动服务器  
  2. chkconfig redisd on  
  3. #打开服务  
  4. service redisd start  
  5. #关闭服务  
  6. service redisd stop