🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
一)单实例安装 * 第一种:yum安装 在centos7操作系统上,需要安装epel源,才能使用yum安装redis软件,默认自带的redis软件版本为: ~~~ Name : redis Arch : x86_64 Version : 3.2.10 Release : 2.el7 Size : 1.4 M ~~~ [admin@huancun01 ~]$ sudo yum -y install redis 查看: ~~~ [admin@huancun01 ~]$ sudo rpm -ql redis /etc/logrotate.d/redis /etc/redis-sentinel.conf (配置文件) /etc/redis.conf (配置文件) /etc/systemd/system/redis-sentinel.service.d /etc/systemd/system/redis-sentinel.service.d/limit.conf /etc/systemd/system/redis.service.d /etc/systemd/system/redis.service.d/limit.conf /usr/bin/redis-benchmark /usr/bin/redis-check-aof /usr/bin/redis-check-rdb /usr/bin/redis-cli /usr/bin/redis-sentinel /usr/bin/redis-server /usr/lib/systemd/system/redis-sentinel.service (启动脚本) /usr/lib/systemd/system/redis.service (启动脚本) /usr/libexec/redis-shutdown (关闭redis) ~~~ * 第二种:编译安装 ~~~ #!/bin/bash #Description install redis-4.0.8 SOFTDIR=/usr/local/src REDISVERSION=4.0.8 REDISPATH=/usr/local/redis-${REDISVERSION} BINDIP=$(/sbin/ifconfig eth1|grep inet|grep -v inet6|awk '{print $2}') REDISLOGDIR=/var/log/redis REDISCONFDIR=/etc/redis PROT=6379 REDISCONFNAME=redis_${PROT}.conf REDISDBDIR=/var/lib/redis_${PROT} USER=redis GROUP=redis # create redis user if ! id $USER &>/dev/null;then groupadd -g 1211 $GROUP useradd -u 1211 -g $GROUP -s /sbin/nologin $USER fi #create redis log dir and redis RDB dbdir [ ! -d $REDISLOGDIR ] && mkdir -p $REDISLOGDIR [ ! -d $REDISDBDIR ] && mkdir -p $REDISDBDIR chown -R $USER:$GROUP ${REDISLOGDIR} chown -R $USER:$GROUP ${REDISDBDIR} #install jemalloc yum -y install jemalloc #download redis and install redis cd $SOFTDIR && [ ! -f redis-${REDISVERSION}.tar.gz ] && wget http://download.redis.io/releases/redis-4.0.8.tar.gz [ ! -d redis-${REDISVERSION} ] && tar xf redis-${REDISVERSION}.tar.gz cd redis-${REDISVERSION} && make PREFIX=${REDISPATH} install #copy redis configure file redis [ ! -d $REDISCONFDIR ] && mkdir -p $REDISCONFDIR cd ${REDISCONFDIR} && [ ! -f ${REDISCONFNAME} ] && cd ${SOFTDIR}/redis-${REDISVERSION} && cp redis.conf ${REDISCONFDIR}/${REDISCONFNAME} sed -i "s#bind 127.0.0.1#bind ${BINDIP}#g" ${REDISCONFDIR}/${REDISCONFNAME} sed -i "s#daemonize no#daemonize yes#g" ${REDISCONFDIR}/${REDISCONFNAME} sed -i "s#logfile ""#logfile ${REDISLOGDIR}/redis_${PROT}.log#g" ${REDISCONFDIR}/${REDISCONFNAME} sed -i "s#dir ./#dir ${REDISDBDIR}#g" ${REDISCONFDIR}/${REDISCONFNAME} sed -i "500 a\ requirepass Aa123456" ${REDISCONFDIR}/${REDISCONFNAME} #configure redis start script cd ${SOFTDIR}/redis-${REDISVERSION}/utils && cp redis_init_script /etc/init.d/redis sed -i '6 a \PROG=$(basename $0)' /etc/init.d/redis sed -i 's#EXEC=/usr/local/bin/redis-server#EXEC=/usr/local/redis-4.0.8/bin/redis-server#g' /etc/init.d/redis sed -i 's#CLIEXEC=/usr/local/bin/redis-cli#CLIEXEC=/usr/local/redis-4.0.8/bin/redis-cli#g' /etc/init.d/redis sed -i 's#CONF="/etc/redis/${REDISPORT}.conf"#CONF="/etc/redis/redis_${REDISPORT}.conf"#g' /etc/init.d/redis chmod u+x /etc/init.d/redis #start redis start /etc/init.d/redis start if [ "`ss -tunlp|grep 6379 |awk -F ":" '{print $2}'|awk '{print $1}'`" == "6379" ];then echo "redis start succefull" else echo "redis start fail" fi echo "export PATH=${REDISPATH}/bin:$PATH" >> /etc/profile source /etc/profile #shutdown redis # vim /etc/init.d/redis # $CLIEXEC -p $REDISPORT -h ${BINDIP} shutdown ~~~ * 第三种:通过saltstack批量安装 ~~~ [admin@master redis]$ cat install.sls include: - pkg.pkg-init redis_dependece: pkg.installed: - names: - jemalloc redis-user-group: group.present: - name: redis - gid: 988 user.present: - name: redis - fullname: redis - shell: /sbin/nologin - uid: 988 - gid: 988 redis-install: file.managed: - name: /usr/local/src/redis-4.0.8.tar.gz - source: salt://redis/files/redis-4.0.8.tar.gz - user: root - group: root - mode: 644 cmd.run: - name: cd /usr/local/src && sudo tar xf redis-4.0.8.tar.gz && cd redis-4.0.8 && sudo make PREFIX=/usr/local/redis install - unless: test -d /usr/local/redis - require: - file: redis-install - pkg: redis_dependece ~~~ ~~~ include: - redis.install /var/log/redis: file.directory: - user: redis - group: redis - mode: 750 - makedirs: True /var/lib/redis: file.directory: - user: redis - group: redis - mode: 750 - makedirs: True redis-configure: file.managed: - name: /etc/redis.conf - source: salt://redis/files/redis.conf - user: redis - group: root - mode: 640 /usr/local/redis/bin/redis-shutdown: file.managed: - source: salt://redis/files/redis-shutdown - user: root - group: root - mode: 755 redis-service: file.managed: - name: /usr/lib/systemd/system/redis.service - source: salt://redis/files/redis.service - user: root - group: root - mode: 644 service.running: - name: redis - enable: True - require: - cmd: redis-install - file: redis-service - file: /usr/local/redis/bin/redis-shutdown - watch: - file: redis-configure ~~~ 第二部分:配置redis单机多实例 多实例安装脚本 ~~~ #!/bin/bash #Description install redis-4.0.8 SOFTDIR=/usr/local/src REDISVERSION=4.0.8 REDISPATH=/usr/local/redis-${REDISVERSION} BINDIP=$(/sbin/ifconfig eth1|grep inet|grep -v inet6|awk '{print $2}') REDISDIR=/data/redis PORT1=6380 PORT2=6381 PORT3=6382 REDISCONFNAME=redis.conf USER=redis GROUP=redis # create redis user if ! id $USER &>/dev/null;then groupadd -g 1211 $GROUP useradd -u 1211 -g $GROUP -s /sbin/nologin $USER fi #create directroy for i in ${PORT1} ${PORT2} ${PORT3};do [ ! -d ${REDISDIR}/$i ] && mkdir -p ${REDISDIR}/$i/{conf,db,log} && chown -R $USER:$GROUP ${REDISDIR}/$i/{db,log} done #install jemalloc yum -y install jemalloc #download redis and install redis cd $SOFTDIR && [ ! -f redis-${REDISVERSION}.tar.gz ] && wget http://download.redis.io/releases/redis-4.0.8.tar.gz [ ! -d redis-${REDISVERSION} ] && tar xf redis-${REDISVERSION}.tar.gz cd redis-${REDISVERSION} && make PREFIX=${REDISPATH} install #copy redis configure file redis for port in ${PORT1} ${PORT2} ${PORT3};do cat > ${REDISDIR}/${port}/conf/redis.conf <<EOF ################################## NETWORK ##################################### bind $BINDIP protected-mode yes port $port tcp-backlog 511 timeout 0 tcp-keepalive 300 ################################# GENERAL ##################################### daemonize yes supervised no pidfile ${REDISDIR}/${port}/redis.pid loglevel notice logfile ${REDISDIR}/${port}/log/redis.log databases 20 ################################ SNAPSHOTTING ################################ save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ${REDISDIR}/${port}/db/ EOF done #configure redis start script for port in ${PORT1} ${PORT2} ${PORT3};do cd ${REDISPATH} && bin/redis-server ${REDISDIR}/${port}/conf/redis.conf done ~~~ 多实例启动脚本 ~~~ #!/bin/sh #chkconfig: 2345 10 90 #description: Startup and stop script for Redis PATH=/usr/local/redis-4.0.8/bin:/usr/local/bin:/sbin:/usr/bin:/bin RPORT1=6380 RPORT2=6381 RPORT3=6382 RPORTS=($RPORT1 $RPORT2 $RPORT3) EXEC=/usr/local/redis-4.0.8/bin/redis-server REDIS_CLI=/usr/local/redis-4.0.8/bin/redis-cli RPIDFILE1=/data/redis/${RPORT1}/redis.pid RPIDFILE2=/data/redis/${RPORT2}/redis.pid RPIDFILE3=/data/redis/${RPORT3}/redis.pid RPIDFILES=($RPIDFILE1 $RPIDFILE2 $RPIDFILE3) BINDIP=$(/sbin/ifconfig eth1|grep inet|grep -v inet6|awk '{print $2}') RCONF1=/data/redis/${RPORT1}/conf/redis.conf RCONF2=/data/redis/${RPORT2}/conf/redis.conf RCONF3=/data/redis/${RPORT3}/conf/redis.conf RCONFS=($RCONF1 $RCONF2 $RCONF3) case "$1" in start) i=0 for PID in ${RPIDFILES[@]} do if [ -e $PID ] then echo "$PID has exist, process is running" else echo "$PID Start ..." $EXEC ${RCONFS[i]} fi if [ "$?" == "0" ] then echo "$PID is running" else echo "$PID is failed to start" fi let i++ done ;; stop) i=0 for PID in ${RPIDFILES[@]} do if [ ! -e $PID ] then echo "$PID does not exist, process is not running" else echo "$PID Stoping ..." $REDIS_CLI -p ${RPORTS[i]} -h $BINDIP SHUTDOWN sleep 2 while [ -e $PID ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "$PID stopped" fi let i++ done ;; restart) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart}" >&2 exit 1 esac ~~~ 三) Redis可执行文件的说明: ~~~ 命令名 命令说明 redis-server redis服务器 redis-cli redis命令行客户端 redis-benchmark redis性能测试工具 redis-check-aof aof文件修复工具 redis-check-dump RDB文件检查工具 redis-sentinel sentinel服务器 ~~~ Redis客户端连接 [admin@huancun01 ~]$ sudo redis-cli -h 192.168.20.144 -p 6379 -a Aa123456 状态回复: ping -->pong 错误回复 hget hello field -> (error) ERR wrong number of arguments for 'hget' command 整数回复 incr hello -> (integer) 1 字符串回复 get sname -> "alex" Redis服务器端常用的配置 * 基础配置 ~~~ bind 绑定到哪个IP地址 port redis对外端口号 pid redis的pid文件 daemonize 是否以守护进程的方式运行yes|no log redis的日志文件 dir redis工作目录 requirepass 设置连接redis的密码 ~~~ 如何要禁止某个命令,可以在配置文件中,操作如下 rename-command FLUSHALL "" rename-command keys "" 这样keys命令、FLUSHALL命令就不能用咯 通用命令: * keys [pattern] 遍历所有的key 192.168.20.144:6379> keys * * exists 检查key是否存在 192.168.20.144:6379> EXISTS myliste (integer) 1 192.168.20.144:6379> EXISTS blogs (integer) 0 * expire key seconds key在seconds秒后过期 192.168.20.144:6379> EXPIRE blog 60 (integer) 1 * ttl key 查看key剩余的过期时间 192.168.20.144:6379> ttl blog (integer) 35 * persist key 去掉key的过期时间 192.168.20.144:6379> PERSIST blog (integer) 1 192.168.20.144:6379> ttl blog (integer) -1 * type key 返回key的类型 192.168.20.144:6379> type blog hash 注意:生产环境中,一般会把keys等一些命令禁止使用 拒绝如下命令 keys flushall flushdb slow lua script mutil/exec operate big value(collection) hgetall