`
webcode
  • 浏览: 5947503 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Redis安装及主从配置

 
阅读更多

Redis是一种面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,适应高并发的应用场景。它起步较晚,发展迅速,目前已被许多大型机构采用,如google、taobao、sina微博等。本文不对其数据结构做深入的讲解,只是介绍其安装及主从结构的配置过程。如需要更详细的了解请参考其官方网站。

Redis的安装

Redis目前只支持Linux及BSD等系统,不支持Window下的安装。在Linux下安装的过程如下所示:

$ wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz

$ tar xzf redis-2.2.12.tar.gz

$ cd redis-2.2.12

$ make

安装成功后的提示为:

Hint: To run 'make test' is a good idea ;)

make[1]: Leaving directory `/usr/alibaba/install/redis-2.2.12/src'

在安装完成后,即可进入至该工程的src下启动服务:

$ src/redis-server

通过内置的build-cli工具进行相应的测试,其测试过程如下:

$ src/redis-cli

redis> set foo bar

OK

redis> get foo

"bar"

redis > getset foo 123

“bar”

redis >get foo

123

Redis的主从配置

机器的分配

主从结构的配置所涉及到的机器及各机器所完成的功能如下所示:

IP地址

角色

10.20.150.205

主结点

10.20.150.208

从结点1

10.20.150.209

从结点2

主从结构的配置

在进行主从结构配置之前,请确保已经在上述三台机器上已经安装了Redis服务。对于Redis的主从结构的配置相对比较简单,只需修改redis.conf文件,配置上主结点对应的IP及端口。具体如下所示:

Port 6380

bind 10.20.150.205

slaveof 10.20.150.205 6379

l port 6380表示当前该Redis服务对应的端口号;

l bind 10.20.150.205 表示当前Redis绑定的主结点的IP地址;

l slaveof 10.20.150.205 6379 表示当前Redis是哪个主结点的从结点,其中包括IP及端口号。

当主结点在启动时

当只有一台主结点在启动时,会显示如下的信息:

[29028] 26 Sep 18:39:12 * Server started, Redis version 2.2.12

[29028] 26 Sep 18:39:12 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

[29028] 26 Sep 18:39:12 * DB loaded from disk: 0 seconds

[29028] 26 Sep 18:39:12 * The server is now ready to accept connections on port 6379

[29028] 26 Sep 18:39:12 - 0 clients connected (0 slaves), 790584 bytes in use

当有一台从结点10.20.150.208启动时

其中主结点会自动检测到有新的从结点启动并加入进来,其显示的信息如下:

[29028] 26 Sep 18:40:31 - Accepted 10.20.150.208:44779

[29028] 26 Sep 18:40:31 * Slave ask for synchronization

[29028] 26 Sep 18:40:31 * Starting BGSAVE for SYNC

[29028] 26 Sep 18:40:31 * Background saving started by pid 29091

[29091] 26 Sep 18:40:31 * DB saved on disk

[29028] 26 Sep 18:40:31 * Background saving terminated with success

[29028] 26 Sep 18:40:31 * Synchronization with slave succeeded

[29028] 26 Sep 18:40:32 - 0 clients connected (1 slaves), 798752 bytes in use

[29028] 26 Sep 18:40:37 - 0 clients connected (1 slaves), 798752 bytes in use

[29028] 26 Sep 18:40:42 - 0 clients connected (1 slaves), 798752 bytes in use

[29028] 26 Sep 18:40:47 - 0 clients connected (1 slaves), 798752 bytes in use

对于从结点显示的信息如下:

#[597] 26 Sep 18:40:29 * Server started, Redis version 2.2.12

[597] 26 Sep 18:40:29 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

[597] 26 Sep 18:40:29 * The server is now ready to accept connections on port 6379

[597] 26 Sep 18:40:29 - 0 clients connected (0 slaves), 790616 bytes in use

[597] 26 Sep 18:40:29 * Connecting to MASTER...

[597] 26 Sep 18:40:29 * MASTER <-> SLAVE sync started: SYNC sent

[597] 26 Sep 18:40:29 * MASTER <-> SLAVE sync: receiving 10 bytes from master

[597] 26 Sep 18:40:29 * MASTER <-> SLAVE sync: Loading DB in memory

[597] 26 Sep 18:40:29 * MASTER <-> SLAVE sync: Finished with success

[597] 26 Sep 18:40:34 - 1 clients connected (0 slaves), 798744 bytes in use

当有另外一台从机10.20.150.209挂载时

当有另一台从机挂载至主机时,主机显示的信息如下:

[29028] 26 Sep 18:42:52 - 0 clients connected (1 slaves), 798752 bytes in use

[29028] 26 Sep 18:42:57 - 0 clients connected (1 slaves), 798752 bytes in use

[29028] 26 Sep 18:42:58 - Accepted 10.20.150.209:47650

[29028] 26 Sep 18:42:58 * Slave ask for synchronization

[29028] 26 Sep 18:42:58 * Starting BGSAVE for SYNC

[29028] 26 Sep 18:42:58 * Background saving started by pid 29287

[29287] 26 Sep 18:42:58 * DB saved on disk

[29028] 26 Sep 18:42:59 * Background saving terminated with success

[29028] 26 Sep 18:42:59 * Synchronization with slave succeeded

[29028] 26 Sep 18:43:02 - 0 clients connected (2 slaves), 806920 bytes in use

从另一台从机上可以看到的信息如下:

[Intranet root@xasc-150-209 /usr/alibaba/install/redis-2.2.12/src]

#./redis-server ../redis.conf

[15034] 26 Sep 18:42:56 * Server started, Redis version 2.2.12

[15034] 26 Sep 18:42:56 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

[15034] 26 Sep 18:42:56 * The server is now ready to accept connections on port 6379

[15034] 26 Sep 18:42:56 - 0 clients connected (0 slaves), 790616 bytes in use

[15034] 26 Sep 18:42:56 * Connecting to MASTER...

[15034] 26 Sep 18:42:56 * MASTER <-> SLAVE sync started: SYNC sent

[15034] 26 Sep 18:42:56 * MASTER <-> SLAVE sync: receiving 10 bytes from master

[15034] 26 Sep 18:42:56 * MASTER <-> SLAVE sync: Loading DB in memory

[15034] 26 Sep 18:42:56 * MASTER <-> SLAVE sync: Finished with success

[15034] 26 Sep 18:43:01 - 1 clients connected (0 slaves), 798744 bytes in use

当主结点宕掉时

当主结点突然间不可用,两台从机会由于找不到主结点而报错,信息如下:

[597] 26 Sep 18:46:09 * Connecting to MASTER...

[597] 26 Sep 18:46:09 # Unable to connect to MASTER: Connection refused

[597] 26 Sep 18:46:10 * Connecting to MASTER...

[597] 26 Sep 18:46:10 # Unable to connect to MASTER: Connection refused

[597] 26 Sep 18:46:11 * Connecting to MASTER...

[597] 26 Sep 18:46:11 # Unable to connect to MASTER: Connection refused

[597] 26 Sep 18:46:12 * Connecting to MASTER...

[597] 26 Sep 18:46:12 # Unable to connect to MASTER: Connection refused

[597] 26 Sep 18:46:13 * Connecting to MASTER...

[597] 26 Sep 18:46:13 # Unable to connect to MASTER: Connection refused

[597] 26 Sep 18:46:14 - 0 clients connected (0 slaves), 790616 bytes in use

主结点在没有启动的情况下,从机先启动时

当主结点还没有启动时,从结点先启动,由于找不到对应主结点,会报如下的信息错误:

[Intranet root@xasc-150-208 /usr/alibaba/install/redis-2.2.12/src]

#./redis-server ../redis.conf

[1842] 26 Sep 18:48:50 * Server started, Redis version 2.2.12

[1842] 26 Sep 18:48:50 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

[1842] 26 Sep 18:48:50 * DB loaded from disk: 0 seconds

[1842] 26 Sep 18:48:50 * The server is now ready to accept connections on port 6379

[1842] 26 Sep 18:48:50 - 0 clients connected (0 slaves), 790616 bytes in use

[1842] 26 Sep 18:48:50 * Connecting to MASTER...

[1842] 26 Sep 18:48:50 # Unable to connect to MASTER: Connection refused

[1842] 26 Sep 18:48:51 * Connecting to MASTER...

[1842] 26 Sep 18:48:51 # Unable to connect to MASTER: Connection refused

[1842] 26 Sep 18:48:52 * Connecting to MASTER...

[1842] 26 Sep 18:48:52 # Unable to connect to MASTER: Connection refused

[1842] 26 Sep 18:48:53 * Connecting to MASTER...

[1842] 26 Sep 18:48:53 # Unable to connect to MASTER: Connection refused

[1842] 26 Sep 18:48:54 * Connecting to MASTER...

[1842] 26 Sep 18:48:54 # Unable to connect to MASTER: Connection refused

[1842] 26 Sep 18:48:55 - 0 clients connected (0 slaves), 790616 bytes in use

当有一台从机不可用时

当有一台从机变得不可用时,主机显示的信息如下:

32 - 1 clients connected (0 slaves), 798744 bytes in use

[16848] 26 Sep 18:50:37 - 1 clients connected (0 slaves), 798744 bytes in use

[16848] 26 Sep 18:50:42 - 1 clients connected (0 slaves), 798744 bytes in use

当两台从机都宕掉时

当两台从机都变得不可用时,主机显示的信息如下:

[29736] 26 Sep 18:52:57 - Client closed connection

[29736] 26 Sep 18:53:02 - 0 clients connected (0 slaves), 790584 bytes in use

[29736] 26 Sep 18:53:07 - 0 clients connected (0 slaves), 790584 bytes in use

主从结构的测试

在主结点上执行写入

在主机上执行写入命令1

redis> set foo bar

在各个从机上会显示的信息如下:

[2528] 26 Sep 18:57:01 - DB 0: 1 keys (0 volatile) in 4 slots HT.

[2528] 26 Sep 18:57:01 - 1 clients connected (0 slaves), 798952 bytes in use

[2528] 26 Sep 18:57:06 - DB 0: 1 keys (0 volatile) in 4 slots HT.

[2528] 26 Sep 18:57:06 - 1 clients connected (0 slaves), 798936 bytes in use

在主机上执行get foo命令,显示的信息如下:

redis 127.0.0.1:6379> get foo

"bar"

在从机10.20.150.208上执行get foo命令,显示的信息如下:

redis 127.0.0.1:6379> get foo

"bar"

同理,在从机10.20.150.209上执行get foo命令,显示的信息如下:

redis 127.0.0.1:6379> get foo

"bar"

当有一台从机挂掉后,在主机上写入

当有一台从机(10.20.150.208)宕机,在主机上执行setfoo1写入命令。

主机上执行get foo1时显示的信息如下:

redis 127.0.0.1:6379> get foo1

"test"

另一台从机上执行getfoo1时显示的信息如下:

redis 127.0.0.1:6379> get foo1

"test"

当把10.20.150.208从机启动起来时,执行getfoo1命令,显示的信息如下:

redis 127.0.0.1:6379> get foo1

"test"

可见,即使当从机挂掉并再次启动时,仍会将主机做过的修改全部都同步至重启的从机上。

当在一台从机上执行写入

另外在一台从机上执行的写入操作,是不会同步至主机及其它从机上的。在一台从机10.20.150.209上执行set foo2 test2命令,在此从机上执行get foo2命令会得到如下的结果:

redis 127.0.0.1:6379> get foo2

"test2"

在主机上执行getfoo2命令时,则会得到如下结果:

redis 127.0.0.1:6379> get foo2

(nil)

同样,在另外一台从机10.20.150.208上执行get foo2命令时,会得到如下结果:

redis 127.0.0.1:6379> get foo2

(nil)

由此可以得出在从机上所做的任何写入操作,只对本台从机起作用,不会被同步至主机及其它从机的。

Redis的配置详解

配置名称

可选值/推荐值

说明

daemonize

yes/no

如果选择yes,则说明redis以看守进程的方式运行在系统的后台。通过ps –ef |grep redis-server可以查看redis服务是否正常。

pidfile

/var/run/redis.pid

Pid文件的全路径信息

port

6379

Redis服务的默认端口

bind

0.0.0.0

Redis服务绑定的Ip地址,如果填写0.0.0.0,则绑定本地所有的Ip地址。

timeout

86400

客户端和Redis服务之间空闲的最大时间,超过这个时间,连接将会断开。配置的时候需要根据客户端的需求进行配置

loglevel

debug, verbose, notice, warning

debug:开发和测试情况下配置为此选项

verbose:无调试信息,会输出notice消息和warning消息等。

notice:比verbose较少的信息

warning:只有比较重要的信息会输出

logfile

stdout或者日志文件名称

指明日志写入到哪个文件中去,如果在daemonize模式下,日志将不会输出到任何地方;否则输出到标准输出或者日志文件中。

database

0

配置数据库的数量。如果为0的话,只配置一个数据库。数据库的索引从0到database-1

save <seconds> <changes>

save 900 1

多少秒内至少有多少个key改变的情况下存盘。可以多个规则进行组合。譬如:

save 900 1

save 300 10

save 60 10000

如果在60秒内有10000个或以上的key发生变化,就存盘;

如果在300秒内有10个key变化就存盘;

如果在900秒内有1个key变化就存盘。

rdbcompression

yes/no

是否支持rdb数据文件压缩,如果不支持的话,产生的rdb数据文件将比采用压缩方式的要大。如果选择no,则暂用cpu要减小。

dir

./

表示数据库文件存贮到指定的目录下。这里一定要填写一个目录名称。

slaveof <masterip> <masterport>

该选项用于实现主从方式的redis,其中masterip和masterport填写主redis服务的服务ip和端口号。

masterauth <master-password>

如果本配置作为一个从redis服务的配置,而且主redis服务需要进行密码认证,则这里需要填写主redis服务的密码信息,即requirepass中填写的数值。

requirepass

如果本配置是作为一个主redis服务的配置,而且需要提供认证机制,则该配置项可以提供该功能。所有的从redis服务都需要在masterauth中填写相应的密码。建议该处的密码信息是一个较长的不易破解的密码。

Maxclients

限制能够同时在线的客户端的数量。

Maxmemory

设置redis最多消耗的内存字节数;如果超过指定的字节数,redis会尝试释放过期的key;如果释放失败,则所有的写失败,读可以继续。对于一些可以预见的应用,可以设置该项;对于一些不可预见的应用,不建议设置该项内容。

appendonly

yes/no

在每次变更的时候是否写AOF日志。如果设为yes,则AOF日志会写入到日志文件中,一段遇到掉电的情况,可以通过日志文件恢复数据;如果对此不敏感,则不需要打开该选项

Appendfilename

如果appendonly设置为yes,则这里需要设置AOF文件的名称

appendfsync

no/ always/everysec

no:由操作系统决定什么时候将日志同步到磁盘;

always:比较慢,最安全

everysec:介于上面两种选项的一个折衷的方法。

vm-enabled

yes/no

如果内存的需求比实际的内存大的话,可以选择yes选项,采用虚拟内存来充当内存;否则采用no选项。

vm-swap-file

虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享

vm-max-memory

将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的(Redis的索引数据就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0。

vm-page-size

32、64或其它

每页占用的字节数。如果你们数据的尺寸比较小,可以选择32或者64,如果数据比较大,那么建议采用一个较大的数据来描述。本参数对启用VM机制有用。

vm-pages

页面的最大数量,对于一个4G的swap文件来说,这个数值最大为4G/ vm-page-size得到的整数值。

vm-max-threads

可以设置访问swap文件的线程数,设置最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的.可能会造成比较长时间的延迟,但是对数据完整性有很好的保证.

glueoutputbuf

把小的输出缓存放在一起,以便能够在一个TCP packet中为客户端发送多个响应,具体原理和真实效果我不是很清楚。所以根据注释,你不是很确定的时候就设置成yes

hash-max-zipmap-entries

在redis 2.0中引入了hash数据结构。当hash中包含超过指定元素个数并且最大的元素没有超过临界时,hash将以一种特殊的编码方式(大大减少内存使用)来存储,这里可以设置这两个临界值

activerehashing

yes/no

开启之后,redis将在每100毫秒时使用1毫秒的CPU时间来对redis的hash表进行重新hash,可以降低内存的使用。当你的使用场景中,有非常严格的实时性需要,不能够接受Redis时不时的对请求有2毫秒的延迟的话,把这项配置为no。如果没有这么严格的实时性要求,可以设置为yes,以便能够尽可能快的释放内存


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics