Redis
最后更新于:2022-04-02 04:00:23
[TOC]
> [phpredis中文手册](http://www.cnblogs.com/ikodota/archive/2012/03/05/php_redis_cn.html)
> [php操作Redis-简书](https://www.jianshu.com/p/02d4d3f91e73)
## 概述
* 速度快
* 丰富的数据结构,除 String外,还有List、Hash、Set、 Sorted set;
* 单线程,避免了线程切换和锁的性能消耗;
* 原子操作;
* 可持久化(RDB与AOF)
* 发布/订阅
* 支持Lua脚本
* 分布式锁
* 事务
* 主从复制与高可用( Redis sentinel)
* 集群(30版本以上)。
## 三种集群模式
主从模式,哨兵模式,cluster(集群)模式
哨兵模式就是主从模式的升级版,该模式下会对响应异常的主节点进行主观下线或者客观下线的操作,并进行主从切换
## 安装
### window
`choco install redis`
### linux
```
$ sudo yum -y install gcc gcc-c++ libstdc++-devel
$ wget http://download.redis.io/releases/redis-6.0.1.tar.gz
$ tar xzf redis-6.0.1.tar.gz
$ cd redis-6.0.1
$ make MALLOC=libc
$ cd src
```
#### 开机启动
1. 设置redis.conf中daemonize为yes,确保后台进行开启
2. 设置开机启动
3. 添加执行权限 ``` chmod 755 /etc/init.d/redis /etc/init.d/redis start chkconfig redis on ``` 4. 测试开机启动 ``` > reboot ... > redis-cli ``` ## 开启 ``` redis-server.exe conf/redis.conf ``` ## 注册成功服务 `redis-server.exe --service-install redis.windows.conf [--service-name Redis] ` ## 命令中测试 ``` > redis-cli.exe -h 127.0.0.1 -p 6379 > set test "hello word" > get test ```
';
vim /etc/init.d/redis
``` #!/bin/bash # chkconfig: 2345 10 90 # description: Start and Stop redis REDISPORT=6379 EXEC=/usr/local/bin/redis-server REDIS_CLI=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_6379.pid CONF="/etc/redis/6379.conf" 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 exists, process is not running." else PID=$(cat $PIDFILE) echo "Stopping..." $REDIS_CLI -p $REDISPORT SHUTDOWN sleep 2 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 ```3. 添加执行权限 ``` chmod 755 /etc/init.d/redis /etc/init.d/redis start chkconfig redis on ``` 4. 测试开机启动 ``` > reboot ... > redis-cli ``` ## 开启 ``` redis-server.exe conf/redis.conf ``` ## 注册成功服务 `redis-server.exe --service-install redis.windows.conf [--service-name Redis] ` ## 命令中测试 ``` > redis-cli.exe -h 127.0.0.1 -p 6379 > set test "hello word" > get test ```