安装redis-3.2.10单节点

前段时间安装好的redis,今天用脚本安装的时候突然出现版本异常的问题,所以更新一篇为大家提供参考

本次安装在CentOS6.5,采用的redis-3.2.10,最新的redis-4.0.1安装同样适用,不过由于4.0.1版本的redis改动较大,大家自己选择即可。,

1.基础环境配置,上传安装包

# 下载地址:http://download.redis.io/releases/redis-3.2.10.tar.gz

useradd redis -s /sbin/nologin -M
mkdir -p /server/tools/
cd /server/tools/

2.安装redis,配置开机自启动

# 解压,编译安装

tar -zxf redis-3.2.10.tar.gz 
cd redis-3.2.10
make PREFIX=/usr/local/redis
make PREFIX=/usr/local/redis install

# 配置环境变量,便于命令工具的调用访问

export PATH=/usr/local/redis/bin/:$PATH
echo 'export PATH=/usr/local/redis/bin:$PATH' >> /etc/profile
echo $PATH

# 手动创建标准目录结构,操作规范化

mkdir -p /usr/local/redis/{conf,data,logs}
/bin/cp /server/tools/redis-3.2.10/redis.conf /usr/local/redis/conf/redis.conf.ori
tree /usr/local/redis

# 创建修改配置文件

cd /usr/local/redis/conf
egrep -v "^#|^$" redis.conf.ori>redis.conf
cat redis.conf

# 默认生效的配置如下:

bind 127.0.0.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
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 ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
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

# 修改配置文件

vim redis.conf

# 确认以下必要的配置,修改完毕后的配置文件可以作为模板,用于创建新的redis实例

bind 0.0.0.0
port 6379
daemonize yes pidfile /usr/local/redis/logs/redis.pid logfile /usr/local/redis/logs/redis.log dir /usr/local/redis/data dbfilename redis.rdb

# 内核参数调优(临时)

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo 511 > /proc/sys/net/core/somaxconn
cat /sys/kernel/mm/transparent_hugepage/enabled
cat /proc/sys/net/core/somaxconn
echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
sysctl -p

# 启动redis,并查看redis启动状态

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
ps -ef |grep redis
netstat -anptl |grep redis
[root@redis-server conf]# cat /usr/local/redis/logs/redis.log 
6003:M 04 Sep 21:10:39.252 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.10 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6003
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

6003:M 04 Sep 21:10:39.255 # Server started, Redis version 3.2.10
6003:M 04 Sep 21:10:39.255 * The server is now ready to accept connections on port 6379

# 连接redis,进行测试

[root@redis-server conf]# /usr/local/redis/bin/redis-cli -p 6379
127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> set b 2
OK
127.0.0.1:6379> set c 3
OK
127.0.0.1:6379> get a 
"1"
127.0.0.1:6379> get b 
"2"
127.0.0.1:6379> get c
"3"
127.0.0.1:6379> exit
[root@redis-server conf]# 

 # 安全关闭redis-server

/usr/local/redis/bin/redis-cli shutdown

# 另外,需要设置一下内核参数保证下次重启不会出错

echo " ">> /etc/rc.local
echo "# redis by zhaoshuai in $(date +%F)" >> /etc/rc.local
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
echo "echo 511 > /proc/sys/net/core/somaxconn" >>/etc/rc.local
tail -4 /etc/rc.local

# 至此,redis-server就安装完成了。

3.创建新的redis实例(端口)

mkdir -p /usr/loca/redis/6380
cd /usr/local/redis/conf
cp redis.conf /usr/loca/redis/6380/6380.conf
cd /usr/local/redis/6380/

# 编辑配置文件,

vim 6380.conf

# 修改为以下内容:

bind 0.0.0.0
daemonize yes
pidfile /usr/local/redis/6380/pid-6380.pid
logfile /usr/local/redis/6380/log-6380.log
dir /usr/local/redis/6380
dbfilename 6380.rdb
......

# 启动redis新实例,如果需要可以设置开机自启动

[root@redis-server 6380]# /usr/local/redis/bin/redis-server /usr/local/redis/6380/6380.conf 
[root@redis-server 6380]# echo "/usr/local/redis/bin/redis-server /usr/local/redis/6380/6380.conf" >>/etc/rc.local 

 4.另附一个redis启动管理脚本,方便大家学习使用

 

#!/bin/sh
#
# chkconfig:   2345 85 15  
# description: this script can manager the redis-server daemon
#              Redis is a persistent key-value database
# exec:        /usr/local/redis/bin/redis-server
# config:      /usr/local/redis/conf/redis.conf  
# pidfile:     /usr/local/redis/logs/redis.pid  
# datafile:    /usr/local/redis/data/redis.rdb  

redis="/usr/local/redis/bin/redis-server"
REDIS_CONF_FILE="/usr/local/redis/conf/redis.conf"
prog=$(basename $redis)
lockfile=/var/lock/subsys/redis

# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
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
}
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)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 2
esac
View Code

 

# 完毕,呵呵呵

 

posted @ 2017-09-05 14:24  天生帅才  阅读(1399)  评论(0编辑  收藏  举报
// 百度统计