linux安装redis

1.检查是否有redis yum 源

yum install redis

 2.安装完毕后,使用下面的命令启动redis服务

# 启动redis
service redis start
# 停止redis
service redis stop
# 查看redis运行状态
service redis status
# 查看redis进程
ps -ef | grep redis

 3.设置redis为开机自动启动

chkconfig redis on

 4.进入redis服务

# 进入本机redis
redis-cli
# 列出所有key
keys *

 5.防火墙开放相应端口

# 开启6379
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
# 开启6380
/sbin/iptables -I INPUT -p tcp --dport 6380 -j ACCEPT
# 保存
/etc/rc.d/init.d/iptables save
# centos 7下执行
service iptables save

 6.修改redis默认端口和密码

# 打开配置文件 
vi /etc/redis.conf
# 修改默认端口,查找 port 6379 修改为相应端口即可
port 6379
# 修改默认密码,查找 requirepass foobared 将 foobared 修改为你的密码
requirepass konglingxi
# 使用配置文件启动 redis
redis-server /etc/redis.conf &
# 使用端口登录,输入redis密码
redis-cli -h 127.0.0.1 -p 6379
auth konglingxi
# 命令方式关闭redis
redis-cli -h 127.0.0.1 -p 6379
shutdown
# 进程号杀掉redis
ps -ef | grep redis
kill -9 XXX

 ps:

1、如果长时间连接不上,可能有两种可能性

  a)bind了127.0.01:只允许在本机连接redis

  b)protected-mode设置了yes(使用redis desktop manager工具需要配置,其余不用)

# 打开redis配置文件
vi /etc/redis.conf
# 找到 bind 127.0.0.1 将其注释
# 找到 protected-mode yes 将其改为
protected-mode no

 2.连接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即可。

3.Could not connect to Redis at 127.0.0.1:6379: Connection refused,在安装好redis扩展 尝试连接redis时,客户端打不开,原因是需要先开启服务端

找到redis.conf 并修改 daemonize no 为 daemonize yes ,这样就可以默认启动就后台运行

原文地址:https://www.cnblogs.com/konglingxi/p/10739077.html