溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

redis的安裝以及主從配置和主從切換

發(fā)布時(shí)間:2020-05-26 17:53:49 來(lái)源:億速云 閱讀:243 作者:鴿子 欄目:系統(tǒng)運(yùn)維

安裝redis
下載redis源碼,并進(jìn)行相關(guān)操作,如下:
wget http://download.redis.io/releases/redis-3.2.3.tar.gz
解壓安裝redis
[root@redis ~]# tar zxf redis-3.2.3.tar.gz
解壓完畢后,現(xiàn)在開(kāi)始安裝,如下:
[root@redis ~]# cd redis-3.2.3/
[root@redis redis-3.2.3]# make&& make install
通過(guò)上圖,我們可以很容易的看出,redis安裝到
/usr/local,/usr/local/bin,/usr/local/share,/usr/local/include,/usr/local/lib,/usr/l
ocal/share/man目錄下。
然后再切換到utils目錄下,執(zhí)行redis初始化腳本install_server.sh,如下:
[root@redis redis-3.2.3]# cd utils/
[root@redisutils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redisconfig file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
CliExecutable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
通過(guò)上面的安裝過(guò)程,我們可以看出redis初始化后redis配置文件為
/etc/redis/6379.conf,日志文件為/var/log/redis_6379.log,數(shù)據(jù)文件dump.rdb存
放到/var/lib/redis/6379目錄下,啟動(dòng)腳本為/etc/init.d/redis_6379。
現(xiàn)在我們要使用systemd,所以在 /etc/systems/system 下創(chuàng)建一個(gè)單位文件名字
為 redis_6379.service。
[root@redisutils]# vi /etc/systemd/system/redis_6379.service
內(nèi)容如下:
[Unit]
Description=Redis on port 6379
[Service]
Type=forking
ExecStart=/etc/init.d/redis_6379 start
ExecStop=/etc/init.d/redis_6379 stop
[Install]
WantedBy=multi-user.target
注:這里Type=forking是后臺(tái)運(yùn)行的形式
啟動(dòng)redis
[root@redisutils]# systemctl daemon-reload
[root@redisutils]# systemctl enable redis_6379.service
[root@redisutils]# systemctl start redis_6379.service
[root@redisutils]# systemctl status redis_6379.service
● redis_6379.service - Redis on port 6379
Loaded: loaded (/etc/systemd/system/redis_6379.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2016-11-16 21:07:26 CST; 4min 25s ago
Process: 7732 ExecStart=/etc/init.d/redis_6379 start (code=exited, status=0/SUCCESS)
Main PID: 7734 (redis-server)
CGroup: /system.slice/redis_6379.service
└─7734 /usr/local/bin/redis-server 127.0.0.1:6379
Nov 16 21:07:26 redissystemd[1]: Starting Redis on port 6379...
Nov 16 21:07:26 redis redis_6379[7732]: Starting Redis server...
Nov 16 21:07:26 redissystemd[1]: Started Redis on port 6379.
[root@redisutils]# netstat -anpt | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0: LISTEN 7734/redis-server 1
從顯示結(jié)果可以看到redis默認(rèn)監(jiān)聽(tīng)的是127.0.0.1的6379端口
防火墻規(guī)則設(shè)置
[root@redisutils]# firewall-cmd --permanent --add-port=6379/tcp
success
[root@redisutils]# firewall-cmd --reload
success
現(xiàn)在來(lái)查看redis版本使用redis-cli –version命令,如下
[root@redisutils]# redis-cli --version
redis-cli 3.2.3
通過(guò)顯示結(jié)果,我們可以看到redis版本是3.2.3。
到此源碼方式安裝redis就介紹完畢。
redis安裝完畢之后,我們?cè)賮?lái)配置redis
設(shè)置redis監(jiān)聽(tīng)的地址,添加監(jiān)聽(tīng)redis主機(jī)的ip
考慮到安全性,我們需要啟用redis的密碼驗(yàn)證功能requirepass參數(shù)
最終redis配置文件如下:
[root@redis ~]# grep -Ev '^#|^$' /etc/redis/6379.conf (紅色部分為修改內(nèi)容)
bind 127.0.0.1 192.168.31.106
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis_6379.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilenamedump.rdb
dir /var/lib/redis/6379
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass pwd@123
appendonly no
appendfilename "appendonly.aof"
appendfsynceverysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limitpubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
重新啟動(dòng)redis服務(wù)
[root@redis ~]# systemctl restart redis_6379.service
[root@redis ~]# netstat -anpt | grep redis
tcp 0 0 192.168.31.106:6379 0.0.0.0:
LISTEN 8418/redis-server 1
Redis 命令
Redis 命令用于在 redis 服務(wù)上執(zhí)行操作。
要在 redis 服務(wù)上執(zhí)行命令需要一個(gè) redis 客戶端。Redis 客戶端在我們之前下載的的
redis 的安裝包中。
語(yǔ)法
Redis 客戶端的基本語(yǔ)法為:
$ redis-cli
實(shí)例
以下實(shí)例講解了如何啟動(dòng) redis 客戶端:
啟動(dòng) redis 客戶端,打開(kāi)終端并輸入命令 redis-cli。該命令會(huì)連接本地的 redis 服務(wù)。
$redis-cli
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> PING
PONG
在以上實(shí)例中我們連接到本地的 redis 服務(wù)并執(zhí)行 PING 命令,該命令用于檢測(cè) redis 服
務(wù)是否啟動(dòng)。
在遠(yuǎn)程服務(wù)上執(zhí)行命令
如果需要在遠(yuǎn)程 redis 服務(wù)上執(zhí)行命令,同樣我們使用的也是 redis-cli 命令。
語(yǔ)法
$ redis-cli -h host -p port -a password
實(shí)例
以下實(shí)例演示了如何連接到主機(jī)為 127.0.0.1,端口為 6379 ,密碼為 mypass 的 redis
服務(wù)上。
$redis-cli -h 127.0.0.1 -p 6379 -a "mypass"
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> PING
PONG
主從復(fù)制
環(huán)境描述:
主 redis:192.168.10.1 6379
從 redis:192.168.10.2 6380
一、主從配置
1、將主從 redis 配置文件 redis.conf 中的 daemonize no 改為 yes
2、修改從 redis 配置文件 redis.conf 中的 port 6379 改為 6380,添加 slaveof
192.168.10.1 6379
三種辦法:a、配置文件中加 slaveof [masterHost] [masterPort]
b、redis-server 啟動(dòng)時(shí)加--slaveof [masterHost] [masterPort]
c、直接使用命令 slaveof [masterHost] [masterPort]
3、啟動(dòng)主從服務(wù)
主 redis:
[root@localhost redis-2.8.3]# src/redis-server /soft/redis-2.8.3-master/redis2.8.3/redis.conf
從 redis:
[root@localhost redis-2.8.3]# src/redis-server /soft/redis-2.8.3-slave/redis2.8.3/redis.conf
4、測(cè)試數(shù)據(jù)同步
主 redis:
[root@localhost redis-2.8.3]# src/redis-cli -p 6379
127.0.0.1:6379> set name abc
OK
127.0.0.1:6379> get name
"abc"
127.0.0.1:6379>
從 redis:
[root@localhost redis-2.8.3]# src/redis-cli -p 6380
127.0.0.1:6380> get name
"abc"
127.0.0.1:6380>
5、默認(rèn)是讀寫分離的
在從 redis:
[root@localhost redis-2.8.3]# src/redis-cli -p 6380
127.0.0.1:6380> set name 123
(error) READONLY You can't write against a read only slave.
二、主從切換
1、停止主 redis
[root@localhost redis-2.8.3]# src/redis-cli -n 6379 shutdown
[root@localhost redis-2.8.3]# src/redis-cli -p 6379
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
2、將從 redis 設(shè)成主 redis
[root@localhost redis-2.8.3]# src/redis-cli -p 6380 slaveof NO ONE
OK
3、測(cè)試從 redis 是否切換從主 redis
[root@localhost redis-2.8.3]# src/redis-cli -p 6380
127.0.0.1:6380> set name 123
OK
127.0.0.1:6380> get name
"123"
127.0.0.1:6380>
4、原來(lái)的主 redis 恢復(fù)正常了,要重新切換回去
1)將現(xiàn)在的主 redis 的數(shù)據(jù)進(jìn)行保存
[root@localhost redis-2.8.3]# src/redis-cli -p 6380
127.0.0.1:6380> get name
"abc"
127.0.0.1:6380> set name 123
OK
127.0.0.1:6380> get name
"123"
127.0.0.1:6380> save
OK
127.0.0.1:6380> get name
"123"
127.0.0.1:6380>
2)將現(xiàn)在的主 redis 根目錄下 dump.rdb 文件拷貝覆蓋到原來(lái)主 redis 的根目錄
3)啟動(dòng)原來(lái)的主 redis
[root@localhost redis-2.8.3]# src/redis-server /soft/redis-2.8.3-master/redis2.8.3/redis.conf
4)在現(xiàn)在的主 redis 中切換
[root@localhost redis-2.8.3]# src/redis-cli -p 6380 slaveof 192.168.10.1 637

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI