天天看點

Ubuntu18.04+Centos7配置redis主從【學習筆記】

一、前提說明及環境設定

說明

主redis(Ubuntu18.04) 從redis(Centos7)
IP位址:192.168.1.106 IP位址:192.168.1.110

安裝環境設定

  1. 兩種系統都均需要安裝編譯器(編譯安裝需要)
  2. 兩種系統安裝vim編輯器(非必要,用系統預設的vi也可以)
  3. 兩種系統都需要關閉防火牆(如果不關閉需要設定防火牆過濾規則)
  4. 如果不想用編譯安裝,可以将兩系統yum源修改為阿裡,進行yum源安裝也可以。

二、redis安裝

Ubuntu18.04安裝redis

這裡介紹yum源安裝【将源修改為阿裡的源】

修改yum源參考

  1. 源安裝
[email protected]:~$ sudo apt-get install redis
           
  1. 檢視redis程序
[email protected]:~$ 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下載下傳至該目錄,并完成解壓
[[email protected] /]# mkdir /software
[[email protected] /]# cd /software/
[[email protected] software]# wget http://download.redis.io/releases/redis-4.0.9.tar.gz
[[email protected] software]# tar -zxvf redis-4.0.9.tar.gz 
[[email protected] software]# ls
redis-4.0.9  redis-4.0.9.tar.gz
           

2.建立安裝目錄,将上述解壓目錄redis-4.0.9複制到該安裝目錄,進行編譯安裝

[[email protected] software]# mkdir -p /usr/local/redis
[[email protected] software]# cp redis-4.0.9 /usr/local/redis/
[[email protected] software]# cd /usr/local/redis/
[[email protected] redis]# ls
redis-4.0.9
[[email protected] redis]# cd redis-4.0.9/
[[email protected] 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
[[email protected] redis-4.0.9]# make
[[email protected] redis-4.0.9]# make test  [測試編譯]
Cleanup: may take some time... OK  [出現這句話表示編譯沒有問題]
[[email protected] redis-4.0.9]# make install
           
  1. 傳回下載下傳目錄,将redis.conf配置檔案拷貝到/usr/local/bin下面,之後進入/usr/local/bin/檢視該目錄是否存在
[[email protected] redis-4.0.9]# cd /software/redis-4.0.9/
[[email protected] 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
[[email protected] redis-4.0.9]# cp redis.conf /usr/local/bin/
[[email protected] redis-4.0.9]# cd /usr/local/bin/
[[email protected] 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】

[email protected]:~$ sudo vim /etc/redis/redis.conf 
bind 192.168.1.106 127.0.0.1
protected-mode no
port 6379
           

修改完畢後,進行重新開機redis(確定防火牆關閉)

[email protected]:~$ sudo redis-server /etc/redis/redis.conf 
           

進入redis用戶端,添加一些資料

[email protected]:~$ 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】

[[email protected] /]# cd /usr/local/bin/
[[email protected] bin]# ls
dump.rdb  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis.conf  redis-sentinel  redis-server
[[email protected] 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服務

[[email protected] 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上

[[email protected] bin]# cd /usr/local/bin/
[[email protected] 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上相同的資訊,設定成功。

檢視主從關系指令

[email protected]:~$ 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
           

繼續閱讀