Linux下redis安装和配置

redis的特点是啥,快,下面是在CentOS6.4上安装和配置非关系型数据库redis

1:下载和编译

[root@localhost ~]# wget http://download.redis.io/releases/redis-2.8.19.tar.gz
[root@localhost ~]# tar zxvf redis-2.8.19.tar.gz
[root@localhost ~]# cd redis-2.8.19
[root@localhost redis-2.8.19]# make -j12
[root@localhost redis-2.8.19]# make test
cd src && make test
make[1]: Entering directory `/root/redis-2.8.19/src'
You need tcl 8.5 or newer in order to run the Redis test

这里报错了,需要安装 >= 8.5版本的tcl,幸运的是CentOS6.4默认yum源正好8.5

[root@localhost redis-2.8.19]# yum install tcl
[root@localhost redis-2.8.19]# make test

到了这一步,该干的都干了,检验一下,先直接启动cli命令行

[root@localhost src]# ./redis-cli 
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> 

可见默认端口是6379,必须先启动服务端

服务端
[root@localhost redis-2.8.19]# cd src
[root@localhost src]# ./redis-server

cli
[root@localhost src]# ./redis-cli 
127.0.0.1:6379> 

看样子OK了

2:安装

这里对于安装redis有两点貌似没做,一点是上面的make install,并没有install到系统目录;另一点是服务默认是127.0.0.1和端口6379,可见肯定会有一个配置文件来配置这些信息,而服务会读这些信息来启动daemon(如果能设置的话)

关于make install,查看README文档

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.

如果make install指定了可执行程序install的目录,中间一大段看来是提供一个交互模式,让你配置端口等等之类的信息,最后还提供了一个daemon程序,可以添加到service,那就照做,交互的时候,全部回车默认即可

[root@localhost redis-2.8.19]# make install
[root@localhost redis-2.8.19]# cd utils
[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

3:配置

既然都安装好了,service里也有了,就直接service启动redis服务再说

[root@localhost utils]# service redis_6379 start
/var/run/redis_6379.pid exists, process is already running or crashed

[root@localhost utils]# lsof -i:6379
COMMAND     PID USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
redis-ser 25013 root    4u  IPv6 340534681      0t0  TCP *:6379 (LISTEN)
redis-ser 25013 root    5u  IPv4 340534683      0t0  TCP *:6379 (LISTEN)

启动居然失败了,这里提示进程已经存在,或者是crashed状态,可见这里判断进程存在是根据pid文件来判断,其实我觉得这种方法不太好,不过它提示都已经说明了,异常crashed可能导致误判;这里查看下6379端口占用的进程,的的确确已经被占用了,可能是刚刚测试时启动了后台daemon,这里就先service关掉,再启动

[root@localhost utils]# service redis_6379 stop
Stopping ...
Redis stopped
You have new mail in /var/spool/mail/root
[root@localhost utils]# lsof -i:6379
[root@localhost utils]# service redis_6379 start
Starting Redis server...
You have new mail in /var/spool/mail/root
[root@localhost utils]# lsof -i:6379
COMMAND     PID USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
redis-ser 28555 root    4u  IPv6 340571416      0t0  TCP *:6379 (LISTEN)
redis-ser 28555 root    5u  IPv4 340571418      0t0  TCP *:6379 (LISTEN)
[root@localhost utils]# ps aux | grep redis
root     28555  0.0  0.0  40532  2044 ?        Ssl  21:58   0:00 /usr/local/bin/redis-server *:6379              
root     28619  0.0  0.0 103176   676 pts/2    D+   21:58   0:00 grep redis

这里,再确认一下启动的参数,由于是service启动的,直接查看redis_6379

[root@localhost utils]# vim /etc/init.d/redis_6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/etc/redis/6379.conf"
REDISPORT="6379"

启动的命令是:$EXEC $CONF

可见跟料想的一样,还是读了配置文件6379.conf,这样就清楚了~!对于配置文件,还是要都了解一下大概意思

daemonize:是否以后台daemon方式运行
pidfile:pid文件位置
port:监听的端口号
timeout:请求超时时间
loglevel:log信息级别
logfile:log文件位置
databases:开启数据库的数量
save * *:保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。
rdbcompression:是否使用压缩
dbfilename:数据快照文件名(只是文件名,不包括目录)
dir:数据快照的保存目录(这个是目录)
appendonly:是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。
appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步)

除了配置,可执行程序有

[root@localhost utils]# ll /usr/local/bin/redis-*
-rwxr-xr-x. 1 root root 4585923 Feb 12 21:38 /usr/local/bin/redis-benchmark
-rwxr-xr-x. 1 root root   22217 Feb 12 21:38 /usr/local/bin/redis-check-aof
-rwxr-xr-x. 1 root root   45435 Feb 12 21:38 /usr/local/bin/redis-check-dump
-rwxr-xr-x. 1 root root 4679403 Feb 12 21:38 /usr/local/bin/redis-cli
lrwxrwxrwx. 1 root root      27 Feb 12 21:38 /usr/local/bin/redis-sentinel -> /usr/local/bin/redis-server
-rwxr-xr-x. 1 root root 6241412 Feb 12 21:38 /usr/local/bin/redis-server

redis-benchmark是性能测试工具,测试Redis在你的系统及你的配置下的读写性能

redis-check-aof是更新日志检查,修复日志文件

redis-check-dump是检查本地数据库文件

redis-cli是命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作 

redis-server是服务器的daemon启动程序

 

虽然啰嗦了不少,不得不说redis安装真的是超级简洁,3步就完了,但是配置内容已经所有执行程序的含义可以了解下~!

发表回复