Redis之基础-1 Redis2.6.16版本之安装介绍

本文档详细介绍了在 CentOS 5.8 系统上安装 Redis 2.6.16 的步骤,包括环境准备、Redis 规划、安装过程、配置详解、启动与连接方法,以及服务的自启动设置。通过此指南,读者能够了解并完成 Redis 的完整安装流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、服务器基本环境:
系统版本:
[root@localhost ~]# cat /etc/redhat-release
CentOS release 5.8 (Final)

系统位数:
[root@localhost ~]# getconf LONG_BIT
64

防火墙:
[root@localhost ~]# service iptables stop
[root@localhost ~]# service ip6tables stop
[root@localhost ~]# chkconfig iptables off
[root@localhost ~]# chkconfig ip6tables off

SElinux:
[root@localhost ~]# sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0

/** 此处需要重启生效 */
[root@localhost ~]# sestatus
SELinux status:                 disabled

IP ADDRESS:
[root@localhost ~]# ifconfig eth0
eth0  Link encap:Ethernet  HWaddr 08:00:27:5D:6D:B2  
          inet addr:192.168.128.200  Bcast:192.168.255.255  Mask:255.255.0.0
          inet6 addr: fe80::a00:27ff:fe5d:6db2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:58608 errors:0 dropped:0 overruns:0 frame:0
          TX packets:844 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:4553888 (4.3 MiB)  TX bytes:103068 (100.6 KiB)
          Base address:0xd010 Memory:f0000000-f0020000

二、Redis规划:
Redis源码包版本:redis-2.6.16.tar.gz
Redis的安装位置:/usr/local/redis2.6
Redis的配置文件:/usr/local/redis2.6/redis.conf 
Redis的日志文件:/usr/local/redis2.6/redis.log
Redis的进程文件:/usr/local/redis2.6/redis.pid 

三、安装Redis:
1.安装gpertools工具:
提示:libunwind 一般情况下不必指定安装路径,用默认的参数安装就好,否则很可能导致gpertools无法找到路径的问题。
# wget -c http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz
# tar zxvf libunwind-1.1.tar.gz && cd libunwind-1.1/
# CFLAGS=-fPIC ./configure
# make CFLAGS=-fPIC
# make CFLAGS=-fPIC install
 
2.安装Google gperftools 内存性能优化软件:
# wget -c --no-check-certificate  https://gperftools.googlecode.com/files/gperftools-2.1.tar.gz
# tar zxvf gperftools-2.1.tar.gz && cd gperftools-2.1/
# ./configure && make && make install && cd ../
# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
# /sbin/ldconfig
# mkdir -p /tmp/tcmalloc
# chmod 0777 /tmp/tcmalloc

3.安装Redis:
# cd /usr/local/src/
# wget -c http://download.redis.io/releases/redis-2.6.16.tar.gz 
# tar zxvf redis-2.6.16.tar.gz
# cd redis-2.6.16
# make USE_TCMALLOC=yes PREFIX=/usr/local/redis2.6 install
# cp /usr/local/src/redis-2.6.16/redis.conf /usr/local/redis2.6/
 
4.配置Redis:
(1)redis.conf配置文件参数:
# vim /usr/local/redis2.6/redis.conf
daemonize yes
pidfile /usr/local/redis2.6/redis.pid
port 6379
#bind 0.0.0.0
unixsocket /tmp/redis.sock
unixsocketperm 755
timeout 300
tcp-keepalive 300
#loglevel verbose
loglevel notice
logfile /usr/local/redis2.6/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis2.6
slaveof 192.168.128.200 6379
#masterauth jason
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
#requirepass jason
appendonly yes
appendfsync everysec
maxclients 10000
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

5.Redis命令介绍:
Redis 由四个可执行文件:redis-benchmark、redis-cli、redis-server、redis-stat 这四个文件,加上一个redis.conf就构成了整个redis的最终可用包。它们的作用如下:
redis-server:Redis服务器的Daemon启动程序
redis-cli:Redis命令行操作工具。当然,你也可以用Telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况
 
6.启动Redis:
# redis-server /usr/local/redis2.6/redis.conf

7.连接Redis:
# redis-cli
redis 127.0.0.1:6379> auth jason
OK
redis 127.0.0.1:6379> exit

8.关闭Redis:
# redis-cli shutdown
关闭某个端口上的Redis:
说明:关闭以后缓存数据会自动dump到硬盘上,硬盘地址见redis.conf中的dbfilename dump.rdb
# redis-cli -p 6397 shutdown  //关闭6397端口的Redis

9.Redis自启动设置:
# chmod +x /etc/init.d/redis 
# chkconfig --add redis
# chkconfig --level 35 redis on
# ln -v -sf /usr/local/redis2.6/bin/* /usr/local/bin/

Redis启动脚本如下:
# cat /etc/init.d/redis
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig:   - 85 15
# description:  Redis is a persistent key-value database
# processname: redis-server
# config:      /usr/local/redis2.6/redis.conf
# pidfile:    /usr/local/redis2.6/redis.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
redis="/usr/local/redis2.6/bin/redis-server"
prog=$(basename $redis)
 
REDIS_CONF_FILE="/usr/local/redis2.6/redis.conf"
 
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
 
lockfile=/var/lock/subsys/redis
 
start() {
    [ -x $redis ] || exit 5
    [ -f $REDIS_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $redis $REDIS_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    stop
    start
}
 
reload() {
    echo -n $"Reloading $prog: "
    killproc $redis -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac

启动后信息提示:
[20044] 25 Nov 16:03:53.768 # 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.
修改内核参数文件:
# echo -e "#Redis\nvm.overcommit_memory = 1" >> /etc/sysctl.conf
# sysctl -p

重启Redis服务:
# service redis restart

参考文献:
官方主站:http://www.redis.io/
下载地址:http://www.redis.cn/download.html
Interactive Tutorial:http://try.redis-db.com/ 
Command API:http://redis.io/commands    
             http://www.redis.cn/commands.html
客户端程序:http://redis.io/clients
Redis文档:http://redis.io/documentation
命令参考:http://manual.csser.com/redis/
Redis内存使用优化与存储:http://www.infoq.com/cn/articles/tq-redis-memory-usage-optimization-storage
Redis复制与可扩展集群搭建:http://www.infoq.com/cn/articles/tq-redis-copy-build-scalable-cluster
Redis资料汇总专题:http://blog.nosqlfan.com/html/3537.html
将redis变成数据库:http://code.google.com/p/alchemydatabase/
类似的产品:http://code.google.com/p/memlink/
Redis-dump:将Redis数据dump成json格式的工具:https://github.com/delano/redis-dump
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值