# Centos7 下安装 Redis5.0.8 缓存:Redis 5.0.8 (master-slave) | IP | 端口 | 密码 | 备注 | | ----------------------------------- | ---- | ------------ | ---- | | 10.18.226.160(192.168.10.5)(主) | 6379 | redis0715jhl | | | 10.18.226.167(192.168.10.9)(从) | 6379 | redis0715jhl | | ## 安装redis > 参考: > > * [Redis安装官方指南](https://redis.io/download#installation) > > * https://blog.csdn.net/qq_41891805/article/details/107120311 > * https://blog.51cto.com/11134648/2158209 ### 1.下载压缩包 [下载地址](https://redis.io/download) ![image-20200806111904053](imgs/image-20200806111904053.png) ### 2.解压编译安装 ```shell tar -zxvf redis-5.0.8.tar.gz -C /usr/local/ cd /usr/local/redis-5.0.8 make make install ``` **操作指令默认在/usr/local/bin(可在任何位置访问到)** * Redis-benchmark:性能测试工具(查看本机) * Redis-check-aof:修复有问题的AOF文件 * Redis-check-dump:修复有问题的dump.rdb文件 * Redis-sentinel:Redis集群使用 * redis-server:Redis服务器启动命令 * redis-cli:客户端 ### 3.启动 ``` redis-server ``` ![img](./imgs/Image_20200714170023.jpg) * 修改运行方式为后台运行 ```shell #备份 cp /usr/local/redis-5.0.8/redis.conf /usr/local/redis-5.0.8/redis.conf.backup #编辑 vim /usr/local/redis-5.0.8/redis.conf #修改daemonize no 为 yes,这样运行方式就可以在后台运行 ``` * 启动时指定配置文件 ```shell cd /usr/local/redis-5.0.8 redis-server redis.conf ``` ### 4.关闭 ```shell #服务端关闭方式1:启动redis-cli,执行 shutdown 或者 直接执行 redis-cli shutdown #服务端关闭方式2:杀死进程 netstat -tunlp | grep redis kill -9 进程号 #客户端关闭:exit ``` ### 5. 设置开机启动 参考: https://www.cnblogs.com/skyessay/p/6433349.html https://segmentfault.com/a/1190000017370825 redis 自带一个启动脚本 1) 复制脚本 ``` cp /usr/local/redis-5.0.8/utils/redis_init_script /etc/init.d/redis ``` 2) 复制配置 ``` mkdir /etc/redis cp /usr/local/redis-5.0.8/redis.conf /etc/redis/redis.conf ``` 3) 修改 /etc/init.d/redis `vim /etc/init.d/redis` 该脚本并不复杂,关键一点一定要在 `#!/bin/sh` 下添加2行 其他的目录按照实际安装情况配置即可 ```shell #!/bin/sh # chkconfig: 2345 10 90 # description: Start and Stop redis # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. ### 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/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/etc/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..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT 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 ``` 4)新增服务 ```shell chkconfig --add redis ``` 5)启动redis * `service redis start` 打开redis命令 * `service redis stop` 关闭redis命令 * `chkconfig redis on` 设为开机启动 * `chkconfig redis off` 设为开机关闭 fail ## 主从同步 参考: https://www.cnblogs.com/weibin1/p/10441832.html 配置文件没有移动,还是在 /usr/local/redis-5.0.8/ 目录下 # Windows 下安装 Redis [官网地址](https://redis.io/) 上没有windows版本的redis安装包 1. 下载安装 [github#windows-redis](https://github.com/tporadowski/redis/releases) 或 [github#microsoft-redis](https://github.com/microsoftarchive/redis/releases) 下载 mis 包 2. 运行 ```shell #进入redis安装目录 #启动 redis-server.exe redis.windows.conf ``` 3. 测试 ```shell #在redis安装目录下 redis-cli ``` ## 常见问题 1. redis-server.exe 启动报错 Creating Server TCP listening socket 127.0.0.1:6379: bind: No error 解决方法: 在命令行中运行 redis-cli.exe 127.0.0.1:6379>shutdown not connected>exit 然后重新运行redis-server.exe redis.windows.conf,启动成功! 2. 登录远程redis,执行命令报错,如 Keys * (error) ERR unkown command 'keys'