四时宝库

程序员的知识宝库

centos7 安装配置redis(centos7安装redis集群)

1.下载解压

wget https://download.redis.io/redis-stable.tar.gz
tar -xvf redis-stable.tar.gz

2.编译安装

make /usr/local/redis     //如果不安装指定目录,那么默认是在/usr/bin
mkdir -p /www/redisdata   // 数据目录,个人习惯是系统盘和数据盘挂载到不同磁盘
cd redis-stable
make PREFIX=/usr/local/redis install

3.编辑配置文件

# cat /etc/redis.conf 
daemonize yes
pidfile /var/run/redis.pid
port 6379
tcp-backlog 511
timeout 300
tcp-keepalive 0
loglevel notice
logfile "/var/log/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 /www/redisdata
slave-serve-stale-data yes
slave-read-only yes
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-entries 512
list-max-ziplist-value 64
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
requirepass 123456                //建议设置密码验证

4.编写启动脚本,编辑配置文件/etc/init.d/redis

# cat /etc/init.d/redis 
#!/bin/sh
#
# Startup script Redis-server
#
# chkconfig: - 49 50
# description: This script starts your redis-server
# processname: redis
# pidfile: /var/run/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

[ -f $redis ] || exit 0


RETVAL=0

# See how we were called.
case "$1" in
  start)
        if [ $UID -ne 0 ] ; then
            echo "User has insufficient privilege."
            exit 4
        fi
        echo -n "Starting redis-server: "
        /usr/local/redis/bin/redis-server /etc/redis.conf
	RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && echo "redis-server start successfully...."
        ;;
  stop)
	if [ -f /var/run/redis.pid ];then
		/usr/local/redis/bin/redis-cli shutdown
		RETVAL=$?
        else
		RETVAL=1
	fi
        echo
        [ $RETVAL -eq 0 ] && echo "redis-servr shutdown successfully...." || echo "redis-server is downing..."
        ;;
  restart)
	$0 stop
	$0 start
	RETVAL=$?
	;;
  *)
        echo "Usage: $0 {start|stop|restart|}"
        exit 2
esac

exit $RETVAL

5.设置开机启动

chmod +x /etc/init.d/redis
chkconfig redis on

6.设置环境变量,如果按照默认路径可以不设置,在/etc/profile默认添加

#redis
REDIS_HOME=/usr/local/redis
PATH=$REDIS_HOME/bin:$PATH
export PATH
source /etc/profile

7.启动、停止、重启服务

service redis stop
service redis start
service redis restart

8.解决警告,查看日志tail -30f /var/log/redis.log

警告1:

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128

步骤:

echo "net.core.somaxconn = 1024" >> /etc/sysctl.conf
sysctl -p

设置打开文件个数参数,系统默认是1024,编辑配置文件/etc/security/limits.conf,在末尾添加:

* soft noproc 65535
* hard noproc 65535
* soft nofile 65535
* hard nofile 65535

重启,可以验证redis是否开机启动,至此redis安装配置完成。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接