设为首页 收藏本站
查看: 741|回复: 0

[经验分享] Redis 主从搭建

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-12-30 10:02:04 | 显示全部楼层 |阅读模式
目录

一、redis主从库相同操作... 2

1.     创建分用户(用于启动redis)... 2

2.     切换到分用户环境... 2

3.     上传redis软件或者wget http://download.redis.io/releases/redis-3.2.6.tar.gz. 2

4.     解压缩redis文件并安装... 2

二、redis主库操作... 2

1.     修改redis的配置文件,将redis的配置文件修改成如下内容:... 2

2.     创建redis所需的日志文件以及存储数据所需的目录... 3

3.     启动redis. 3

三、redis从库操作... 4

1.     修改redis的配置文件,将redis的配置文件修改成如下内容:... 4

2.     创建redis所需的日志文件以及存储数据所需的目录... 6

3.     启动redis. 6

四、redis主从测试... 7

1.     主库写入数据... 7

2.     从库读取数据... 7

3.     主从库查看日志... 8

4.     主从库磁盘备份数据... 8














一、redis主从库相同操作

    1.       创建分用户(用于启动redis)

[iyunv@Centos6~]# useradd redis

    2.       切换到分用户环境

[iyunv@Centos6~]# sudo su – redis

    3.       上传redis软件或者wget http://download.redis.io/releases/redis-3.2.6.tar.gz

[redis@Centos6~]$ rz -y

rz waitingto receive.

Startingzmodem transfer.  Press Ctrl+C to cancel.

Transferringredis-3.2.6.tar.gz...

  100%   1508 KB    1508 KB/sec    00:00:01       0 Errors

[redis@Centos6~]$ ls

redis-3.2.6.tar.gz

    4.       解压缩redis文件并安装

[redis@Centos6~]$ mkdir run

[redis@Centos6~]$ tar zxf redis-3.2.6.tar.gz –C run/

[redis@Centos6~]$ cd run/redis-3.2.6/

[redis@Centos6redis-3.2.6]$ make
二、redis主库操作

1.      修改redis的配置文件,将redis的配置文件修改成如下内容:

[redis@Centos6~]$ egrep -v "^$|^#" run/redis-3.2.6/redis.conf   

bind 10.0.0.189[l1]

protected-modeyes

port 16001[l2]

tcp-backlog511

timeout 0

tcp-keepalive300

daemonize yes[l3]

supervisedno

pidfile/var/run/redis_6379.pid

loglevelnotice

logfile "/home/redis/log/16001/redis-master.log"[l4]

databases16

save 900 1

save 30010

save 6010000

stop-writes-on-bgsave-erroryes

rdbcompressionyes

rdbchecksumyes

dbfilenamedump.rdb

dir /home/redis/data/16001/[l5]

slave-serve-stale-datayes

slave-read-onlyyes

repl-diskless-syncno

repl-diskless-sync-delay5

repl-disable-tcp-nodelayno

slave-priority100

appendonlyno

appendfilename"appendonly.aof"

appendfsynceverysec

no-appendfsync-on-rewriteno

auto-aof-rewrite-percentage100

auto-aof-rewrite-min-size64mb

aof-load-truncatedyes

lua-time-limit5000

slowlog-log-slower-than10000

slowlog-max-len128

latency-monitor-threshold0

notify-keyspace-events""

hash-max-ziplist-entries512

hash-max-ziplist-value64

list-max-ziplist-size-2

list-compress-depth0

set-max-intset-entries512

zset-max-ziplist-entries128

zset-max-ziplist-value64

hll-sparse-max-bytes3000

activerehashingyes

client-output-buffer-limitnormal 0 0 0

client-output-buffer-limitslave 256mb 64mb 60

client-output-buffer-limitpubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsyncyes

2.      创建redis所需的日志文件以及存储数据所需的目录

[redis@Centos6~]$ mkdir -p data/16001 log/16001

[redis@Centos6~]$ ln -s ~/run/redis-3.2.6 ~/run/redis

3.      启动redis

[redis@Centos6~]$ ~/run/redis/src/redis-server ~/run/redis/redis.conf

[redis@Centos6~]$ netstat-lantup|grep redis

(Not all processes could be identified, non-owned process info

will not be shown, youwould have to be root to see it all.)

tcp     0     0 10.0.0.189:16001    0.0.0.0:*       LISTEN      8981/redis-server 1[l6]

[redis@Centos6~]$ catlog/16001/redis-master.log[l7]

8981:M 24Dec 15:26:44.340 # You requested maxclients of 10000 requiring at least 10032max file descriptors.

8981:M 24Dec 15:26:44.340 # Server can't set maximum open files to 10032 because of OSerror: Operation not permitted.

8981:M 24Dec 15:26:44.340 # Current maximum open files is 4096. maxclients has beenreduced to 4064 to compensate for low ulimit. If you need higher maxclientsincrease 'ulimit -n'.

                _._                                                

           _.-``__ ''-._                                            

      _.-``   `.  `_.  ''-._           Redis 3.2.6 (00000000/0) 64 bit

  .-`` .-```. ```\/    _.,_ ''-._                                   

(   '      ,       .-` | `,    )     Running in standalone mode

|`-._`-...-` __...-.``-._|'` _.-'|     Port: 16001

|   `-._   `._    /    _.-'    |     PID: 8981

  `-._   `-._  `-./  _.-'   _.-'                                   

|`-._`-._   `-.__.-'    _.-'_.-'|                                 

|   `-._`-._        _.-'_.-'    |          http://redis.io      

  `-._   `-._`-.__.-'_.-'    _.-'                                 

|`-._`-._   `-.__.-'    _.-'_.-'|                                 

|   `-._`-._        _.-'_.-'    |                                 

  `-._   `-._`-.__.-'_.-'    _.-'                                 

      `-._   `-.__.-'    _.-'                                      

          `-._        _.-'                                          

              `-.__.-'                                             



8981:M 24Dec 15:26:44.376 # WARNING: The TCP backlog setting of 511 cannot be enforcedbecause /proc/sys/net/core/somaxconn is set to the lower value of 128.

8981:M 24Dec 15:26:44.376 # Server started, Redis version 3.2.6

8981:M 24Dec 15:26:44.377 # WARNING overcommit_memory is set to 0! Background save mayfail under low memory condition. To fix this issue add 'vm.overcommit_memory =1' to /etc/sysctl.conf and then reboot or run the command 'sysctlvm.overcommit_memory=1' for this to take effect.

8981:M 24Dec 15:26:44.377 # WARNING you have Transparent Huge Pages (THP) supportenabled in your kernel. This will create latency and memory usage issues withRedis. To fix this issue run the command 'echo never >/sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your/etc/rc.local in order to retain the setting after a reboot. Redis must berestarted after THP is disabled.

8981:M 24Dec 15:26:44.377 * Theserver is now ready to accept connections on port 16001
三、redis从库操作

1.      修改redis的配置文件,将redis的配置文件修改成如下内容:

[redis@Centos6~]$cp -rf ~/run/redis-3.2.6 ~/run/redis-slave

[redis@Centos6~]$ egrep -v "^$|^#" run/redis-slave/redis.conf  

bind10.0.0.189

protected-modeyes

port 16002

tcp-backlog511

timeout 0

tcp-keepalive300

daemonizeyes

supervisedno

pidfile/var/run/redis_6379.pid

loglevelnotice

logfile"/home/redis/log/16002/redis-slave.log"

databases16

save 900 1

save 30010

save 6010000

stop-writes-on-bgsave-erroryes

rdbcompressionyes

rdbchecksumyes

dbfilenamedump.rdb

dir/home/redis/data/16002/

slaveof 10.0.0.189 16001[l8]

slave-serve-stale-datayes

slave-read-onlyyes

repl-diskless-syncno

repl-diskless-sync-delay5

repl-disable-tcp-nodelayno

slave-priority100

appendonlyno

appendfilename"appendonly.aof"

appendfsynceverysec

no-appendfsync-on-rewriteno

auto-aof-rewrite-percentage100

auto-aof-rewrite-min-size64mb

aof-load-truncatedyes

lua-time-limit5000

slowlog-log-slower-than10000

slowlog-max-len128

latency-monitor-threshold0

notify-keyspace-events""

hash-max-ziplist-entries512

hash-max-ziplist-value64

list-max-ziplist-size-2

list-compress-depth0

set-max-intset-entries512

zset-max-ziplist-entries128

zset-max-ziplist-value64

hll-sparse-max-bytes3000

activerehashingyes

client-output-buffer-limitnormal 0 0 0

client-output-buffer-limitslave 256mb 64mb 60

client-output-buffer-limitpubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsyncyes

2.      创建redis所需的日志文件以及存储数据所需的目录

[redis@Centos6~]$ mkdir -p data/16002 log/16002

3.      启动redis

[redis@Centos6~]$ ~/run/redis-slave/src/redis-server ~/run/redis-slave/redis.conf

[redis@Centos6~]$ netstat -lantup|grep redis

(Not allprocesses could be identified, non-owned process info

will not be shown, you would have to be rootto see it all.)

tcp    0    0 10.0.0.189:16001      0.0.0.0:*        LISTEN     8981/redis-server 1

tcp  0    0 10.0.0.189:16002     0.0.0.0:*          LISTEN     8996/redis-server 1

tcp    0   0 10.0.0.189:57884   10.0.0.189:16001     ESTABLISHED 8996/redis-server 1

tcp  0  0 10.0.0.189:16001   10.0.0.189:57884        ESTABLISHED 8981/redis-server 1[l9]

[redis@Centos6~]$ cat log/16002/redis-slave.log

8990:S 24Dec 15:36:09.108 # You requested maxclients of 10000 requiring at least 10032max file descriptors.

8990:S 24Dec 15:36:09.108 # Server can't set maximum open files to 10032 because of OSerror: Operation not permitted.

8990:S 24Dec 15:36:09.108 # Current maximum open files is 4096. maxclients has beenreduced to 4064 to compensate for low ulimit. If you need higher maxclientsincrease 'ulimit -n'.

                _._                                                

           _.-``__ ''-._                                            

      _.-``   `.  `_. ''-._           Redis 3.2.6(00000000/0) 64 bit

  .-`` .-```. ```\/    _.,_ ''-._                                 

(   '      ,       .-` | `,    )     Running in standalone mode

|`-._`-...-` __...-.``-._|'` _.-'|     Port: 16002

|    `-._   `._   /     _.-'    |    PID: 8990

  `-._   `-._  `-./  _.-'   _.-'                                 

|`-._`-._   `-.__.-'    _.-'_.-'|                                 

|   `-._`-._        _.-'_.-'    |          http://redis.io      

  `-._   `-._`-.__.-'_.-'    _.-'                                 

|`-._`-._   `-.__.-'    _.-'_.-'|                                 

|   `-._`-._        _.-'_.-'    |                                 

  `-._   `-._`-.__.-'_.-'    _.-'                                 

      `-._   `-.__.-'    _.-'                                      

          `-._        _.-'                                          

              `-.__.-'                                               



8990:S 24Dec 15:36:09.231 # WARNING: The TCP backlog setting of 511 cannot be enforcedbecause /proc/sys/net/core/somaxconn is set to the lower value of 128.

8990:S 24Dec 15:36:09.231 # Server started, Redis version 3.2.6

8990:S 24Dec 15:36:09.231 # WARNING overcommit_memory is set to 0! Background save mayfail under low memory condition. To fix this issue add 'vm.overcommit_memory =1' to /etc/sysctl.conf and then reboot or run the command 'sysctlvm.overcommit_memory=1' for this to take effect.

8990:S 24Dec 15:36:09.231 # WARNING you have Transparent Huge Pages (THP) supportenabled in your kernel. This will create latency and memory usage issues withRedis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'as root, and add it to your /etc/rc.local in order to retain the setting aftera reboot. Redis must be restarted after THP is disabled.

8990:S 24Dec 15:36:09.231 * Theserver is now ready to accept connections on port 16002

8990:S 24Dec 15:36:09.232 * Connectingto MASTER 10.0.0.189:16001

8990:S 24Dec 15:36:09.269 * MASTER<-> SLAVE sync started

8990:S 24 Dec 15:36:09.269 * Non blocking connect for SYNC firedthe event.

8990:S 24 Dec 15:36:09.270 * Master replied to PING, replicationcan continue...

8990:S 24 Dec 15:36:09.270 * Partial resynchronization notpossible (no cached master)

8990:S 24 Dec 15:36:09.588 * Full resync from master:f97b8e62d896862a0941aeb66c199a0d0d31b943:1

8990:S 24 Dec 15:36:09.603 * MASTER <-> SLAVE sync: receiving76 bytes from master

8990:S 24 Dec 15:36:09.603 * MASTER <-> SLAVE sync: Flushingold data

8990:S 24 Dec 15:36:09.603 * MASTER <-> SLAVE sync: LoadingDB in memory

8990:S 24 Dec 15:36:09.603 * MASTER <-> SLAVE sync: Finishedwith success[l10]
四、redis主从测试

1.      主库写入数据

[redis@Centos6~]$ ~/run/redis/src/redis-cli -h 10.0.0.189 -p 16001

10.0.0.189:16001>set name abc

OK

10.0.0.189:16001>get name

"abc"

10.0.0.189:16001>

2.      从库读取数据

[redis@Centos6~]$ ~/run/redis/src/redis-cli -h 10.0.0.189 -p 16002

10.0.0.189:16002>get name

"abc"

10.0.0.189:16002> set age 30

(error) READONLY You can't write against a read only slave.[l11]

10.0.0.189:16002>

3.      主从库查看日志

[redis@Centos6~]$ tail log/16001/redis-master.log

8981:M 24Dec 15:36:44.131 * Slave 10.0.0.189:16002 asks for synchronization

8981:M 24Dec 15:36:44.131 * Full resync requested by slave 10.0.0.189:16002

8981:M 24Dec 15:36:44.131 * Starting BGSAVE for SYNC with target: disk

8981:M 24Dec 15:36:44.559 * Background saving started by pid 8999

8999:C 24Dec 15:36:44.616 * DB saved on disk

8999:C 24Dec 15:36:44.616 * RDB: 8 MB of memory used by copy-on-write

8981:M 24Dec 15:36:44.669 * Background saving terminated with success

8981:M 24Dec 15:36:44.669 * Synchronization with slave 10.0.0.189:16002 succeeded

[redis@Centos6~]$ tail log/16002/redis-slave.log

8996:S 24Dec 15:36:44.126 * Connecting to MASTER 10.0.0.189:16001

8996:S 24Dec 15:36:44.131 * MASTER <-> SLAVE sync started

8996:S 24Dec 15:36:44.131 * Non blocking connect for SYNC fired the event.

8996:S 24Dec 15:36:44.131 * Master replied to PING, replication can continue...

8996:S 24Dec 15:36:44.131 * Partial resynchronization not possible (no cached master)

8996:S 24Dec 15:36:44.605 * Full resync from master:f97b8e62d896862a0941aeb66c199a0d0d31b943:43

8996:S 24Dec 15:36:44.669 * MASTER <-> SLAVE sync: receiving 76 bytes from master

8996:S 24Dec 15:36:44.669 * MASTER <-> SLAVE sync: Flushing old data

8996:S 24Dec 15:36:44.669 * MASTER <-> SLAVE sync: Loading DB in memory

8996:S 24Dec 15:36:44.670 * MASTER <-> SLAVE sync: Finished with success

4.      主从库磁盘备份数据

[redis@Centos6~]$ ll data/16001/

total 4

-rw-rw-r--.1 redis redis 76 Dec 24 15:36 dump.rdb

[redis@Centos6~]$ ll data/16002/

total 4

-rw-r--r--.1 redis redis 76 Dec 24 15:36 dump.rdb

[redis@Centos6~]$

[l1]Redis绑定的IP地址

[l2]Redis服务的端口

[l3]开启Daemon启动模式

[l4]Redis的日志文件

[l5]Redis存放DB文件的目录

[l6]查看redis的端口是否已经处于监听状态

[l7]查看redis的启动日志

[l8]指定主库的地址以及端口

[l9]从库端口已经处于监听状态,并且已经与主库建立连接

[l10]同步日志

[l11]无法写入数据,由于是从库




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-321464-1-1.html 上篇帖子: redis单机搭建 下篇帖子: redis.conf配置文件详细讲解(三)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表