您的位置:首页 > 数据库 > Redis

Redis 配置

2016-06-08 19:52 344 查看
Redis 配置

1.启动服务

  redis-server /usr/local/etc/redis.conf

2.修改配置文件  

  添加密码:requirepass password

  远程访问数据库:bind 0.0.0.0

  作为后台服务启动:daemonize yes

3.远程访问redis数据库:

  redis-cli -h 192.168.1.123 -p 6379 -a "password"

  如果远程访问失败,则需要关闭linux防火墙或开放6379端口:

  防火墙  

    需要重启后生效
      chkconfig iptables on
      chkconfig iptables off

     即时生效,重启后失效
      service iptables start
      service iptables stop

  开放端口

    修改/etc/sysconfig/iptables 文件,添加以下内容:
    -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
    -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

    -A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT

4.设置开机启动

  建立redis用户与日志目录
    useradd redis
    mkdir -p /var/lib/redis
    mkdir -p /var/log/redis
    chown redis.redis /var/lib/redis //redis的db文件需要放在这里,需要修改redis.conf配置
    chown redis.redis /var/log/redis

    chown redis.redis /var/log/redis

  建立启动脚本文件
  

# chkconfig: 2345 90 10

# description: Redis is a persistent key-value database

PATH=/bin:/sbin:/usr/bin:/usr/local/bin

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

PIDFILE=/var/run/redis.pid
CONF="/usr/local/etc/redis.conf"

case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Reids server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$REDIS_CLI -p $REDISPORT -a cjqc123456 SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waitiong for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac


  设定开机启动服务

    将开机启动脚本放到/etc/init.d/目录中
    cp redis /etc/init.d
    chmod +x /etc/init.d/redis
    chkconfig redis on
    // 如果出现错误:service redis does not support chkconfig
    // 则在redis文件开头增加如下代码:
    # chkconfig: 2345 90 10
    # description: Redis is a persistent key-value database
  测试启动脚本
    // 启动redis服务
    service redis start or /etc/init.d/redis start
    // 停止Redis服务
    service redis stop or /etc/init.d/redis stop

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: