lunix安装Redis

339 阅读4分钟

前言 安装Redis需要知道自己需要哪个版本,有针对性的安装。 比如如果需要redis GEO这个地理集合的特性,那么redis版本就不能低于3.2版本,由于这个特性是3.2版本才有的。 另外需要注意的是,Redis约定次版本号(即第一个小数点后的数字)为偶数的版本是稳定版(如2.8版、3.0版),奇数版本是非稳定版(如2.7版、2.9版),生产环境下一般需要使用稳定版本。 安装Redis官方是这么描述的:Download, extract and compile Redis with: 即:下载,解压,并且编译。 因此文档只做实验尝鲜,故安装官方最新版本redis5.0.5 步骤: 1.下载安装包

[root@CentOS7 opt]# pwd #查看当前目录,源码包下载在此目录下 /opt [root@CentOS7 opt]# wget download.redis.io/releases/re… 2.解压源码包

[root@CentOS7 opt]# tar -zxvf redis-5.0.5.tar.gz #解压会生成一个redis-5.0.5的目录 3.安装GCC依赖

[root@CentOS7 opt]# yum install gcc gcc-c++ #遇到选择,直接输入y即可 4.编译&安装

[root@CentOS7 opt]# cd redis-5.0.5/ #进入到redis目录 [root@CentOS7 redis-5.0.5]# make #等待编译完成即可,未报错即进行下一步 [root@CentOS7 redis-5.0.5]# make install #安装,执行make install的时候会将src下面的几个命令复制到/usr/local/bin/下 5.运行redis

[root@CentOS7 ~]# /opt/redis-5.0.5/src/redis-server #在redis目录下运行 [root@CentOS7 ~]# redis-server #在任意目录下运行,因为redis-server命令在/usr/local/bin目录里面,而该目录又配置在PATH中,所以你可以像执行ls、mkdir等命令的方式去执行redis-cli或者redis-server等命令。 安装后操作 以后台进程方式启动redis 第一步:修改redis.conf文件,redis.conf文件就在redis目录下

① 配置允许所有ip都可以访问redis,在bind 127.0.0.1前加“#”将其注释掉 ② 默认为保护模式,把 protected-mode yes 改为 protected-mode no ③ 默认为不守护进程模式,把daemonize no 改为daemonize yes ④ 将 requirepass foobared前的“#”去掉,密码改为你想要设置的密码(练习设置为123456,即将foobared改为123456) 第二步:指定redis.conf文件启动

[root@CentOS7 redis-5.0.5]# redis-server /opt/redis-5.0.5/redis.conf 第三步:关闭redis进程

[root@CentOS7 redis-5.0.5]# ps -ef |grep redis #ps -aux | grep redis查看redis进程 root 17311 1 0 15:23 ? 00:00:00 redis-server 127.0.0.1:6379 [root@CentOS7 redis-5.0.5]# kill -9 17311 #kill掉redis进程 第四步:检查是否开启了所有Ip访问:

[root@CentOS7 redis-5.0.5]# netstat -lunpt 如果端口号前面显示的是*或者0.0.0.0则说明客户端可以访问了,如果是127.0.0.1,表示只能本机访问,配置文件中没将其注释掉 设置redis开机自启动 1.在/etc目录下新建redis目录

[root@CentOS7 redis-5.0.5]# mkdir -pv /etc/redis 2.将配置文件复制进/etc/redis/下,并命名为6379.conf

[root@CentOS7 redis]# cp /opt/redis-5.0.5/redis.conf /etc/redis/6379.conf 3.创建服务

用service来管理服务的时候,是在/etc/init.d/目录中创建一个脚本文件,来管理服务的启动和停止. 在systemctl中,也类似,文件目录有所不同,在/etc/systemd/system目录下创建一个脚本文件redis.service,里面的内容如下: [Unit] Description=Redis After=network.target

[Service] Type=forking ExecStart=/usr/local/bin/redis-server /etc/redis/6379.conf ExecStop=/usr/locl/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown

RestartSec=10 Restart=always

[Install] WantedBy=multi-user.target 4.刷新配置,让systemctl识别得到

[root@CentOS7 redis]# systemctl daemon-reload 5.启动关闭redis

[root@CentOS7 system]# systemctl start redis #启动redis服务 [root@CentOS7 system]# systemctl stop redis #关闭redis服务 6.设置redis开机启动

[root@CentOS7 system]# systemctl enable redis 总结

今天重启游戏服务器在连接redis数据库时突然报错:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.

究其原因是因为强制把redis快照关闭了导致不能持久化的问题,在网上查了一些相关解决方案,通过stop-writes-on-bgsave-error值设置为no即可避免这种问题。

有两种修改方法,一种是通过redis命令行修改,另一种是直接修改redis.conf配置文件

命令行修改方式示例:

127.0.0.1:6379> config set stop-writes-on-bgsave-error no

修改redis.conf文件:vi打开redis-server配置的redis.conf文件,然后使用快捷匹配模式:/stop-writes-on-bgsave-error定位到stop-writes-on-bgsave-error字符串所在位置,接着把后面的yes设置为no即可。 ———————————————— 版权声明:本文为CSDN博主「Fanrncho」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:blog.csdn.net/qq_31766907…