Redis安裝、主從配置及aof使用


找了02,03,04三台機器,04做主,02做從,03做客戶端。

 

都使用jumbo install redis安裝了Redis(server+client)。

 

在 02 從的 ~/.jumbo/etc/redis.conf 里

slaveof <masterip> 6379

 

在04 主的 ~/.jumbo/etc/redis.conf 里

appendonly yes

appendfsync everysec

 

主從都要改:

daemonize yes

logfile "/home/work/.jumbo/var/log/redis/redis.log"

 

然后先啟動主,再啟動從,都使用

redis-server (因為deamonize為true,所以不用&)

 

報如下錯誤:

7652:C 05 Oct 16:59:37.203 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf

7652:M 05 Oct 16:59:37.207 # Creating Server TCP listening socket *:6379: unable to bind socket

 

然后發現居然是因為沒有指定配置文件:

 redis-server ~/.jumbo/etc/redis.conf 

這樣就可以了。

 

然后跑到03上,用客戶端訪問:

redis-cli  -h 10.117.146.16 -p 6379  

發現:

Could not connect to Redis at 10.117.146.16:6379: Connection refused
Could not connect to Redis at 10.117.146.16:6379: Connection refused
not connected>

然后也發現從機一直報error log:

36855:S 05 Oct 17:10:57.379 * Connecting to MASTER 10.117.146.16:6379
36855:S 05 Oct 17:10:57.379 * MASTER <-> SLAVE sync started
36855:S 05 Oct 17:10:57.379 # Error condition on socket for SYNC: Connection refused

 

先把從Redis關了,用客戶端:

$ redis-cli
127.0.0.1:6379> shutdown
not connected>
not connected> quit

 

然后再看Redis配置文件 vi ~/.jumbo/etc/redis.conf

有如下內容:

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

所以,注釋掉最后一行。重新啟動。

 

然后又開始報這樣的錯:

12487:M 05 Oct 17:16:46.170 # Creating Server TCP listening socket *:6379: unable to bind socket

 

在redis.conf里面加上下面這行:

# bind 127.0.0.1
bind 0.0.0.0

 

重新啟動:redis-server ~/.jumbo/etc/redis.conf 

然后 ps xu | grep redis 可以看到:

work     23986  0.0  0.0 79428 2104 ?        Ssl  17:18   0:00 redis-server 0.0.0.0:6379 

 

在03機器上可以訪問:

redis-cli -h 10.117.x.x -p 6379
10.117.146.16:6379>

 

然后可以看到aof文件位置在:

$ ls -la ~/.jumbo/var/lib/redis/appendonly.aof
-rw-r--r-- 1 work work 0 Oct 5 17:06 /home/work/.jumbo/var/lib/redis/appendonly.aof

 

然后從Redis的日志有:

25089:S 05 Oct 17:22:59.018 * MASTER <-> SLAVE sync started
25089:S 05 Oct 17:22:59.018 * Non blocking connect for SYNC fired the event.
25089:S 05 Oct 17:22:59.019 * Master replied to PING, replication can continue...
25089:S 05 Oct 17:22:59.019 * Partial resynchronization not possible (no cached master)
25089:S 05 Oct 17:22:59.019 * Full resync from master: 64377169e1e5fc029ad8e584e24947a53d4f8fa0:1
25089:S 05 Oct 17:22:59.103 * MASTER <-> SLAVE sync: receiving 76 bytes from master
25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Flushing old data
25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Loading DB in memory
25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Finished with success

主Redis的日志有:

23986:M 05 Oct 17:22:59.020 * Slave 10.117.x.x:6379 asks for synchronization
23986:M 05 Oct 17:22:59.020 * Full resync requested by slave 10.117.146.21:6379
23986:M 05 Oct 17:22:59.020 * Starting BGSAVE for SYNC with target: disk
23986:M 05 Oct 17:22:59.020 * Background saving started by pid 40323
40323:C 05 Oct 17:22:59.076 * DB saved on disk
40323:C 05 Oct 17:22:59.076 * RDB: 0 MB of memory used by copy-on-write
23986:M 05 Oct 17:22:59.104 * Background saving terminated with success
23986:M 05 Oct 17:22:59.104 * Synchronization with slave 10.117.x.x:6379 succeeded

 

以下是先用客戶端訪問主,再訪問從的結果:

$ redis-cli -h 10.117.146.16 -p 6379
10.117.146.16:6379> zadd page_rank 10 google.com
(integer) 1
10.117.146.16:6379> zadd page_rank 9 baidu.com 8 bing.com
(integer) 2
10.117.146.16:6379> zrange page_rank 0 -1 withscores
1) "bing.com"
2) "8"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"
10.117.146.16:6379> zrange page_rank 0 -1
1) "bing.com"
2) "baidu.com"
3) "google.com"
10.117.146.16:6379> quit

$ redis-cli -h 10.117.146.21 -p 6379 10.117.146.21:6379> zrange page_rank 0 -1 withscores 1) "bing.com" 2) "8" 3) "baidu.com" 4) "9" 5) "google.com" 6) "10" 10.117.146.21:6379> zrange page_rank 0 -1 1) "bing.com" 2) "baidu.com" 3) "google.com" 10.117.146.21:6379> quit

而此時查看主和從的日志,都沒有明顯的日志,因為日志開在了notice級別:

主:
40323:C 05 Oct 17:22:59.076 * RDB: 0 MB of memory used by copy-on-write 23986:M 05 Oct 17:22:59.104 * Background saving terminated with success 23986:M 05 Oct 17:22:59.104 * Synchronization with slave 10.117.146.21:6379 succeeded
從:
25089:S 05 Oct 17:22:59.019 * Partial resynchronization not possible (no cached master) 25089:S 05 Oct 17:22:59.019 * Full resync from master: 64377169e1e5fc029ad8e584e24947a53d4f8fa0:1 25089:S 05 Oct 17:22:59.103 * MASTER <-> SLAVE sync: receiving 76 bytes from master 25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Flushing old data 25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Loading DB in memory 25089:S 05 Oct 17:22:59.104 * MASTER <-> SLAVE sync: Finished with success

 

而aof文件,相比於剛才,已經有了增加:

$ ls -la ~/.jumbo/var/lib/redis/appendonly.aof
-rw-r--r--  1 work work 149 Oct  5 17:28 /home/work/.jumbo/var/lib/redis/appendonly.aof

剛剛是:

$ ls -la ~/.jumbo/var/lib/redis/appendonly.aof
-rw-r--r-- 1 work work 0 Oct 5 17:06 /home/work/.jumbo/var/lib/redis/appendonly.aof

 

然后通過客戶端分別關掉兩個server:

$ redis-cli -h 10.117.146.21 -p 6379
10.117.146.21:6379> SHUTDOWN save
not connected> quit
$ redis-cli -h 10.117.146.16 -p 6379
10.117.146.16:6379> SHUTDOWN save
not connected> 
not connected> quit

然后再次分別啟動,來監測aof文件是否會自動加載。

用客戶端連接,發現數據都還在,如下所示:

$ redis-cli -h 10.117.x.x -p 6379
10.117.x.x:6379> zrange page_rank 0 -1 withscores
1) "bing.com"
2) "8"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"
10.117.x.x:6379> 

所以,aof文件是自動加載的。Redis的持久化是生效的。

 

再說一下Redis的密碼驗證:

現在安裝的目錄里配置文件位置:/home/work/.jumbo/etc/redis.conf

在master上:
# requirepass foobared
requirepass [用戶名]

在slave上:
# masterauth <master-password>
masterauth [用戶名]

然后重啟。重啟方法見前文。

之后訪問的時候,會出現沒有權限的情況,需要在命令前指定權限,或者在redis-cli參數里用 -a 加權限:

$ redis-cli -h 10.117.146.16 -p 6379
10.117.146.16:6379> keys *
(error) NOAUTH Authentication required.
10.117.146.16:6379> auth [用戶名]
OK
10.117.146.16:6379> keys *
 1) "age"
 2) "testName"
 3) "newset"
 4) "k2"
 5) "myhash"
 6) "page_rank"
 7) "myset"
 8) "k1"
 9) "myzset"
10) "myset2"
11) "k5"
12) "mylist"
13) "k3"
10.117.146.16:6379> quit

$ redis-cli -h 10.117.146.16 -p 6379 -a [用戶名]
10.117.146.16:6379> keys *
 1) "age"
 2) "testName"
 3) "newset"
 4) "k2"
 5) "myhash"
 6) "page_rank"
 7) "myset"
 8) "k1"
 9) "myzset"
10) "myset2"
11) "k5"
12) "mylist"
13) "k3"
10.117.146.16:6379> quit

$ redis-cli -h 10.117.146.16 -p 6379
10.117.146.16:6379> keys *
(error) NOAUTH Authentication required.
10.117.146.16:6379> auth abc
(error) ERR invalid password

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM