分享

redis单机多实例

 KILLKISS 2015-08-03

这一篇主要安装单机多实例,以及主从复制的配置。这就是一个redis的集群了。

单机多实例的配置

[root@121 redis_7000]# pwd
/opt/redis-2.8.19/redis_7000
[root@121 redis_7000]# ./src/redis-cli -p 7000
127.0.0.1:7000> ping
PONG
127.0.0.1:7000> shutdown
not connected> quit

先把7000这个端口关闭。

接下来按照redis的官方推荐的方式来安装redis。

执行以下命令,把redis-server脚本和redis-cli脚本放到bin目录中

[root@121 redis_7000]# cp src/redis-server /usr/local/bin/
[root@121 redis_7000]# cp src/redis-cli /usr/local/bin/

接下来建立目录以存放redis的配置文件或是redis数据,

  • mkdir /etc/redis ——redis的配置目录

  • mkdir /var/redis ——redis的存放数据的目录

  • mkdir /var/redis/log ——redis的日志文件的存放目录

  • mkdir /var/redis/run ——redis的pid文件的存放目录

  • mkdir /var/redis/redis_7000 ——redis实例的工作目录

[root@121 redis_7000]# mkdir /etc/redis ——redis的配置目录
[root@121 redis_7000]# mkdir /var/redis ——redis的存放数据的目录
[root@121 redis_7000]# mkdir /var/redis/log ——redis的日志文件的存放目录
[root@121 redis_7000]# mkdir /var/redis/run ——redis pid文件的存放目录
[root@121 redis_7000]# mkdir /var/redis/redis_7000 ——redis实例的工作目录

然后复制配置文件的模板到/etc/redis/目录中,

[root@121 redis_7000]# cp redis.conf /etc/redis/7000.conf

修改配置文件,修改的有以下内容,

  • Set daemonize to yes (by default it is set to no).

  • Set the pidfile to /var/run/redis_6379.pid (modify the port if needed).

  • Change the port accordingly. In our example it is not needed as the default port is already 6379.

  • Set your preferred loglevel.

  • Set the logfile to /var/log/redis_6379.log

  • Set the dir to /var/redis/6379 (very important step!)

# By default Redis does not run as a daemon. Use ‘yes’ if you need it.

# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.

daemonize yes

# When running daemonized, Redis writes a pid file in /var/run/redis.pid by

# default. You can specify a custom pid file location here.

pidfile /var/redis/run/redis_7000.pid

# Accept connections on the specified port, default is 6379.

# If port 0 is specified Redis will not listen on a TCP socket.

port 7000

# Specify the server verbosity level.

# This can be one of:

# debug (a lot of information, useful for development/testing)

# verbose (many rarely useful info, but not a mess like the debug level)

# notice (moderately verbose, what you want in production probably)

# warning (only very important / critical messages are logged)

loglevel notice

# Specify the log file name. Also the empty string can be used to force

# Redis to log on the standard output. Note that if you use standard

# output for logging but daemonize, logs will be sent to /dev/null

logfile /var/redis/log/redis_7000.log

# The working directory.

# The DB will be written inside this directory, with the filename specified

# above using the ‘dbfilename’ configuration directive.

# The Append Only File will also be created inside this directory.

# Note that you must specify a directory here, not a file name.

dir /var/redis/redis_7000

然后redis_init_script脚本放到init.d目录中, 

[root@121 redis_7000]# cp utils/redis_init_script /etc/init.d/redis_7000

然后打开脚本,/etc/init.d/redis_7000

[root@121 redis_7000]# vi /etc/init.d/redis_7000

按照上面的配置修改参数如下,

REDISPORT=7000

EXEC=/usr/local/bin/redis-server

CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/redis/run/redis_${REDISPORT}.pid

CONF=/etc/redis/${REDISPORT}.conf

下面开始启动redis,配置的端口为7000,如下,

[root@121 redis_7000]# /etc/init.d/redis_7000 start
Starting Redis server...
[root@121 redis_7000]# ps -ef | grep redis
root     27449     1  0 15:26 ?        00:00:00 /usr/local/bin/redis-server *:7000              
root     27460 24088  0 15:26 pts/0    00:00:00 grep redis
[root@121 redis_7000]# redis-cli -p 7000
127.0.0.1:7000> ping
PONG
127.0.0.1:7000>

启动成功了。

如何启动多个实例,也就是说一个实例要对应着一个配置文件。如果使用init.d下的脚本的话也要一个实例对应一个脚本。(也可以写成一个启动脚本)

如下,复制脚本,新建目录。

先新建多个实例的目录。

[root@121 redis]# pwd
/var/redis
[root@121 redis]# mkdir redis_7001 redis_7002
[root@121 redis]# mkdir redis_8000 redis_8001 redis_8002
[root@121 redis]# mkdir redis_9000 redis_9001 redis_9002
[root@121 redis]# ls -l
total 44
drwxr-xr-x. 2 root root 4096 Mar 20 14:38 log
drwxr-xr-x. 2 root root 4096 Mar 20 15:22 redis_7000
drwxr-xr-x. 2 root root 4096 Mar 20 15:31 redis_7001
drwxr-xr-x. 2 root root 4096 Mar 20 15:31 redis_7002
drwxr-xr-x. 2 root root 4096 Mar 20 15:31 redis_8000
drwxr-xr-x. 2 root root 4096 Mar 20 15:31 redis_8001
drwxr-xr-x. 2 root root 4096 Mar 20 15:31 redis_8002
drwxr-xr-x. 2 root root 4096 Mar 20 15:32 redis_9000
drwxr-xr-x. 2 root root 4096 Mar 20 15:32 redis_9001
drwxr-xr-x. 2 root root 4096 Mar 20 15:32 redis_9002
drwxr-xr-x. 2 root root 4096 Mar 20 15:26 run

复制配置文件,

[root@121 redis]# pwd
/etc/redis
[root@121 redis]# ls
7000.conf
[root@121 redis]# cp 7000.conf 7001.conf
[root@121 redis]# cp 7000.conf 7002.conf
[root@121 redis]# cp 7000.conf 8000.conf
[root@121 redis]# cp 7000.conf 8001.conf
[root@121 redis]# cp 7000.conf 8002.conf
[root@121 redis]# cp 7000.conf 9000.conf
[root@121 redis]# cp 7000.conf 9001.conf
[root@121 redis]# cp 7000.conf 9002.conf
[root@121 redis]# ls -l
total 324
-rw-r--r--. 1 root root 36202 Mar 20 15:12 7000.conf
-rw-r--r--. 1 root root 36202 Mar 20 15:34 7001.conf
-rw-r--r--. 1 root root 36202 Mar 20 15:34 7002.conf
-rw-r--r--. 1 root root 36202 Mar 20 15:34 8000.conf
-rw-r--r--. 1 root root 36202 Mar 20 15:34 8001.conf
-rw-r--r--. 1 root root 36202 Mar 20 15:34 8002.conf
-rw-r--r--. 1 root root 36202 Mar 20 15:34 9000.conf
-rw-r--r--. 1 root root 36202 Mar 20 15:35 9001.conf
-rw-r--r--. 1 root root 36202 Mar 20 15:35 9002.conf

修改配置文件中成相应的配置。只需要把原来的端口7000,替换成相应的端口,可以使用相应的vim的命令,

如下,全文替换7000为9000。

%s/7000/9000/g

语法为 :[addr]s/源字符串/目的字符串/[option]

全局替换命令为::%s/源字符串/目的字符串/g

[addr] 表示检索范围,省略时表示当前行。

如:“1,20” :表示从第1行到20行;

“%” :表示整个文件,同“1,$”;

“. ,$” :从当前行到文件尾;

s : 表示替换操作

[option] : 表示操作类型

如:g 表示全局替换; 

c 表示进行确认

p 表示替代结果逐行显示(Ctrl + L恢复屏幕);

省略option时仅对每行第一个匹配串进行替换;

如果在源字符串和目的字符串中出现特殊字符,需要用”\”转义

复制启动脚本,

[root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_7001
[root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_7002
[root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_8000
[root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_8001
[root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_8002
[root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_9000
[root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_9001
[root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_9002

启动脚本也要进行相应的替换

替换完成了,把所有的实例都启动起来,如下,

[root@121 redis_7000]# ps -ef |grep 7000
root     27449     1  0 15:26 ?        00:00:00 /usr/local/bin/redis-server *:7000              
root     28424 24088  0 15:59 pts/0    00:00:00 grep 7000
[root@121 redis_7000]# redis-cli -p 7000 shutdown
[root@121 redis_7000]# ps -ef |grep 7000
root     28436 24088  0 15:59 pts/0    00:00:00 grep 7000
[root@121 redis_7000]#

先把原来启动的7000端口杀掉,启动所有的实例,

[root@121 redis_7000]# /etc/init.d/redis_7000 start
Starting Redis server...
[root@121 redis_7000]# /etc/init.d/redis_7001 start
Starting Redis server...
[root@121 redis_7000]# /etc/init.d/redis_7002 start
Starting Redis server...
[root@121 redis_7000]# /etc/init.d/redis_8000 start
Starting Redis server...
[root@121 redis_7000]# /etc/init.d/redis_8001 start
Starting Redis server...
[root@121 redis_7000]# /etc/init.d/redis_8002 start
Starting Redis server...
[root@121 redis_7000]# /etc/init.d/redis_9000 start
Starting Redis server...
[root@121 redis_7000]# /etc/init.d/redis_9001 start
Starting Redis server...
[root@121 redis_7000]# /etc/init.d/redis_9002 start
Starting Redis server...
[root@121 redis_7000]# ps -ef | grep redis
root     28465     1  0 16:00 ?        00:00:00 /usr/local/bin/redis-server *:7000              
root     28470     1  0 16:00 ?        00:00:00 /usr/local/bin/redis-server *:7001              
root     28477     1  0 16:00 ?        00:00:00 /usr/local/bin/redis-server *:7002              
root     28489     1  0 16:01 ?        00:00:00 /usr/local/bin/redis-server *:8000              
root     28500     1  0 16:01 ?        00:00:00 /usr/local/bin/redis-server *:8001              
root     28506     1  0 16:01 ?        00:00:00 /usr/local/bin/redis-server *:8002              
root     28511     1  0 16:01 ?        00:00:00 /usr/local/bin/redis-server *:9000              
root     28516     1  0 16:01 ?        00:00:00 /usr/local/bin/redis-server *:9001              
root     28523     1  0 16:01 ?        00:00:00 /usr/local/bin/redis-server *:9002              
root     28535 24088  0 16:01 pts/0    00:00:00 grep redis

好了,有点小激动,所有的实例都启动成功了。这时可以检查一下配置的日志文件,pid文件,是否都存在,如下,

[root@121 log]# pwd
/var/redis/log
[root@121 log]# ls -l
total 44
-rw-r--r--. 1 root root  768 Mar 20 14:30 redis_6379.log
-rw-r--r--. 1 root root 7015 Mar 20 16:00 redis_7000.log
-rw-r--r--. 1 root root 1864 Mar 20 16:00 redis_7001.log
-rw-r--r--. 1 root root 1864 Mar 20 16:00 redis_7002.log
-rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_8000.log
-rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_8001.log
-rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_8002.log
-rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_9000.log
-rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_9001.log
-rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_9002.log
[root@121 run]# pwd
/var/redis/run
[root@121 run]# ls -l
total 36
-rw-r--r--. 1 root root 6 Mar 20 16:00 redis_7000.pid
-rw-r--r--. 1 root root 6 Mar 20 16:00 redis_7001.pid
-rw-r--r--. 1 root root 6 Mar 20 16:00 redis_7002.pid
-rw-r--r--. 1 root root 6 Mar 20 16:01 redis_8000.pid
-rw-r--r--. 1 root root 6 Mar 20 16:01 redis_8001.pid
-rw-r--r--. 1 root root 6 Mar 20 16:01 redis_8002.pid
-rw-r--r--. 1 root root 6 Mar 20 16:01 redis_9000.pid
-rw-r--r--. 1 root root 6 Mar 20 16:01 redis_9001.pid
-rw-r--r--. 1 root root 6 Mar 20 16:01 redis_9002.pid

嗯,配置也应该没有问题。

这块其实可以写到一个脚本里,有点麻烦也没写过太多的脚本,现就这样吧。

主从复制的配置

下面就来看一下如何进行主从复制的配置。要把7001和7002端口的redis实例配成7000端口的redis的slave,7000端口的redis为master。其他依次类推。

slave 7001

[root@121 run]# redis-cli -p 7001
127.0.0.1:7001> slaveof 127.0.0.1 7000
OK
127.0.0.1:7001> get testkey
(nil)
127.0.0.1:7001> get testkey
"testdata"
127.0.0.1:7001>

slave 7002

[root@121 ~]# redis-cli -p 7002
127.0.0.1:7002> slaveof 127.0.0.1 7000
OK
127.0.0.1:7002> get testkey
(nil)
127.0.0.1:7002> get testkey
"testdata"
127.0.0.1:7002>

master 7000

[root@121 ~]# redis-cli -p 7000
127.0.0.1:7000> get testkey
(nil)
127.0.0.1:7000> set testkey testdata
OK
127.0.0.1:7000>

可以看到主从复制也配置好了。这时候可以到redis实例的文件找一个dump.rdb的文件,这个文件和主从复制有关系。

[root@121 redis_7000]# pwd
/var/redis/redis_7000
[root@121 redis_7000]# ls
dump.rdb

主从复制的原理:

redis不能主主复制

Redis的Replication:

    这里首先需要说明的是,在Redis中配置Master-Slave模式真是太简单了。相信在阅读完这篇Blog之后你也可以轻松做到。这里我们还是先列出一些理论性的知识,后面给出实际操作的案例。

    下面的列表清楚的解释了Redis Replication的特点和优势。

    1). 同一个Master可以同步多个Slaves。

    2). Slave同样可以接受其它Slaves的连接和同步请求,这样可以有效的分载Master的同步压力。因此我们可以将Redis的Replication架构视为图结构。

    3). Master Server是以非阻塞的方式为Slaves提供服务。所以在Master-Slave同步期间,客户端仍然可以提交查询或修改请求。

    4). Slave Server同样是以非阻塞的方式完成数据同步。在同步期间,如果有客户端提交查询请求,Redis则返回同步之前的数据。

    5). 为了分载Master的读操作压力,Slave服务器可以为客户端提供只读操作的服务,写服务仍然必须由Master来完成。即便如此,系统的伸缩性还是得到了很大的提高。

    6). Master可以将数据保存操作交给Slaves完成,从而避免了在Master中要有独立的进程来完成此操作。

    

Replication的工作原理

    在Slave启动并连接到Master之后,它将主动发送一个SYNC命令。此后Master将启动后台存盘进程(redis-cli save),同时收集所有接收到的用于修改数据集的命令,在后台进程执行完毕后,Master将传送整个数据库文件到Slave,以完成一次完全同步。而Slave服务器在接收到数据库文件数据之后将其存盘并加载到内存中。此后,Master继续将所有已经收集到的修改命令,和新的修改命令依次传送给Slaves,Slave将在本次执行这些数据修改命令,从而达到最终的数据同步。

如果Master和Slave之间的链接出现断连现象,Slave可以自动重连Master,但是在连接成功之后,一次完全同步将被自动执行。

===============================END===============================

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多