Redis安装、主从配置及aof使用

找了02,03,04三台机器,04做主,02做从,03做客户端。

都使用jumbo install redis安装了Redis(server+client)。

在 02 从的 ~/.jumbo/etc/redis.conf 里

slaveof <masterip> 6379

在04 主的 ~/.jumbo/etc/redis.conf 里

appendonly yes

appendfsync everysec

主从都要改:

daemonize yes

logfile "/home/work/.jumbo/var/log/redis/redis.log"

然后先启动主,再启动从,都使用

redis-server (因为deamonize为true,所以不用&)

报如下错误:

7652:C 05 Oct 16:59:37.203 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf

7652:M 05 Oct 16:59:37.207 # Creating Server TCP listening socket *:6379: unable to bind socket

然后发现居然是因为没有指定配置文件:

redis-server ~/.jumbo/etc/redis.conf

这样就可以了。

然后跑到03上,用客户端访问:

redis-cli  -h 10.117.146.16 -p 6379

发现:

Could not connect to Redis at 10.117.146.16:6379: Connection refused
Could not connect to Redis at 10.117.146.16:6379: Connection refused
not connected>

然后也发现从机一直报error log:

36855:S 05 Oct 17:10:57.379 * Connecting to MASTER 10.117.146.16:6379
36855:S 05 Oct 17:10:57.379 * MASTER <-> SLAVE sync started
36855:S 05 Oct 17:10:57.379 # Error condition on socket for SYNC: Connection refused

先把从Redis关了,用客户端:

$ redis-cli
127.0.0.1:6379> shutdown
not connected>
not connected> quit

然后再看Redis配置文件 vi ~/.jumbo/etc/redis.conf

有如下内容:

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

所以,注释掉最后一行。重新启动。

然后又开始报这样的错:

12487:M 05 Oct 17:16:46.170 # Creating Server TCP listening socket *:6379: unable to bind socket

在redis.conf里面加上下面这行:

# bind 127.0.0.1
bind 0.0.0.0

重新启动:redis-server ~/.jumbo/etc/redis.conf

然后 ps xu | grep redis 可以看到:

work     23986  0.0  0.0 79428 2104 ?        Ssl  17:18   0:00 redis-server 0.0.0.0:6379

在03机器上可以访问:

redis-cli -h 10.117.x.x -p 6379
10.117.146.16:6379>

然后可以看到aof文件位置在:

$ ls -la ~/.jumbo/var/lib/redis/appendonly.aof
-rw-r--r-- 1 work work 0 Oct 5 17:06 /home/work/.jumbo/var/lib/redis/appendonly.aof

然后从Redis的日志有:

25089:S 05 Oct 17:22:59.018 * MASTER <-> SLAVE sync started
25089:S 05 Oct 17:22:59.018 * Non blocking connect for SYNC fired the event.
25089:S 05 Oct 17:22:59.019 * Master replied to PING, replication can continue...
25089:S 05 Oct 17:22:59.019 * Partial resynchronization not possible (no cached master)
25089:S 05 Oct 17:22:59.019 * Full resync from master: 64377169e1e5fc029ad8e584e24947a53d4f8fa0:1
25089:S 05 Oct 17:22:59.103 * MASTER <-> SLAVE sync: receiving 76 bytes from master
25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Flushing old data
25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Loading DB in memory
25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Finished with success

主Redis的日志有:

23986:M 05 Oct 17:22:59.020 * Slave 10.117.x.x:6379 asks for synchronization
23986:M 05 Oct 17:22:59.020 * Full resync requested by slave 10.117.146.21:6379
23986:M 05 Oct 17:22:59.020 * Starting BGSAVE for SYNC with target: disk
23986:M 05 Oct 17:22:59.020 * Background saving started by pid 40323
40323:C 05 Oct 17:22:59.076 * DB saved on disk
40323:C 05 Oct 17:22:59.076 * RDB: 0 MB of memory used by copy-on-write
23986:M 05 Oct 17:22:59.104 * Background saving terminated with success
23986:M 05 Oct 17:22:59.104 * Synchronization with slave 10.117.x.x:6379 succeeded

以下是先用客户端访问主,再访问从的结果:

$ redis-cli -h 10.117.146.16 -p 6379
10.117.146.16:6379> zadd page_rank 10 google.com
(integer) 1
10.117.146.16:6379> zadd page_rank 9 baidu.com 8 bing.com
(integer) 2
10.117.146.16:6379> zrange page_rank 0 -1 withscores
1) "bing.com"
2) "8"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"
10.117.146.16:6379> zrange page_rank 0 -1
1) "bing.com"
2) "baidu.com"
3) "google.com"
10.117.146.16:6379> quit

$ redis-cli -h 10.117.146.21 -p 6379
10.117.146.21:6379> zrange page_rank 0 -1 withscores
1) "bing.com"
2) "8"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"
10.117.146.21:6379> zrange page_rank 0 -1
1) "bing.com"
2) "baidu.com"
3) "google.com"
10.117.146.21:6379> quit

而此时查看主和从的日志,都没有明显的日志,因为日志开在了notice级别:

主:40323:C 05 Oct 17:22:59.076 * RDB: 0 MB of memory used by copy-on-write
23986:M 05 Oct 17:22:59.104 * Background saving terminated with success
23986:M 05 Oct 17:22:59.104 * Synchronization with slave 10.117.146.21:6379 succeeded
从:
25089:S 05 Oct 17:22:59.019 * Partial resynchronization not possible (no cached master)
25089:S 05 Oct 17:22:59.019 * Full resync from master: 64377169e1e5fc029ad8e584e24947a53d4f8fa0:1
25089:S 05 Oct 17:22:59.103 * MASTER <-> SLAVE sync: receiving 76 bytes from master
25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Flushing old data
25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Loading DB in memory
25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Finished with success

而aof文件,相比于刚才,已经有了增加:

$ ls -la ~/.jumbo/var/lib/redis/appendonly.aof
-rw-r--r--  1 work work 149 Oct  5 17:28 /home/work/.jumbo/var/lib/redis/appendonly.aof

刚刚是:

$ ls -la ~/.jumbo/var/lib/redis/appendonly.aof
-rw-r--r-- 1 work work 0 Oct 5 17:06 /home/work/.jumbo/var/lib/redis/appendonly.aof

然后通过客户端分别关掉两个server:

$ redis-cli -h 10.117.146.21 -p 6379
10.117.146.21:6379> SHUTDOWN save
not connected> quit
$ redis-cli -h 10.117.146.16 -p 6379
10.117.146.16:6379> SHUTDOWN save
not connected>
not connected> quit

然后再次分别启动,来监测aof文件是否会自动加载。

用客户端连接,发现数据都还在,如下所示:

$ redis-cli -h 10.117.x.x -p 6379
10.117.x.x:6379> zrange page_rank 0 -1 withscores
1) "bing.com"
2) "8"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"
10.117.x.x:6379> 

所以,aof文件是自动加载的。Redis的持久化是生效的。

时间: 2024-10-13 07:33:22

Redis安装、主从配置及aof使用的相关文章

NoSQL -- redis 安装 主从 配置详解 常用命令

Redis 也是key-value存储系统,官方站点 http://redis.io,但相对于memcache,有如下优势: 1.支持更多地value类型(string.hash.lists.sets.sorted sets等): 2.支持数据持久化,预防服务重启后需要重新存储: redis 有两种文件格式:全量数据(RDB=redis database).增量请求(aof=append only file). 前者是将内存中的数据写进磁盘,便于下次读取文件时直接进行加载,快照形式: 后者是将r

redis哨兵主从配置

REDIS哨兵主从配置 环境描述操作系统:CentOS 5.10 x64硬件配置:阿里云8核8G100G硬盘.IP地址:10.253.2.32 [默认主]IP地址:10.253.5.158[默认从]版本号:redis-2.8 Redis安装部署 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集.(difference)等,还支持多种排序功能.所以Redis

redis安装和配置

redis安装和配置 1.安装编译工具 yum install wget? make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel kernel keyutils? patch perl 2.安装tcl组件包(安装Redis需要tcl支持) 下载:http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz 上传tcl8.6.1-src.tar.gz到/usr/local/src

CentOS 下 redis 安装与配置

CentOS 下 redis 安装与配置 1.到官网上找到合适版本下载解压安装 Xml代码   [[email protected] src]# wget -c http://redis.googlecode.com/files/redis-2.4.7.tar.gz [[email protected] src]# tar -zxv -f redis-2.4.7.tar.gz [[email protected] src]# cd /usr/local/src/redis-2.4.7 [[ema

docker Redis的主从配置

redis是k-v型nosql数据库,支持字符串(string).列表(list).集合(set).散列(hash).有序集合(zset:形如member:score的散列集合,其中member为成员,score为成员得分,必须为float型数据). 综合使用redis的以上5种数据类型,可以将redis应用于各种场景,比如点赞.投票网站.消息队列.分布式锁(使用setnx指令,该指令只有在key不存在的时候,才会执行写入操作).文件分发(没研究过).日志记录等等. redis支持主从配置(拓展

DNS安装&主从配置

DNS是什么? DNS 为Domain Name System(域名系统)的缩写,它是一种将ip地址转换成对应的主机名或将主机名转换成与之相对应ip地址的一种服务机制. 其中通过域名解析出ip地址的叫做正向解析,通过ip地址解析出域名的叫做反向解析. DNS使用TCP和UDP, 端口号都是53, 但它主要使用UDP,服务器之间备份使用TCP. 全世界只有13台"根"服务器,1个主根服务器放在美国,其他12台为辅根服务器 DNS服务器根据角色可以分为:主DNS, 从DNS, 缓存DNS服

Redis 安装与配置

Redis 安装与配置

redis学习主从配置

配置slave服务器只需要在配置文件中加入如下配置: slaveof 127.0.0.1 6379 即:slaveof  masterip  masterport redis学习主从配置

从零开始搭建系统1.5——Redis安装及配置

从零开始搭建系统1.5--Redis安装及配置 原文地址:https://www.cnblogs.com/provence666/p/8638528.html

Redis安装与配置Redis安装与配置

今天在使用Redis的时候遇到了一些问题,这个问题的解决,发现很多人使用Redis的时候没有一点安全意识.所以又重温了一下Redis,觉得应该写一下Redis的安全和配置. Redis安装与配置Redis安装与配置 安装 下载,解压,编译: $ wget http://download.redis.io/releases/redis-4.0.10.tar.gz $ tar xzf redis-4.0.10.tar.gz $ mv redis-4.0.10 /usr/local/redis $ c