Redis入门:分布式存储
要完成数据的分片存储,需要多个redis实例。
1 多个REDIS实例
前面的单个redis节点实例的启动时默认配置端口号6379。
1.1 配置文件
配置文件位置:/redis根目录/redis.conf。
修改配置文件,使用vim命令进行编辑:
代码语言:javascript复制vim redis.conf
1.1.1 内存分配
一个redis实例默认占用所有物理内存,在实际使用中需要限制大小。这里为了简化操作,不做内存占用的配置,使用默认即可。
从下面这一部分中,就能看出内存配置的方式和配置文件的使用方式,如果要使用配置文件,需要在启动的时候将配置文件作为启动命令的第一个参数。
内存配置写法不同代表的大小也不同。参照11行到16行。
以下为本段配置内容详情:
代码语言:javascript复制 1 # Redis configuration file example.
2 #
3 # Note that in order to read the configuration file, Redis must be
4 # started with the file path as first argument:
5 #
6 # ./redis-server /path/to/redis.conf
7
8 # Note on units: when memory size is needed, it is possible to specify
9 # it in the usual form of 1k 5GB 4M and so forth:
10 #
11 # 1k => 1000 bytes
12 # 1kb => 1024 bytes
13 # 1m => 1000000 bytes
14 # 1mb => 1024*1024 bytes
15 # 1g => 1000000000 bytes
16 # 1gb => 1024*1024*1024 bytes
17 #
18 # units are case insensitive so 1GB 1Gb 1gB are all the same.
19
1.1.2 绑定ip
这个版本的配置文件中,绑定IP的示例从60行到62行都是。
绑定的配置在75行。
以下为本段配置内容详情:
代码语言:javascript复制 46 ################################## NETWORK #####################################
47
48 # By default, if no "bind" configuration directive is specified, Redis listens
49 # for connections from all available network interfaces on the host machine.
50 # It is possible to listen to just one or multiple selected interfaces using
51 # the "bind" configuration directive, followed by one or more IP addresses.
52 # Each address can be prefixed by "-", which means that redis will not fail to
53 # start if the address is not available. Being not available only refers to
54 # addresses that does not correspond to any network interfece. Addresses that
55 # are already in use will always fail, and unsupported protocols will always BE
56 # silently skipped.
57 #
58 # Examples:
59 #
60 # bind 192.168.1.100 10.0.0.1 # listens on two specific IPv4 addresses
61 # bind 127.0.0.1 ::1 # listens on loopback IPv4 and IPv6
62 # bind * -::* # like the default, all available interfaces
63 #
64 # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
65 # internet, binding to all the interfaces is dangerous and will expose the
66 # instance to everybody on the internet. So by default we uncomment the
67 # following bind directive, that will force Redis to listen only on the
68 # IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
69 # will only be able to accept client connections from the same host that it is
70 # running on).
71 #
72 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
73 # JUST COMMENT OUT THE FOLLOWING LINE.
74 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75 bind 127.0.0.1 -::1
76
1.1.3 保护模式
保护模式不启动,在实际工作中为了安全是要启动的,做测试的过程为了避免麻烦,可以不启动。
配置项在94行,只需要更改值即可,yes表示开启,no表示不开启。
以下为本段配置详情:
代码语言:javascript复制 90 # By default protected mode is enabled. You should disable it only if
91 # you are sure you want clients from other hosts to connect to Redis
92 # even if no authentication is configured, nor a specific set of interfaces
93 # are explicitly listed using the "bind" directive.
94 protected-mode no
1.1.4 默认端口
6379是默认端口(要启动其他的redis实例需要修改端口)。
98行为配置信息,port后为要使用的端口号。
以下是本段配置信息:
代码语言:javascript复制 96 # Accept connections on the specified port, default is 6379 (IANA #815344).
97 # If port 0 is specified Redis will not listen on a TCP socket.
98 port 6379
1.1.5 连接超时
当客户端空闲时间达到一小时,就会自动断开连接。0秒表示不启用超时配置。
119行为此项目配置内容,此项的配置单位为秒,如果启用配置为对应的秒数即可。
以下为本段配置内容详情,这里本人配置了1分钟:
代码语言:javascript复制 118 # Close the connection after a client is idle for N seconds (0 to disable)
119 # timeout 0
120 timeout 60
1.1.6 后台运行
daemonize设置成yes让redis服务器启动由守护进程管理,使其后台执行,不占用控制台。其配置在257行。
内容如下:
代码语言:javascript复制 252 ################################# GENERAL #####################################
253
254 # By default Redis does not run as a daemon. Use 'yes' if you need it.
255 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
256 # When Redis is supervised by upstart or systemd, this parameter has no impact.
257 daemonize yes
1.1.7 pid文件
对应不同的redis实例,pid的文件名称需要和端口同名。配置在289行。
内容如下:
代码语言:javascript复制 277 # If a pid file is specified, Redis writes it where specified at startup
278 # and removes it at exit.
279 #
280 # When the server runs non daemonized, no pid file is created if none is
281 # specified in the configuration. When the server is daemonized, the pid file
282 # is used even if not specified, defaulting to "/var/run/redis.pid".
283 #
284 # Creating a pid file is best effort: if Redis is not able to create it
285 # nothing bad happens, the server will start and run normally.
286 #
287 # Note that on modern Linux systems "/run/redis.pid" is more conforming
288 # and should be used instead.
289 pidfile /var/run/redis_6379.pid
1.1.8 持久化规则
- save 900 1 当900秒以内,至少有1条数据变动,可使flush保存数据到文件。
- save 300 10 当300秒以内,至少10条数据变动,保存文件。
- save 60 10000 当60秒内,有10000条数据变动,保存文件。
保持默认规则即可,也可按照实际需求进行配置。
本人这里配置了第三种,具体内容如下:
代码语言:javascript复制360 ################################ SNAPSHOTTING ################################
361
362 # Save the DB to disk.
363 #
364 # save <seconds> <changes>
365 #
366 # Redis will save the DB if both the given number of seconds and the given
367 # number of write operations against the DB occurred.
368 #
369 # Snapshotting can be completely disabled with a single empty string argument
370 # as in following example:
371 #
372 # save ""
373 #
374 # Unless specified otherwise, by default Redis will save the DB:
375 # * After 3600 seconds (an hour) if at least 1 key changed
376 # * After 300 seconds (5 minutes) if at least 100 keys changed
377 # * After 60 seconds if at least 10000 keys changed
378 #
379 # You can set these explicitly by uncommenting the three following lines.
380 #
381 # save 3600 1
382 # save 300 100
383 save 60 10000
1.2 其他实例
将刚配置好的配置文件复制2份,分别修改端口号和pid文件名即可。
代码语言:javascript复制cp redis.conf redis6380.conf
cp redis.conf redis6381.conf
将拷贝的文件中只修改与端口有关内容port和pid的文件名。
2 启动REDIS多实例
启动多实例的命令如下:
代码语言:javascript复制redis-server redis.conf(指定启动文件)
启动另外两个节点:
代码语言:javascript复制#redis-server redis6380.conf
#redis-server redis6381.conf
#ps -ef|grep redis
root 2822 1 0 15:53 ? 00:00:00 redis-server 127.0.0.1:6379
root 2836 1 0 15:53 ? 00:00:00 redis-server 127.0.0.1:6380
root 2848 1 0 15:53 ? 00:00:00 redis-server 127.0.0.1:6381
root 2868 1697 0 15:53 pts/1 00:00:00 grep --color=auto redis
3 登录客户端
指定端口登录客户端redis-cli -p [端口号]
代码语言:javascript复制#默认登录
[root@lk7 bin]# redis-cli
127.0.0.1:6379> exit
[root@lk7 bin]# redis-cli -p 6379
127.0.0.1:6379> exit
[root@lk7 bin]# redis-cli -p 6380
127.0.0.1:6380> exit
[root@lk7 bin]# redis-cli -p 6381
127.0.0.1:6381> exit
6380和6381会共享6379的dump.db文件,所以不同的节点实例在同一个机器上运行时,可以修改dump.db指定端口文件。