一、下载源码包并解压编译
redis官网地址:http://www.redis.io/
tar xf redis-stable.tar.gz
cd redis-stable
make
编译完成后,在src目录下,有3个可执行文件redis-server、redis-benchmark、redis-cli。然后拷贝到redis目录下。
mkdir /usr/local/redis
cp redis-server redis-benchmark redis-cli /usr/local/redis
拷贝配置文件到redis目录下
cp redis.conf /usr/local/redis
二、启动与关闭服务
启动redis服务:
cd /usr/local/redis
./redis-server ./redis.conf
不加redis.conf则使用安装目录的默认配置文件启动
后台启动要把daemonize改为yes
关闭redis服务:
redis-cli shutdown(有密码的情况为:redis-cli shutdown -a 密码 shutdown)
三、客户端连接及基本操作
本地连接:redis-cli
远程连接:./redis-cli -h host -p port -a password
(远程连接需关闭连接限制:编辑redis.conf,找到bind 127.0.0.1注释掉)
redis默认是无需密码登录的,开启密码需编辑redis.conf,添加requirepass "密码"
查询所有的key:keys *
查询当前版本:info
四、遇到的一些问题
1、浏览器访问应用服务时,报Could not get a resource from the pool ,网上有很多报错原因和解决方法,需要对应的情况才能有效解决。这里最终是发现,应用程序中的redis连接配置文件中指定的ip地址还是原内网redis, 修改该ip为新的redis服务器地址,问题解决。
2、报错:(error) 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快照导致不能持久化。
解决方法:127.0.0.1:6379> config set stop-writes-on-bgsave-error no
3、主从复制不成功,从库报错:Opening the temp file needed for MASTER <-> SLAVE synchronization: Permission denied
原因:多半是因为从库上启动redis-server时不是root用户启动,而普通用户无法对redis目录进行写操作,导致无法在redis目录产生dump.rdb
解决方法:
1、从库上用root启动redis
2、更改redis目录为启动用户可写