Ubuntu18.04+Centos7配置redis主从【学习笔记】

一、前提说明及环境设置

说明

主redis(Ubuntu18.04)从redis(Centos7)
IP地址:192.168.1.106IP地址:192.168.1.110

安装环境设置

  1. 两种系统都均需要安装编译器(编译安装需要)
  2. 两种系统安装vim编辑器(非必要,用系统默认的vi也可以)
  3. 两种系统都需要关闭防火墙(如果不关闭需要设置防火墙过滤规则)
  4. 如果不想用编译安装,可以将两系统yum源修改为阿里,进行yum源安装也可以。

二、redis安装

Ubuntu18.04安装redis
这里介绍yum源安装【将源修改为阿里的源】
修改yum源参考

  1. 源安装
wang@wjz:~$ sudo apt-get install redis
  1. 查看redis进程
wang@wjz:~$ ps aux | grep redis
root      45922  0.2  0.1  58556  3996 ?        Ssl  13:46   0:04 redis-server 192.168.1.106:6379
wang      45983  0.0  0.0  21540  1056 pts/2    S+   14:14   0:00 grep --color=auto redis

此时ubuntu上redis安装成功。

Centos7安装redis
这里介绍编译安装

  1. 创建安装目录,将redis下载至该目录,并完成解压
[root@localhost /]# mkdir /software
[root@localhost /]# cd /software/
[root@localhost software]# wget http://download.redis.io/releases/redis-4.0.9.tar.gz
[root@localhost software]# tar -zxvf redis-4.0.9.tar.gz 
[root@localhost software]# ls
redis-4.0.9  redis-4.0.9.tar.gz

2.创建安装目录,将上述解压目录redis-4.0.9复制到该安装目录,进行编译安装

[root@localhost software]# mkdir -p /usr/local/redis
[root@localhost software]# cp redis-4.0.9 /usr/local/redis/
[root@localhost software]# cd /usr/local/redis/
[root@localhost redis]# ls
redis-4.0.9
[root@localhost redis]# cd redis-4.0.9/
[root@localhost redis-4.0.9]# ls
00-RELEASENOTES  CONTRIBUTING  deps     Makefile   README.md   runtest          runtest-sentinel  src    utils
BUGS             COPYING       INSTALL  MANIFESTO  redis.conf  runtest-cluster  sentinel.conf     tests
[root@localhost redis-4.0.9]# make
[root@localhost redis-4.0.9]# make test  [测试编译]
Cleanup: may take some time... OK  [出现这句话表示编译没有问题]
[root@localhost redis-4.0.9]# make install
  1. 返回下载目录,将redis.conf配置文件拷贝到/usr/local/bin下面,之后进入/usr/local/bin/查看该目录是否存在
[root@localhost redis-4.0.9]# cd /software/redis-4.0.9/
[root@localhost redis-4.0.9]# ls
00-RELEASENOTES  CONTRIBUTING  deps     Makefile   README.md   runtest          runtest-sentinel  src    utils
BUGS             COPYING       INSTALL  MANIFESTO  redis.conf  runtest-cluster  sentinel.conf     tests
[root@localhost redis-4.0.9]# cp redis.conf /usr/local/bin/
[root@localhost redis-4.0.9]# cd /usr/local/bin/
[root@localhost bin]# ls
dump.rdb  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis.conf  redis-sentinel  redis-server

三、修改两主机上的redis.conf配置文件

Ubuntu18.04上redis.conf配置文件的修改【主redis】

wang@wjz:~$ sudo vim /etc/redis/redis.conf 
bind 192.168.1.106 127.0.0.1
protected-mode no
port 6379

修改完毕后,进行重启redis(确保防火墙关闭)

wang@wjz:~$ sudo redis-server /etc/redis/redis.conf 

进入redis客户端,添加一些数据

wang@wjz:~$ redis-cli
127.0.0.1:6379> set name Tom
OK
127.0.0.1:6379> set age 23
OK
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> 

Centos7上redis.conf配置文件的修改【从redis】

[root@localhost /]# cd /usr/local/bin/
[root@localhost bin]# ls
dump.rdb  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis.conf  redis-sentinel  redis-server
[root@localhost bin]# vim redis.conf 
bind 192.168.1.110 127.0.0.1
port 6379
# slaveof <masterip> <masterport>
slaveof 192.168.1.106 6379

修改完毕后,启动redis服务

[root@localhost bin]# redis-server redis.conf 
10135:C 02 May 13:47:15.529 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
10135:C 02 May 13:47:15.529 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=10135, just started
10135:C 02 May 13:47:15.529 # Configuration loaded
10135:S 02 May 13:47:15.530 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 10135
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

10135:S 02 May 13:47:15.532 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
10135:S 02 May 13:47:15.532 # Server initialized
10135:S 02 May 13:47:15.532 # 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.
10135:S 02 May 13:47:15.532 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. 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 be restarted after THP is disabled.
10135:S 02 May 13:47:15.532 * DB loaded from disk: 0.000 seconds
10135:S 02 May 13:47:15.532 * Ready to accept connections
10135:S 02 May 13:47:15.532 * Connecting to MASTER 192.168.1.106:6379
10135:S 02 May 13:47:15.532 * MASTER <-> SLAVE sync started
10135:S 02 May 13:47:15.533 * Non blocking connect for SYNC fired the event.
10135:S 02 May 13:47:15.533 * Master replied to PING, replication can continue...
10135:S 02 May 13:47:15.534 * Partial resynchronization not possible (no cached master)
10135:S 02 May 13:47:15.535 * Full resync from master: d570c47a8e1fa630f15eaf2c240cf861fae69740:0
10135:S 02 May 13:47:15.599 * MASTER <-> SLAVE sync: receiving 416 bytes from master
10135:S 02 May 13:47:15.599 * MASTER <-> SLAVE sync: Flushing old data
10135:S 02 May 13:47:15.599 * MASTER <-> SLAVE sync: Loading DB in memory
10135:S 02 May 13:47:15.601 * MASTER <-> SLAVE sync: Finished with success
10135:S 02 May 14:47:36.243 * 1 changes in 900 seconds. Saving...
10135:S 02 May 14:47:36.248 * Background saving started by pid 13844
13844:C 02 May 14:47:36.255 * DB saved on disk
13844:C 02 May 14:47:36.256 * RDB: 6 MB of memory used by copy-on-write
10135:S 02 May 14:47:36.352 * Background saving terminated with success

出现以上信息表示设置从redis成功,然后重新打开一个终端,进入从redis客户端,检查主redis上的信息是否同步到从redis上

[root@localhost bin]# cd /usr/local/bin/
[root@localhost bin]# redis-cli 
127.0.0.1:6379> keys *
1) "name"
2) "age"
127.0.0.1:6379> get name
"Tom"
127.0.0.1:6379> get age
"23"
127.0.0.1:6379> 

发现从redis上有与zhuredis上相同的信息,设置成功。


查看主从关系命令

wang@wjz:~$ redis-cli -h 192.168.1.106 info Replication
# Replication
role:master
connected_slaves:0
master_replid:b89f94ec9e6b115e40d7d815b8f82c507f0cb270
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值