centos6安装redis-4.x

本文详细介绍了在Linux环境下部署和优化Redis服务器的过程,包括安装必要的软件包、设置防火墙、创建用户和目录、编译安装Redis、配置参数、解决常见警告、设置自启动等关键步骤。

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

ssh

yum install openssh-server
service sshd start
chkconfig sshd on

iptables

service iptables start
chkconfig iptables on
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
service iptables restart

创建用户

adduser redis
passwd redis

创建存储目录

mkdir /usr/local/redis/
mkdir /usr/local/redis/data
chown redis:redis /usr/local/redis/
chown redis:redis /usr/local/redis/data
su redis
chmod 700 /usr/local/redis/
chmod 700 /usr/local/redis/data

/bin/sh: cc: command not found

sudo yum -y install gcc gcc-c++ libstdc++-devel 
make MALLOC=libc

安装redis

su redis
tar xzf redis-4.0.9.tar.gz   
ln -s redis-4.0.9 redis #建立一个链接
su root
make install

修改配置

vi /usr/local/redis/reidis/redis.conf
daemonize yes
requirepass foobared
bind 0.0.0.0
protected-mode no
tcp-keepalive 60
appendonly yes
no-appendfsync-on-rewrite yes
dir /usr/local/redis/data

警告解决

3367:M 27 Oct 14:04:42.093 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
3367:M 27 Oct 14:04:42.093 # Server can’t set maximum open files to 10032 because of OS error: Operation not permitted.
3367:M 27 Oct 14:04:42.093 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase ‘ulimit -n’.

vi /etc/security/limits.conf 
*               soft    nofile          65535
*               hard    nofile          65535
sysctl -p

3426:M 27 Oct 14:28:16.367 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

vi /etc/sysctl.conf
net.core.somaxconn= 1024
sysctl -p

3426:M 27 Oct 14:28:16.367 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

echo never > /sys/kernel/mm/transparent_hugepage/enabled
vi /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
reboot

3426:M 27 Oct 14:28:16.367 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘i’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.

vi /etc/sysctl.conf
vm.overcommit_memory = 1
sysctl -p

自启动

cp /usr/local/redis/redis/utils/redis_init_script /etc/rc.d/init.d/redis
vi /etc/rc.d/init.d/redis
### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/redis/redis/bin/redis-server
CLIEXEC=/usr/local/redis/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/redis/redis.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                cd /home/redis
                su redis -c "$EXEC $CONF"
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                cd /home/redis
                su redis -c "$CLIEXEC -p $REDISPORT -a foobared shutdown"
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
chkconfig --add redis
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值