一般单个redis的进程是不能满足实际需求的,需要在单台服务器上部署多个redis进程,充分发挥cpu的效能,多台服务器上的redis进程将组成庞大的集群,多的一般部署达到数千个redis进程。
暂时不考虑高可用的情况,下面在一台centos6.5上部署三个redis实例,要想实现部署需要修改redis.conf文件,需要修改服务端口号、日志文件编号、rdb文件编号等,下面边查看常用配置边修改。
(1)units单位,定义了基本的度量单位,不区分大小写。
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.
(2)bind设置,如果要让客户端都可以连接到redis服务,就不需要设置绑定的ip,如果客户端只能连上某个或某几个redis服务,bind后需绑定具体的服务ip,如果bind 127.0.0.1,代表只能通过127.0.0.1来访问redis服务,这样只能在提供redis服务的当台机器上访问,其他客户端无法访问,这里先不bind ip。
40 # By default, if no "bind" configuration directive is specified, Redis listens
41 # for connections from all the network interfaces available on the server.
42 # It is possible to listen to just one or multiple selected interfaces using
43 # the "bind" configuration directive, followed by one or more IP addresses.
44 #
45 # Examples:
46 #
47 # bind 192.168.1.100 10.0.0.1
48 # bind 127.0.0.1 ::1
58 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES 59 # JUST COMMENT THE FOLLOWING LINE. 60 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 61 # bind 127.0.0.1
(3)protected-mode默认是开启的,这样就只能本机访问redis,如果想让其他客户端免密也能访问到当前redis服务,就需要设置为no。如果设置为no,配置文件中建议使用bind绑定ip地址,并且客户端需提供密码才能访问redis。
76 # By default protected mode is enabled. You should disable it only if
77 # you are sure you want clients from other hosts to connect to Redis
78 # even if no authentication is configured, nor a specific set of interfaces
79 # are explicitly listed using the "bind" directive.
80 protected-mode no
可以测试使用requirepass 123456来设置密码,然后redis-cli登录后尝试操作redis,发现提示权限不足,需要使用auth 密码命令来获取权限,获取权限后再查看发现可以执行命令,可以看出保护模式关,密码为123456。
# config get 属性名 来获取配置文件内容,发现还是未修改的值127.0.0.1:6379> config get protected-mode
1) "protected-mode"
2) "yes"
127.0.0.1:6379> shutdown
not connected> quit
You have new mail in /var/spool/mail/root
[root@node01 ~]# redis-cli# redis-server redis.conf 加载修改后配置文件启动redis后,使用config get命令会提示权限不足
127.0.0.1:6379> config get protected-mode
(error) NOAUTH Authentication required.# auth 密码 获取权限
127.0.0.1:6379> auth 123456
OK# 保护模式为关
127.0.0.1:6379> config get protected-mode
1) "protected-mode"
2) "no"# 密码为123456
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123456"
这里不设置密码,并且保护模式关。
(4)port 端口号,redis为外提供服务的端口号,默认为6379,如果配置多个实例,需要修改端口号,本次3台端口号分别为6379 6380 6381,此外其他需要区分的文件也使用端口号点缀,如每个redis服务的日志文件,以及rdb持久化文件,还有/var/run/redis_xxxx.pid文件。
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379
(5)timeout 0,如果设置为0,代表客户端不主动断开连接就一直会连接,如果设置了N秒,就会在N秒空闲后断开与redis服务的连接,这里设置为0,节约资源。
106 # Close the connection after a client is idle for N seconds (0 to disable)
107 timeout 0
(6)daemonize默认设置为no,代表启动redis会在前台启动,如果设置为yes就默认在后台启动,这里设置为yes。
128 # By default Redis does not run as a daemon. Use 'yes' if you need it.
129 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
130 daemonize yes
(7)不同的redis进程,会与不同的pid号,这个pid号就保存在这个文件里,不同的redis服务,需要点缀端口号,其他两台设置为redis_6380.pid和redis_6381.pid
150 # Creating a pid file is best effort: if Redis is not able to create it
151 # nothing bad happens, the server will start and run normally.
152 pidfile /var/run/redis_6379.pid
查看一下,当前pid文件里服务进程号为1930,使用ps -ef|grep redis命令查看当前服务进程号也为1930,两者一致。
[root@node01 ~]# cat /var/run/redis_6379.pid
1930
[root@node01 ~]# ps -ef|grep redis
root 1930 1424 0 19:51 pts/0 00:00:02 redis-server *:6379
root 1934 1477 0 19:51 pts/1 00:00:00 redis-cli
root 2084 1524 0 20:01 pts/2 00:00:00 vim redis.conf
root 2508 2477 0 20:14 pts/3 00:00:00 grep redis
(8)logfile指定日志文件名,可以点缀上端口号。日志文件为redis在运行时的记录,内容和前台运行输出的内容一致,如果设置为空字符串,就将日志保存到/dev/null,代表日志文件进入''黑洞'',即不保存。
162 # Specify the log file name. Also the empty string can be used to force
163 # Redis to log on the standard output. Note that if you use standard
164 # output for logging but daemonize, logs will be sent to /dev/null
165 logfile "redis6379.log"
(9)rdb持久化策略,即自动save保存数据到磁盘的频率,数据变动频率越快保存次数越多,测试部署按照默认值来就行,不修改。
191 # In the example below the behaviour will be to save:
192 # after 900 sec (15 min) if at least 1 key changed
193 # after 300 sec (5 min) if at least 10 keys changed
194 # after 60 sec if at least 10000 keys changed
195 #
196 # Note: you can disable saving completely by commenting out all "save" lines.
197 #
198 # It is also possible to remove all the previously configured save
199 # points by adding a save directive with a single empty string argument
200 # like in the following example:
201 #
202 # save ""
203
204 save 900 1 #900秒至少变动1次数据,调用save
205 save 300 10 #300秒至少变动10次数据,调用save
206 save 60 10000 #60秒至少变动10000次数据,调用save
(10)dbfilename指定rdb持久化文件名,需要点缀上端口号,不同的redis服务使用不同的文件名,可以避免redis数据混乱。
238 # The filename where to dump the DB
239 dbfilename dump6379.rdb
注意需要准备三个redis.conf文件,公共配置修改成上面一样,其他如端口号、日志文件名和rdb持久化文件名需要个性化修改,即使用端口号来区分。这里修改完redis.conf文件默认为6379端口号的,其他两台在此基础上vim命令行模式下使用:%s/6379/6380或:%s/6379/6381来批量修改个性化配置,其他公用的一样即可。
(11)修改完后,使用'redis-server redis.conf配置文件'命令来加载配置文件启动服务,三台启动后,使用ps -ef|grep redis来查看是否都正常启动。
[root@node01 /home/software/redis-3.2.11]# redis-server redis6379.conf
[root@node01 /home/software/redis-3.2.11]# redis-server redis6380.conf
[root@node01 /home/software/redis-3.2.11]# redis-server redis6381.conf# 三台均启动,对应不同的端口号
[root@node01 /home/software/redis-3.2.11]# ps -ef|grep redis
root 2912 1 0 20:47 ? 00:00:00 redis-server *:6379
root 2916 1 0 20:47 ? 00:00:00 redis-server *:6380
root 2920 1 0 20:47 ? 00:00:00 redis-server *:6381
root 2924 1424 0 20:47 pts/0 00:00:00 grep redis
以上,就完成了多个redis实例的部署,实现了分布式,接下来客户端连接如果想具体连接哪台就-p指定端口号即可。
[root@node01 ~]# redis-cli -p 6380
127.0.0.1:6380> keys *
(empty list or set)
127.0.0.1:6380> quit
You have new mail in /var/spool/mail/root
[root@node01 ~]# redis-cli -p 6381
127.0.0.1:6381> keys *
(empty list or set)
127.0.0.1:6381> quit
[root@node01 ~]# redis-cli -p 6379
127.0.0.1:6379> get name
(nil)
1381

被折叠的 条评论
为什么被折叠?



