redis常用配置使用经验

安装

tar zxvf redis-2.8.9.tar.gz  
cd redis-2.8.9  
#直接make 编译  
make  
#可使用root用户执行`make install`,将可执行文件拷贝到/usr/local/bin目录下。这样就可以直接敲名字运行程序了。  
make install  

启动

# 服务端
#加上`&`号使redis以后台程序方式运行  
./redis-server &  

#指定配置文件启动redis
redis-server redis.conf
(本机中): redis-server /usr/local/redis-4.0.11/redis.conf


# 客户端
redis-cli

检测

#检测后台进程是否存在  
ps -ef |grep redis  
  
#检测6379端口是否在监听  
netstat -lntp | grep 6379  
  
#使用`redis-cli`客户端检测连接是否正常  
./redis-cli  
127.0.0.1:6379> keys *  
(empty list or set)  
127.0.0.1:6379> set key "hello world"  
OK  
127.0.0.1:6379> get key  
"hello world"  

停止

#使用客户端  
redis-cli shutdown  
#因为Redis可以妥善处理SIGTERM信号,所以直接kill -9也是可以的  
kill -9 PID 

Part II. 通过指定配置文件启动

配置文件

可为redis服务启动指定配置文件,配置文件redis.conf在Redis根目录下。

#修改daemonize为yes,即默认以后台程序方式运行(还记得前面手动使用&号强制后台运行吗)。  
daemonize no  
#可修改默认监听端口  
port 6379  
#修改生成默认日志文件位置  
logfile "/home/futeng/logs/redis.log"  
#配置持久化文件存放位置  
dir /home/futeng/data/redisData  

启动时指定配置文件

redis-server ./redis.conf  
#如果更改了端口,使用`redis-cli`客户端连接时,也需要指定端口,例如:  
redis-cli -p 6380 
#example

fanyuanxiang$ cd ../usr/local 
fanyuanxiangdeMacBook-Air:local fanyuanxiang$ ls
Homebrew			redis-4.0.11

fanyuanxiangdeMacBook-Air:local fanyuanxiang$ redis-server redis-4.0.11/redis.conf 

# 指定文件位置然后指定redis.conf 文件的位置。

其他启停同直接启动方式。配置文件是非常重要的配置工具,随着使用的逐渐深入将显得尤为重要,推荐在一开始就使用配置文件。

Part III. 使用Redis启动脚本设置开机自启动

启动脚本

推荐在生产环境中使用启动脚本方式启动redis服务。启动脚本redis_init_script位于位于Redis的/utils/目录下。

#大致浏览下该启动脚本,发现redis习惯性用监听的端口名作为配置文件等命名,我们后面也遵循这个约定。  
#redis服务器监听的端口  
REDISPORT=6379  
#服务端所处位置,在make install后默认存放与`/usr/local/bin/redis-server`,如果未make install则需要修改该路径,下同。  
EXEC=/usr/local/bin/redis-server  
#客户端位置  
CLIEXEC=/usr/local/bin/redis-cli  
#Redis的PID文件位置  
PIDFILE=/var/run/redis_${REDISPORT}.pid  
#配置文件位置,需要修改  
CONF="/etc/redis/${REDISPORT}.conf"  

配置环境

\1. 根据启动脚本要求,将修改好的配置文件以端口为名复制一份到指定目录。需使用root用户。

mkdir /etc/redis  
cp redis.conf /etc/redis/6379.conf  

\2. 将启动脚本复制到/etc/init.d目录下,本例将启动脚本命名为redisd(通常都以d结尾表示是后台自启动服务)。

cp redis_init_script /etc/init.d/redisd 

\3. 设置为开机自启动

此处直接配置开启自启动chkconfig redisd on将报错误:service redisd does not support chkconfig
参照此篇文章,在启动脚本开头添加如下两行注释以修改其运行级别:

#!/bin/sh  
# chkconfig:   2345 90 10  
# description:  Redis is a persistent key-value database  
#  

再设置即可成功。

#设置为开机自启动服务器  
chkconfig redisd on  
#打开服务  
service redisd start  
#关闭服务  
service redisd stop  

redis中出现的相关异常错误

1.今天运行Redis时发生错误,错误信息如下:

org.springframework.dao.InvalidDataAccessApiUsageException: 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.; nested exception is redis.clients.jedis.exceptions.JedisDataException: 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

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).

Redis被配置为保存数据库快照,但它目前不能持久化到硬盘。用来修改集合数据的命令不能用。请查看Redis日志的详细错误信息。

原因:

强制关闭Redis快照导致不能持久化。

临时解决方案:

运行config set stop-writes-on-bgsave-error no 命令后,关闭配置项stop-writes-on-bgsave-error解决该问题。

root@ubuntu:/usr/local/redis/bin# redis-cli
127.0.0.1:6379> config set stop-writes-on-bgsave-error no
OK
127.0.0.1:6379> lpush myColour "red"
(integer) 1

但是 重点来了

敲黑板啦

上面的解决办法只能治标,最终还要治本

一、 首先到linux 查看redis 的日志文件

  1. 找到redis 配置文件 redis.conf

​ 输入 find . -name "redis.conf" 查找配置文件路径

img

我的在etc 文件夹下 你的就不一定了

打开redis.conf 文件全局搜索 logfile 查看日志文件地址

img

根据地址找到redis.log 打开查找日志打印 我的是这个鬼东西

img

dir /etc/cron.d 这个文件权限不够导致写入错误 好啦找问题之所在了

既然权限不够 就赋予他权限 cron.d 是个文件夹 我就将整个文件夹及里面统一赋予权限755 依然是权限不够 只能在高了777 搞定了

赋予权限的语句是 chmod -R 777 cron.d 权限不懂的话另行百度。

问题是解决了 ,还有个问题是为什么云服务器重启后忽然权限不够了呢???

chmod [可选项] [mode] <file…>

参数说明:
 
[可选项]
  -c, --changes          like verbose but report only when a change is made (若该档案权限确实已经更改,才显示其更改动作)
  -f, --silent, --quiet  suppress most error messages  (若该档案权限无法被更改也不要显示错误讯息)
  -v, --verbose          output a diagnostic for every file processed(显示权限变更的详细资料)
       --no-preserve-root  do not treat '/' specially (the default)
       --preserve-root    fail to operate recursively on '/'
       --reference=RFILE  use RFILE's mode instead of MODE values
  -R, --recursive        change files and directories recursively (以递归的方式对目前目录下的所有档案与子目录进行相同的权限变更)
       --help		显示此帮助信息
       --version		显示版本信息
[mode] 
    权限设定字串,详细格式如下 :
    [ugoa...][[+-=][rwxX]...][,...],
    其中
    [ugoa...]
    u 表示该档案的拥有者,g 表示与该档案的拥有者属于同一个群体(group)者,o 表示其他以外的人,a 表示所有(包含上面三者)。
    [+-=]
    + 表示增加权限,- 表示取消权限,= 表示唯一设定权限。
    [rwxX]
    r 表示可读取,w 表示可写入,x 表示可执行,X 表示只有当该档案是个子目录或者该档案已经被设定过为可执行。
 	
[file...]
    文件列表(单个或者多个文件、文件夹)
数字形式
rwx = 4 + 2 + 1 = 7
rw = 4 + 2 = 6
rx = 4 +1 = 5

示例:

#设置当前目录下的所有档案与子目录皆设为任何人可读写
chmod -R a+rw *
#==
chmod -R 666
#数字对应的位置 :拥有者、群组、其他组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值