第1章 一、安装redis
1、下载redis包 wget http://download.redis.io/releases/redis-3.2.1.tar.gz 2、解压redis包到/opt下 tar -zxvf /home/redis-3.2.1.tar.gz -C /opt 3、安装并测试redis cd /opt/redis-3.2.1/src make && make install
二、配置redis
1、复制并修改配置文档 cp /opt/redis-3.2.1/redis.conf /etc/ vi /etc/redis.conf #开启redis远程访问 #将 bind 127.0.0.1改为 bind 0.0.0.0 #开启redis守护进程 #根据需求,可配置redis是否进行持久化:修改save策略 #添加密码访问 requirepass password 2、将redis配置进系统服务 vi /etc/init.d/redis /etc/init.d/redis内容如下 #chkconfig: 2345 90 10 # description: Redis is a persistent key-value database ########################### PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 EXEC=/usr/local/bin/redis-server REDIS_CLI=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_6379.pid CONF="/etc/redis.conf" PASSWORD=password case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis 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 SHUTDOWN #若有密码则加上密码 #$REDIS_CLI -a $PASSWORD -p $REDISPORT SHUTDOWN while [ -x ${PIDFILE} ] do echo "Waiting 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 ############################## #给redis文件加上执行权限 chmod +x /etc/init.d/redis 3、设置redis服务开机启动 chkconfig redis on 4、在防火墙中开放redis端口 vi /etc/sysconfig/iptables #加入下面的端口配置 -A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT #重启防火墙 service iptables restart 5、启动、关闭和重启redis服务 service redis start service redis stop service redis restart
三、使用redis
#进入redis命令行 redis-cli -a password 3.master配置了密码,slave如何