最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

redis安裝 集群 教程

2023-02-21 16:11 作者:bili_39183997178  | 我要投稿

數(shù)據(jù)庫服務(wù)軟件分為2類:

???????????????? 關(guān)系型數(shù)據(jù)庫服務(wù)軟件 簡稱 RDBMS

???????????????????? 存儲數(shù)據(jù)的特點?

???????????????????? 服務(wù)軟件?MySQL

? ? ? ? ? ? ? ? ?非關(guān)系型數(shù)據(jù)庫服務(wù)軟件 簡稱 NoSQL

???????????????????? 存儲數(shù)據(jù)的特點?

???????????????????? 服務(wù)軟件??

redis 軟件介紹

Remote Dictionary? Server(遠(yuǎn)程字典服務(wù)器)

是一款高性能的(Key/Values

支持?jǐn)?shù)據(jù)持久化(定期把內(nèi)存里數(shù)據(jù)存儲到硬盤)

支持多種數(shù)據(jù)類型:字符、列表、散列、集合

支持 master-salve 模式數(shù)據(jù)備份

中文網(wǎng)站www.redis.cn

安裝軟件

[root@host51 ~]# which? gcc || yum -y install gcc?? 安裝編譯工具gcc

[root@host51 ~]#tar -xf redis-4.0.8.tar.gz 解壓源碼

[root@host51 ~]#cd redis-4.0.8/ 進(jìn)入源碼目錄

[root@host51 ~]#make? 編譯

[root@host51 ~]#make install 安裝

#初始配置

[root@host51 ~]#./utils/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 redis config file name [/etc/redis/6379.conf]? 主配置文件

Selected default - /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log] 服務(wù)日志文件

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379] 數(shù)據(jù)庫目錄

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server] 服務(wù)啟動命令

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

Cli Executable : /usr/local/bin/redis-cli? 連接服務(wù)命令

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...?? 初始化配置后 會自動啟動redis服務(wù) 并設(shè)置開機(jī)運行

Installation successful!

[root@host51 redis-4.0.8]#

?#查看服務(wù)的端口號

[root@host51 redis-4.0.8]# netstat? -utnalp? | grep? 6379

tcp??????? 0????? 0 127.0.0.1:6379????????? 0.0.0.0:*?????????????? LISTEN????? 4108/redis-server 1

查看進(jìn)程

[root@host51 redis-4.0.8]# ps -C redis-server

? PID TTY????????? TIME CMD

?4108 ???????? 00:00:00 redis-server


停止redis服務(wù)

[root@host51 redis-4.0.8]# /etc/init.d/redis_6379 stop

Stopping ...

?#啟動redis服務(wù)

[root@host51 redis-4.0.8]# /etc/init.d/redis_6379 start

Stopping ...

[root@host51 ~]# redis-cli?? 連接服務(wù) 存取數(shù)據(jù)


[root@host51 ~]# redis-cli shutdown

[root@host51 ~]# /etc/init.d/redis_6379 start

#修改密碼等

[root@host51 ~]# vim /etc/redis/6379.conf

70 bind 192.168.4.51?? 地址

93 port 6351? 端口

501 requirepass 123456? 密碼? ???

:wq

[root@host51 ~]# /etc/init.d/redis_6379? stop

Stopping ...

Redis stopped

[root@host51 ~]# /etc/init.d/redis_6379? start

[root@host51 ~]# netstat -utnalp? | grep redis-server

tcp??????? 0? 0 192.168.4.51:6351? 0.0.0.0:*????? LISTEN????? 4405/redis-server 1

[root@host51 ~]# redis-cli? -h 192.168.4.51 -p 6351 -a 123456


使用命令停止服務(wù)

[root@host51 ~]# redis-cli? -h 192.168.4.51 -p 6351 -a 123456 shutdown

[root@host51 ~]# /etc/init.d/redis_6379? start? 啟動服務(wù)


修改腳本使其也可以使用腳本停止服務(wù)(啟動腳本是使用shell語法編寫)

[root@host51 ~]# vim?? +43?? /etc/init.d/redis_6379

$CLIEXEC -h 192.168.4.51 -p 6351 -a 123456 shutdown?????????

??????????? :wq??????????

#執(zhí)行腳本停止服務(wù)

[root@host51 ~]# /etc/init.d/redis_6379? stop

??


LNP+Redis 案例

? ?具體配置步驟如下:?

???????????????????? 1) 安裝源碼的nginx軟件

???????????????????? 2) 安裝php軟件? ??php php-fpm php-devel

? ? ? ? ? ? ? ? ? ? ?? ?3) 修改nginx服務(wù)的配置文件實現(xiàn)動靜分離

? ? ? ? ? ? ? ? ? ? ? ? ?4) 啟動服務(wù)

??????????????????????????? 啟動nginx服務(wù) 和 php-fpm服務(wù)

??????????????????????????? 查看服務(wù)對應(yīng)的端口80 和 9000

???????????????????? 5) 測試Nginx服務(wù)能否解釋PHP代碼

??? ? ? ? ? ? ? ? ??

2.2 配置php支持redis

意思就是在網(wǎng)站服務(wù)器編寫php 腳本 可以連接redis服務(wù)存儲數(shù)據(jù)和查詢數(shù)據(jù)

默認(rèn)PHP不支持redis (也就是不能連接redis服務(wù))

? ?1) 安裝軟件提供連接redis服務(wù)的功能模塊

????????????? 2) 讓PHP進(jìn)程在運行時 調(diào)用redis模塊

????????????? 3) 查看是否支持redis服務(wù)

????????????? 4) 測試配置

???????????????????? 4.1 在網(wǎng)站服務(wù)器編寫PHP腳本 存儲數(shù)據(jù) 和查詢數(shù)據(jù)

??????????????????????????? #編寫存儲數(shù)據(jù)的腳本

??????????????????????????? #編寫查詢數(shù)據(jù)的腳本

???????????????????? 4.2 在客戶端訪問網(wǎng)站服務(wù)器php腳本

? ? ? ? ? ? ? ? ? ? ? 4.3 在Redis服務(wù)器本機(jī)查看內(nèi)存里的數(shù)據(jù)

???????????????????????? 能夠看到PHP存儲的數(shù)據(jù)為成功


[root@host50~]#tar -xf redis-cluster-4.3.0.tgz

[root@host50~]#cd redis-4.3.0/

?創(chuàng)建配置命令configure 和生產(chǎn)PHP配置信息文件/usr/bin/php-config

[root@host50 redis-4.3.0]# phpize?

Configuring for:

PHP Api Version:???????? 20100412

Zend Module Api No:????? 20100525

Zend Extension Api No:?? 220100525

?#配置

[root@host50redis-4.3.0]# ./configure? --with-php-config=/usr/bin/php-config?

[root@host50 redis-4.3.0]# make ?編譯

[root@host50 redis-4.3.0]# make install 安裝

#提示模塊的安裝目錄

Installing shared extensions:???? /usr/lib64/php/modules/??

[root@host50 redis-4.3.0]#

?#查看目錄下的模塊列表 有redis.so 模塊文件即可

[root@host50 redis-4.3.0]# ls /usr/lib64/php/modules/?

curl.so????? json.so??? mysql.so????? pdo.so???????? phar.so?? sqlite3.so

fileinfo.so? mysqli.so? pdo_mysql.so? pdo_sqlite.so? redis.so? zip.so

[root@host50 redis-4.3.0]# vim /etc/php.ini

?728 extension_dir = "/usr/lib64/php/modules/"? 模塊文件所在的目錄

?730 extension = "redis.so" 模塊名

:wq

?[root@host50 redis-4.3.0]# systemctl? restart php-fpm

?#查看支持的模塊

[root@host50 ~]# php -m? | grep -i? redis

redis


Redis服務(wù)的內(nèi)存清除策略

redis服務(wù)提供的內(nèi)存清除策略如下:(在/etc/redis/6379.conf 有說明信息)

?? 1) LRU? 刪除最近最少使用的,有2種清除方式:

???????????? volatile-lru??? 針對設(shè)置了TTL的key

???????????? allkeys-lru ??? 針對所有的key

? ? ?2)LFU? 刪除使用頻率最少的 有2種清除方式:

???????????? volatile-lfu??? 針對設(shè)置了TTL的key

???????????? allkeys-lfu ??? 針對所有的key

? ??3) RANDOM? 隨機(jī)刪除 有2種清除方式:

???????????? volatile-random 針對設(shè)置了TTL的key

???????????? allkeys-random? 針對所有的key

? ?4) volatile-ttl? 刪除快要過期的

? ?5)noeviction 不刪除 存儲空間不夠時 服務(wù)直接報錯


與內(nèi)存相關(guān)的配置項 vim /etc/redis/6379.conf

maxmemory? ????? //最大內(nèi)存

maxmemory-policy ??? //定義使用策略

maxmemory-samples? 20 //選取key模板的個數(shù) (針對lru 和 ttl 、volatile-ttl 策略



Redis集群

準(zhǔn)備工作:

??第1步 安裝redis軟件并做初始化配置

第2步 停止按照初始化配置啟動的Redis服務(wù)

第3步 修改主配置文件(啟用集群功能)

第4步 啟動redis服務(wù) 并查看端口號

重要說明:內(nèi)存里不允許有數(shù)據(jù) 不能設(shè)置連接密碼

[root@host51 ~]# yum -y install gcc

[root@host51 ~]# tar -xf redis-4.0.8.tar.gz

[root@host51 ~]# cd redis-4.0.8/

[root@host51 ~]# make && make install

[root@host51 ~]# ./utils/install_server.sh? 遇到提示就回車

[root@host51 ~]# /etc/init.d/redis_6379? stop

[root@host51 ~]# vim /etc/redis/6379.conf

bind 192.168.4.51

port 6379

cluster-enabled yes #啟用集群功能

cluster-config-file nodes-6379.conf 保存集群信息的配置文件

cluster-node-timeout 5000? 集群中主機(jī)的連接超時時間

[root@host51 ~]# ??? ???????

[root@host51 ~]# /etc/init.d/redis_6379? start

[root@host51 ~]# netstat? -utnlp? | grep redis-server

tcp??????? 0????? 0 192.168.4.51:16379????? 0.0.0.0:*?????????????? LISTEN????? 4249/redis-server 1

tcp??????? 0????? 0 192.168.4.51:6379????? 0.0.0.0:*?????????????? LISTEN????? 4249/redis-server 1

? 注重復(fù)上面的步驟有幾臺就重復(fù)好多

配置管理主機(jī)

第一步:準(zhǔn)備ruby腳本的運行環(huán)境

?????? ?第二步:創(chuàng)建腳本

?????? ?第三步:查看腳本幫助信息

]#yum? -y? install?? rubygems?? ruby

]#gem? install? redis-3.2.1.gem

]#mkdir? /root/bin???? ?????????? //創(chuàng)建命令檢索目錄

]#tar -xf redis-4.0.8.tar.gz

]#cp? redis-4.0.8/src/redis-trib.rb?? /root/bin/? ???

]#chmod? +x?? /root/bin/redis-trib.rb? 設(shè)置執(zhí)行權(quán)限

]#redis-trib.rb?? help?? (能看到幫助信息為成功)

創(chuàng)建集群

??? ????

?????? 創(chuàng)建集群的命令格式??

[root@mgm57 ~]# redis-trib.rb? create --replicas 數(shù)字?? ip地址:端口? ip地址:端口? ip地址:端口? ....

說明,創(chuàng)建集群時,會把前3臺服務(wù)器 配置為主服務(wù)器,剩下的其他主機(jī)全做從服務(wù)器

?????? --replicas 從服務(wù)器的臺數(shù)(指定每個主服務(wù)器有幾臺從服務(wù)器)

[root@mgm57 ~]# redis-trib.rb create --replicas 1? 192.168.4.51:6379? 192.168.4.52:6379 192.168.4.53:6379 192.168.4.54:6379 192.168.4.55:6379 192.168.4.56:6379

>>> Creating cluster

創(chuàng)建集群失?。?/p>

? ? ? ? ? ? ?1 如果所有的Redis服務(wù)都啟用的集群功能 內(nèi)存里也沒有數(shù)據(jù) 也沒設(shè)置連接密碼 還是創(chuàng)建失敗

?????????? ? 可能是物理內(nèi)存不足 導(dǎo)致創(chuàng)建失敗 把沒用的虛擬機(jī)刪除

?????????? ?

?????????? 2 在集群中所有主機(jī)執(zhí)行清除集群信息的命令 > cluster reset?

查看集群信息

?????????? #查看集群統(tǒng)計信息

[root@mgm57 ~]# redis-trib.rb info? 192.168.4.56:6379

#查看集群詳細(xì)信息

[root@mgm57 ~]# redis-trib.rb check? 192.168.4.51:6379



訪問集群存取數(shù)據(jù)

命令格式?

]# redis-cli? -c?? -h redis服務(wù)器的ip?? -p 端口號

說明:

?????? 連接集群中的任意一臺服務(wù)器都可以查詢數(shù)據(jù)和存儲數(shù)據(jù))

?????? -c 連接集群中的主機(jī) 使用集群算法存儲數(shù)據(jù)


管理集群

3.1 向集群里添加新服務(wù)器

??? ??? 說明:什么情況下需要向集群里添加新主機(jī)(添加master角色服務(wù)器)

?????????? ? 擴(kuò)大內(nèi)存空間(添加master角色服務(wù)器)。

?????????? ? 為了保證服務(wù)的可靠性(給主服務(wù)器添加多個從服務(wù)器)

??? 1)添加master角色的服務(wù)器

?????????? 訴求把新的Redis服務(wù)器Host58 添加到集群里做master 服務(wù)器

?????????? 具體配置如下:

????????????? 1) Host58運行redis 服務(wù)且啟用了集群功能

????????????????? redis服務(wù)的端口 6379?? 服務(wù)使用的地址 192.168.4.58

????????????? 2) 在管理主機(jī)mgm57 如下操作:

???????????????????? 2.1 把host58主機(jī)添加到集群里

命令格式

[root@mgm57 ~]# redis-trib.rb add-node 新主機(jī)Ip:端口? 集群中已有主機(jī)的ip:端口

?分配hast slots (master角色的服務(wù)器沒有hast slots得不到存儲數(shù)據(jù)機(jī)會)

命令格式????????????????????

[root@mgm57 ~]# redis-trib.rb?? reshard? 集群中已有主機(jī)的ip:端口

?

?2)添加slave角色的服務(wù)器

?????????? 說明:從角色服務(wù)器的數(shù)據(jù)是master服務(wù)器同步過來的數(shù)據(jù)

????????????? ? 所以slave角色的服務(wù)器不需要分配hash slots

????????????? ? 只要把主機(jī)添加到集群了做slave服務(wù)器就可以了。

?????? 命令格式

[root@mgm57 ~]# redis-trib.rb? add-node?? --slave? 新主機(jī)Ip:端口? 集群中已有主機(jī)的ip:端口

3.2 把服務(wù)器移除集群

?????????? 3.2.1? 移除slave角色的主機(jī)

命令格式? [root@mgm57 ~]# redis-trib.rb del-node? 集群中任意主機(jī)的Ip:端口? 被移除主機(jī)的id

說明:slave角色的主機(jī)的沒有hash slots 直接移除即可

??? ? 主機(jī)被移除集群后redis服務(wù)會自動停止

?????????????

?移除master角色的主機(jī)

? ? ?說明:master角色的服務(wù)器會占用hash slots? 要先釋放hash slots? 再執(zhí)行移除主機(jī)的命令

? ? 具體操作步驟:

????????????????? 第一步:釋放 hash slots (再次執(zhí)行reshard命令)

????????????????? 第二步:移除主機(jī) (執(zhí)行刪除主機(jī)的命令)

#釋放 hash slots (再次執(zhí)行reshard命令)

[root@mgm57 ~]# redis-trib.rb? reshard? 192.168.4.56:6379

?第1個問題:釋放hash slots 的個數(shù)

How many slots do you want to move (from 1 to 16384)? 4096? (host58主機(jī)占用hash slots 的個數(shù))

?第2個問題:接收4096的個hash slots的主數(shù)據(jù)庫服務(wù)器的ID (隨便給那個主服務(wù)器都可以)

What is the receiving node ID?0eb3b7aa0493a19189cba35b0c658202cc20884b?? (host51主機(jī)的id ,就是把釋放的4096個hash slots給主數(shù)據(jù)庫服務(wù)器host51)

?第3個問題:從那臺主服務(wù)器移除4096個hash slots

Source node #1:87cc1c128166e08a16cc294758611453bbc71437? (host58主機(jī)的id)

Source node #2:done 結(jié)束指定

第4個問題:確認(rèn)配置 yes同意? no 退出???????

Do you want to proceed with the proposed reshard plan (yes/no)? yes? 同意


查看host58主機(jī)的id 然后刪除主機(jī) Host58

[root@mgm57 ~]# redis-trib.rb? check? 192.168.4.56:6379 | grep? 192.168.4.58

M: 87cc1c128166e08a16cc294758611453bbc71437 192.168.4.58:6379

[root@mgm57 ~]# redis-trib.rb? del-node?? 192.168.4.56:6379? 87cc1c128166e08a16cc294758611453bbc71437 ???????????

>>> Removing node 87cc1c128166e08a16cc294758611453bbc71437 from cluster?

? ? ? ? ? ? ? ? ?

?

擴(kuò)展知識

? ? ? 1) 平均分配當(dāng)前所有主服務(wù)器的hash slots

[root@mgm57 ~]# redis-trib.rb? info? 192.168.4.56:6379? 槽不平均

192.168.4.53:6379 (f2c1bdb7...) -> 1 keys | 4096 slots | 1 slaves.

192.168.4.51:6379 (0eb3b7aa...) -> 3 keys | 8192 slots | 1 slaves.

192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 4096 slots | 1 slaves.

[OK] 6 keys in 3 masters.

0.00 keys per slot on average.

平均分配hash slots

[root@mgm57 ~]# redis-trib.rb??? rebalance 192.168.4.56:6379

? ? 再次查看平均了

[root@mgm57 ~]# redis-trib.rb info 192.168.4.56:6379

192.168.4.53:6379 (f2c1bdb7...) -> 2 keys | 5462 slots | 1 slaves.

192.168.4.51:6379 (0eb3b7aa...) -> 2 keys | 5461 slots | 1 slaves.

192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 5461 slots | 1 slaves.

[OK] 6 keys in 3 masters.

0.00 keys per slot on average.


2) 把移除的主機(jī)再次添加到集群里的步驟

????????????? 第一步: 啟動Redis服務(wù)并清除前世記憶(在Redis服務(wù)器本機(jī)操作)

????????????? 第二步: 清空內(nèi)存里的數(shù)據(jù)(在Redis服務(wù)器本機(jī)操作)

????????????? 第三步: 在管理主機(jī)執(zhí)行執(zhí)行添加命令(在管理主機(jī)執(zhí)行)

????????????? 第四步: 查看是否添加成功(在管理主機(jī)執(zhí)行)

#啟動服務(wù)器?

/etc/init.d/redis_6379 start

Starting Redis server...

[root@host58 ~]# netstat? -utnlp? | grep? -i redis-server

tcp??????? 0????? 0 192.168.4.58:6379?????? 0.0.0.0:*?????????????? LISTEN????? 4480/redis-server 1

tcp??????? 0????? 0 192.168.4.58:16379????? 0.0.0.0:*?????????????? LISTEN????? 4480/redis-server 1

[root@host58 ~]#

[root@host58 ~]# redis-cli? -h 192.168.4.58 -p 6379

192.168.4.58:6379> cluster reset清除前世記憶

OK

192.168.4.58:6379>??????????????

192.168.4.58:6379> keys *

在管理主機(jī)把5添加到集群里

[root@mgm57 ~]# redis-trib.rb add-node 192.168.4.58:6379? 192.168.4.51:6351

?#查看集群信息

[root@mgm57 ~]# redis-trib.rb info? 192.168.4.51:6351? 發(fā)現(xiàn)多了主服務(wù)器58



把 移除的59 再次添加到集群 做53主機(jī)的slave數(shù)據(jù)庫服務(wù)器

?命令格式

redis-trib.rb add-node --slave --master-id 主服務(wù)器的id?? 新服務(wù)器的ip:端口 集群中已有機(jī)器的Ip:端口

#啟動53主機(jī)的redis服務(wù)并清除前世記錄

]# /etc/init.d/redis_6379 start

]# redis-cli? -h 192.168.4.59 -p 6379

> cluster? reset

> exit

?#在管理主機(jī)查看host53主機(jī)的id

[root@mgm57 ~]# redis-trib.rb check?? 192.168.4.51:6351 | grep 192.168.4.53

M: d9c9ef29c07ad740a05fa7aaa4d96ce25fa089b5 192.168.4.53:6353

#添加59主機(jī)做主服務(wù)器53的 slave服務(wù)器

[root@mgm57 ~]# redis-trib.rb add-node --slave --master-id d9c9ef29c07ad740a05fa7aaa4d96ce25fa089b5 192.168.4.59:6379 192.168.4.51:6351

?

3)把集群中的主機(jī)恢復(fù)為獨立的數(shù)據(jù)庫服務(wù)器(host51~host56)

????????????? 在數(shù)據(jù)庫服務(wù)器本機(jī)執(zhí)行如下操作

?????????? ??? #停止redis服務(wù)

????????????? #注釋掉配置文件中的集群功能

????????????? #清空數(shù)據(jù)庫目錄

????????????? #啟動服務(wù)

????????????? #連接服務(wù)查看不到集群信息 也沒有數(shù)據(jù)

[root@host51 ~]# redis-cli? -h 192.168.4.51 shutdown

[root@host51 ~]# vim? /etc/redis/6379.conf

#cluster-enabled yes

#cluster-config-file nodes-6379.conf

#cluster-node-timeout 5000

[root@host51 ~]# rm -rf /var/lib/redis/6379/*

[root@host51 ~]# /etc/init.d/redis_6379 start

Starting Redis server...

[root@host51 ~]# netstat -utnalp? |grep -i redis-server

tcp??????? 0????? 0 192.168.4.51:6379???? ??0.0.0.0:*?????????????? LISTEN????? 9082/redis-server 1

[root@host51 ~]# redis-cli? -h 192.168.4.51 -p 6379

192.168.4.51:6379> keys *

(empty list or set)

192.168.4.51:6379> cluster info

ERR This instance has cluster support disabled? 沒有啟用集群功能

192.168.4.51:6379>

?#在管理主機(jī)查看集群信息看不到host51主機(jī)了

[root@mgm57 ~]# redis-trib.rb info 192.168.4.56:6379

192.168.4.54:6379 (bdba7786...) -> 2 keys | 5462 slots | 1 slaves.

192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 5461 slots | 1 slaves.

192.168.4.58:6379 (87cc1c12...) -> 2 keys | 5461 slots | 0 slaves.

[OK] 6 keys in 3 masters.

0.00 keys per slot on average.



主從復(fù)制

???? redis服務(wù)的主從復(fù)制(與mysql服務(wù)主同步的功能一樣,都是實現(xiàn)數(shù)據(jù)自動同步的存儲結(jié)構(gòu)。

???? 主服務(wù)器:接收客戶端連接

???? 從服務(wù)器:連接主服務(wù)器同步數(shù)據(jù)

?主從復(fù)制結(jié)構(gòu)模式:

一主一從?? 、 一主多從 、 主從從

?redis主從復(fù)制工作過程(數(shù)據(jù)同步原理):

?。?!說明?。。。簭姆?wù)器首次做的是全量同步,且同步的數(shù)據(jù)會覆蓋本機(jī)的數(shù)據(jù)

??????????? 第1步:slave向master發(fā)送sync命令

??????????? 第2步:master啟動后臺存盤進(jìn)程,并收集所有修改數(shù)據(jù)命令

??????????? 第3步:master完成后臺存盤后,傳送整個數(shù)據(jù)文件到slave

??????????? 第4步:slave接收數(shù)據(jù)文件,加載到內(nèi)存中完成首次完全同步,后續(xù)有新數(shù)據(jù)產(chǎn)生時,master繼續(xù)收集數(shù)據(jù)修改命令依次傳給slave,完成同步


命令行配置命令(馬上生效 但不永久 適合配置線上服務(wù)器)

info? replication???? #查看復(fù)制信息

slaveof?? 主服務(wù)器ip地址?? 主服務(wù)器端口號?? #指定主服務(wù)服務(wù)器IP地址和服務(wù)端口號

slaveof?? no? one???????????????????????? #臨時恢復(fù)為主服務(wù)器

?修改配置文件(永久有效,重啟了redis服務(wù)依然有效)

??????? ]# vim /etc/redis/6379.conf

??????? slaveof? 主服務(wù)器ip地址?? 主服務(wù)器端口號

???? ? :wq

!!!說明!!!!

??????? redis服務(wù)運行后 默認(rèn)角色就是master(主)所以一臺主機(jī)做master 服務(wù)器的話 無需配置。

??????? 主從結(jié)構(gòu)中的從服務(wù)器 都是只讀的, 客戶端連接從服務(wù)器對數(shù)據(jù)僅有查詢權(quán)限


配置一主一從?

[root@host51 ~]# redis-cli? -h 192.168.4.51 -p 6379? #host51主機(jī) 連接服務(wù)?

192.168.4.51:6379> info replication?? 查看復(fù)制信息

# Replication

role:master? 角色是 master

connected_slaves:0? 從服務(wù)器的個數(shù) 是 零個

?[root@host52 ~]# redis-cli? -h 192.168.4.52 -p 6379 #host52主機(jī)連接服務(wù)

192.168.4.52:6379> info replication

# Replication

role:master?? 角色是 master

connected_slaves:0??? 從服務(wù)器的個數(shù) 是 零個

192.168.4.52:6379> slaveof? 192.168.4.51? 6379?? 指定主服務(wù)器的ip 和端口

OK

?#在52主機(jī)再次查看復(fù)制信息

192.168.4.52:6352> info replication

# Replication

role:slave? 角色變?yōu)?slave

master_host:192.168.4.51?? 主服務(wù)器ip

master_port:6351? 主服務(wù)器端口

master_link_status:up?? 能與主服務(wù)器連接?? 如是down 表示連接不上master 服務(wù)

.....???????

把52永久配置為51的從服務(wù)器 (host52主機(jī)的系統(tǒng)重啟 或redis服務(wù)重啟 都依然是host51的從服務(wù)器)

[root@host52 ~]# vim +282 /etc/redis/6379.conf

282 slaveof? 192.168.4.51 6379??

:wq

?#在51主機(jī)再次查看復(fù)制信息

[root@host51 ~]# redis-cli? -h 192.168.4.51 -p 6379

192.168.4.51:6379> info? replication

# Replication

role:master

connected_slaves:1? 有1臺 從服務(wù)器

slave0:ip=192.168.4.52,port=6379,state=online,offset=294,lag=0? 從服務(wù)器的具體信息

?驗證配置:

???? 第一步 連接主服務(wù)器存儲數(shù)據(jù)

[root@host51 ~]# redis-cli? -h 192.168.4.51 -p 6379

192.168.4.51:6379> mset?? x 1? y? 2? z 3?

OK

192.168.4.51:6379> keys *

1) "x"

2) "z"

3) "y"

192.168.4.51:6351> ?????

???? 第二步? 連接從服務(wù)器 能夠查看到和主服務(wù)器一樣的數(shù)據(jù)

[root@host52 ~]# redis-cli? -h 192.168.4.52 -p 6379

192.168.4.52:6379> keys *

1) "z"

2) "y"

3) "x"

???? 說明:主從結(jié)構(gòu)的從服務(wù)器不允許執(zhí)行存儲數(shù)據(jù)的操作

192.168.4.52:6379> set name bob

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

192.168.4.52:6379>


配置一主多從

在host53主機(jī)做如下配置

[root@host53 ~]# redis-cli? -h 192.168.4.53 -p 6379??

192.168.4.53:6379> slaveof? 192.168.4.51? 6379?? 指定主服務(wù)器的ip 和端口

OK

?

在master服務(wù)host51 查看復(fù)制信息

[root@host51 ~]# redis-cli? -h 192.168.4.51 -p 6379

192.168.4.51:6379> info? replication

# Replication

role:master

connected_slaves:2?? 有2臺從服務(wù)器

slave0:ip=192.168.4.52,port=6379,state=online,offset=294,lag=0?

slave1:ip=192.168.4.51,port=6379,state=online,offset=294,lag=0?

?

?

配置主從從結(jié)構(gòu)

??? 在host53主機(jī)做如下配置

??????? 第1步 把一主多從結(jié)構(gòu)中的host53主機(jī)恢復(fù)為獨立的數(shù)據(jù)庫服務(wù)器

??????? 第2步:把host53配置為 host52主機(jī)的slave服務(wù)器

??? [root@host53 ~]# redis-cli? -h 192.168.4.53 -p 6379??

??? 192.168.4.53:6379>? slaveof? no? one

??? OK

??? 192.168.4.53:6379>? slaveof? 192.168.4.52 6379

? 在host52主機(jī)查看復(fù)制信息

??? #既有主服務(wù)器的信息又有從服務(wù)器的信息

192.168.4.52:6379> info replication

# Replication

role:slave

master_host:192.168.4.51

master_port:6379

master_link_status:up

....

connected_slaves:1

slave0:ip=192.168.4.53,port=6379,state=online,offset=2310,lag=0

.....



配置帶驗證的主從復(fù)制

??????????? 意思就是:

?????????? 主從結(jié)構(gòu)中的master服務(wù)器設(shè)置了連接密碼,

?????????? slave服務(wù)器要指定連接密碼才能正常同步master主機(jī)數(shù)據(jù)

第一步 給Redis服務(wù)器Host51 設(shè)置連接密碼 123456?

第二步 在redis服務(wù)器host52 設(shè)置連接 master服務(wù)器? host51的連接密碼? 并設(shè)置本機(jī)redis服務(wù)的連接密碼為123456

第三步 在redis服務(wù)器host53 設(shè)置連接 master服務(wù)器? host52的連接密碼

?第一步:給主服務(wù)器51 設(shè)置連接密碼

[root@host51 ~]# redis-cli? -h 192.168.4.51 -p 6379

192.168.4.51:6379> config get requirepass

1) "requirepass"

2) ""

192.168.4.51:6379> config set requirepass 123456

OK

192.168.4.51:6379> config get requirepass

(error) NOAUTH Authentication required.

192.168.4.51:6379>

192.168.4.51:6379> auth 123456

OK

192.168.4.51:6379> config get requirepass

1) "requirepass"

2) "123456"

192.168.4.51:6379> config rewrite

OK

192.168.4.51:6379> exit

[root@host51 ~]# tail -1 /etc/redis/6379.conf

requirepass "123456"

[root@host51 ~]#

?第二步 :配置redis服務(wù)器 host52

[root@host52 ~]# redis-cli? -h 192.168.4.52? -p 6379

192.168.4.52:6379> config get requirepass

1) "requirepass"

2) ""

192.168.4.52:6379> config set requirepass 123456

OK

192.168.4.52:6379> auth 123456

OK

192.168.4.52:6379> config get masterauth

1) "masterauth"

2) ""

192.168.4.52:6379> config set masterauth 123456

OK

192.168.4.52:6379> config get masterauth

1) "masterauth"

2) "123456"

192.168.4.52:6379> config rewrite

OK

192.168.4.52:6379>

192.168.4.52:6379> info replication

192.168.4.52:6379> info replication

# Replication

role:slave

master_host:192.168.4.51

master_port:6379

master_link_status:up

....

....

192.168.4.52:6379> exit

[root@host52 redis-4.0.8]# tail -2 /etc/redis/6379.conf

masterauth "123456"

requirepass "123456"

[root@host52 redis-4.0.8]#

?第三步 在redis服務(wù)器host53 設(shè)置連接 master服務(wù)器? host52的連接密碼

[root@host53 ~]# redis-cli? -h 192.168.4.53

192.168.4.53:6379> config set masterauth 123456

OK

192.168.4.53:6379> slaveof 192.168.4.52 6379

OK

192.168.4.53:6379> config rewrite

OK

192.168.4.53:6379>

192.168.4.53:6379> info? replication

# Replication

role:slave

master_host:192.168.4.52

master_port:6379

master_link_status:up

....

192.168.4.53:6379> exit


哨兵服務(wù)

??什么是哨兵服務(wù):監(jiān)視主從復(fù)制結(jié)構(gòu)中主服務(wù)器,發(fā)現(xiàn)主服務(wù)器無法連接后,會把對應(yīng)的從升級為主數(shù)據(jù)庫服務(wù)器,繼續(xù)監(jiān)視新的主數(shù)據(jù)庫服務(wù)器,壞掉的主數(shù)據(jù)庫服務(wù)器恢復(fù)后,會自動做當(dāng)前主服務(wù)器的從主機(jī)。哨兵服務(wù)+redis主從服務(wù) 能夠?qū)崿F(xiàn)redis服務(wù)高可用和數(shù)據(jù)的自動備份,但遠(yuǎn)比Redis集群的資金成本和運維成本要低。

說明:

??? ??? 1)可以使用一主一從或 一主多從 或 主從從?? +?? 哨兵服務(wù) 做服務(wù)的高可用 和 數(shù)據(jù)自動備份?

?????? 2)如果主從結(jié)構(gòu)中的redis服務(wù)設(shè)置連接密碼的話必須全每臺數(shù)據(jù)庫都要設(shè)置密碼且密碼要一樣,

??????? 3)宕機(jī)的服務(wù)器 啟動服務(wù)后,要人為指定主服務(wù)器的連接密碼? ?

步驟一? 給host53主機(jī)的Redis服務(wù)設(shè)置連接密碼? 要與另外2臺Redis服務(wù)器設(shè)置一樣的連接密碼

[root@host53 ~]# redis-cli? -h 192.168.4.53

192.168.4.53:6379> config set requirepass 123456

OK

192.168.4.53:6379> auth 123456

OK

192.168.4.53:6379> config rewrite

OK

192.168.4.53:6379>

步驟二:配置哨兵服務(wù)(192.168.4.57)

??????????????? 具體操作步驟:

??????????????????? 1)安裝源碼軟件redis (無需做初始化配置,如果做了初始化把redis服務(wù)停止即可)

??????????????????? 2)創(chuàng)建并編輯主配置文件 (說明源碼包里有哨兵服務(wù)配置文件的模板sentinel.conf)

? 1) 安裝源碼軟件redis (無需做初始化配置,如果做了初始化把redis服務(wù)停止即可)

[root@redis57 ~ ]# yum -y install gcc

[root@redis57 ~ ]# tar -zxf redis-4.0.8.tar.gz

[root@redis57 redis]# cd redis-4.0.8/

[root@redis1 redis-4.0.8]# make

[root@redis1 redis-4.0.8]# make install?????????

?2)創(chuàng)建并編輯主配置文件

??????????????? #說明源碼包里有哨兵服務(wù)配置文件的模板sentinel.conf

???????????? [root@host57 ~]# vim? /etc/sentinel.conf

???????? ??? bind? 192.168.4.57

???????????? sentinel?? monitor??? redis_server?? 192.168.4.51?? 6379?? 1

???????????? sentinel auth-pass??? redis_server?? 123456?? #如果主服務(wù)器沒有連接密碼此配置項可用省略

???????????? :wq

??3)啟動哨兵服務(wù)? (會占用當(dāng)前終端顯示啟動信息)

?[root@mgm57 redis-4.0.8]# nohup redis-sentinel? /etc/sentinel.conf? &

[1] 5701

[root@mgm57 redis-4.0.8]# nohup: 忽略輸入并把輸出追加到"nohup.out"?? 回車即可

?[root@mgm57 redis-4.0.8]# jobs? 查看當(dāng)前終端后臺進(jìn)程

[1]+? 運行中?????????????? nohup redis-sentinel /etc/sentinel.conf &


?3)查看哨兵服務(wù)主配置文件 監(jiān)視的主服務(wù)器 自動修改為 從服務(wù)器的ip

[root@host57 ~]# cat /etc/sentinel.conf

.....

sentinel monitor redis_server 192.168.4.52 6379 1

.....

sentinel auth-pass redis_server 123456

sentinel known-slave redis_server 192.168.4.53 6379

sentinel known-slave redis_server 192.168.4.51 6379



二、持久化 ???????(理論多)

????? 2.1、什么是持久化: redis服務(wù)可以永久的保存數(shù)據(jù),怎么實現(xiàn)的呢?

? ?? 2.2、實現(xiàn)方式有2種:分別是 RDB文件 和? AOF文件

? ? ? ? ? 1) RDB文件

???????????????????????? 指定就是數(shù)據(jù)庫目錄下的 dump.rdb 文件

??? ???????????????????? redis運行服務(wù)后,會根據(jù)配置文件的設(shè)置的存盤頻率 把內(nèi)存里的數(shù)據(jù)復(fù)制到數(shù)據(jù)庫目錄下的dump.rdb文件里(覆蓋保存)

? ? ? ???2)AOF文件

????????????????? redis服務(wù)AOF文件(與mysql服務(wù)的binlog日志文件的功能相同)

?????????? ????? 是一個文件,記錄連接redis服務(wù)后執(zhí)行的寫操作命令并且是以追加的方式記錄寫操作命令

????????????????? 默認(rèn)沒有開啟,使用需要人為啟用。

? ? ? ??。?!說明:通過這2種文件 均可以實現(xiàn)對數(shù)據(jù)的手動備份和手動恢復(fù)!??!


RDB文件的使用

????????? 2.4.1、查看 redis服務(wù)存儲數(shù)據(jù)到硬盤的存盤頻率

vim /etc/redis/6379.conf (默認(rèn) 219行 220行 221行)?? ?

save? 秒?? 變量的個數(shù)

save 900?? 1

save 300?? 10

save 60??? 10000


使用RDB文件 實現(xiàn)數(shù)據(jù)的備份和恢復(fù)

?????????????? 第1步:拷貝 dump.rdb 文件就是對數(shù)據(jù)的備份

[root@host56 ~]# cp /var/lib/redis/6379/dump.rdb? /opt/

[root@host56 ~]# ls /opt/*.rdb

/opt/dump.rdb

使用備份的dump.rdb文件恢復(fù)

????????????????? ??????? 第1步? 停止內(nèi)存沒有數(shù)據(jù)的redis服務(wù)

????????????????? ??????? 第2步? 把沒有數(shù)據(jù)的dump.rdb文件刪除

????????????????? ??????? 第3步 把備份dump.rdb文件拷貝到數(shù)據(jù)庫目錄下

????????????????? ??????? 第4步 啟動redis服務(wù) 并連接服務(wù)查看數(shù)據(jù)

[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6379? shutdown

[root@host56 ~]# rm -rf /var/lib/redis/6379/dump.rdb

[root@host56 ~]# cp /opt/dump.rdb? /var/lib/redis/6379/

[root@host56 ~]# /etc/init.d/redis_6379 start

Starting Redis server...

[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6379

192.168.4.56:6379> keys *

?1) "i"

?2) "d"

?3) "x"

.4.4 RDB方式的優(yōu)/缺點

?????????? 優(yōu)點:

?????????????? 高性能的持久化實現(xiàn) —— 創(chuàng)建一個子進(jìn)程來執(zhí)行持久化,先將數(shù)據(jù)寫入臨時文件,持久化過程結(jié)束后,再用這個臨時文件替換上次持久化好的文件;

?????????????? 過程中主進(jìn)程不做任何IO操作比較適合大規(guī)模數(shù)據(jù)恢復(fù),且對數(shù)據(jù)完整性要求不是非常高的場合

? ? ? ? ? ?缺點:意外宕機(jī)時,丟失最后一次持久化的所有數(shù)據(jù)


AOF文件的使用?

??????????????? 2.5.1 啟用AOF文件

????????????? ??????? 1、通過修改配置文件啟用(修改文件需要重啟redis服務(wù)才能生效,所以時適合線下服務(wù)器)

注意:通過配置文件啟用AOF文件? 服務(wù)器原有的數(shù)據(jù)會被刪除

?vim /etc/redis/6379.conf???????

appendonly yes?????? ? 啟用 aof 文件??

appendfilename "appendonly.aof"? 日志文件名??

:wq

[root@host53 ~]# /etc/init.d/redis_6379 start

Starting Redis server...

[root@host53 ~]# ls /var/lib/redis/6379/?? 存放在日志目錄下

appendonly.aof? dump.rdb

連接服務(wù)存儲數(shù)據(jù)后 再查看文件行數(shù)

[root@host53 ~]# redis-cli? -h 192.168.4.53 -p 6379

192.168.4.53:6379> keys *? 刪除了服務(wù)器原有的數(shù)據(jù)


命令行啟動(馬上生效且不需要重啟redis服務(wù),也不會刪除原有的數(shù)據(jù) 所以適合線上服務(wù)器)

[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6379

192.168.4.56:6356> config set? appendonly yes??? 啟用aof文件

OK

192.168.4.56:6356> config rewrite? 永久有效

OK

192.168.4.56:6356> keys *

(empty list or set)

192.168.4.56:6356> set x 1

OK

192.168.4.56:6356> set y 2

OK

192.168.4.56:6356> set z 3

OK

192.168.4.56:6356> keys *

1) "x"

2) "z"

3) "y"

192.168.4.56:6356> exit

[root@host56 ~]# ls /var/lib/redis/6379/*.aof????? 數(shù)據(jù)庫目錄多了.aof文件

/var/lib/redis/6379/appendonly.aof

[root@host56 ~]# ls /var/lib/redis/6379/*.rdb

/var/lib/redis/6379/dump.rdb


使用AOF文件 實現(xiàn)數(shù)據(jù)的備份和恢復(fù)

?????? ??????? 第1步? 備份aof文件

[root@host56 ~]# cp /var/lib/redis/6379/appendonly.aof? /opt/

[root@host56 ~]# ls /opt/*.aof

/opt/appendonly.aof

[root@host56 ~]#

??????

?????? ??????? 第2步? 使用備份的aof文件恢復(fù)數(shù)據(jù)

????????????? ?????? #刪除數(shù)據(jù)

[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6379

192.168.4.56:6379> flushall

192.168.4.56:6379> exit

? ? ? ? ? ? #恢復(fù)數(shù)據(jù)

???????????????????? ??????? 第1步: 把沒有數(shù)據(jù)的服務(wù)停止

???????????????????? ?[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6356? shutdown

? ? ? ? ? ? ? ? ? ? ? ? ? ?第2步: 刪除沒有數(shù)據(jù)的aof文件和rdb文件

???????????????????? ?[root@host56 ~]# rm -rf? /var/lib/redis/6379/*

? ? ? ? ? ? ? ? ? ? ? 第3步:把備份的aof文件拷貝到數(shù)據(jù)庫目錄

???????????????????? [root@host56 ~]# cp /opt/appendonly.aof? /var/lib/redis/6379/

? ? ? ? ? ? ? ? ? ? ? 第4步:啟動redis服務(wù)并查看數(shù)據(jù)

???????????????????????? [root@host56 ~]# /etc/init.d/redis_6379 start

???????????????????????? Starting Redis server...


? 使用自帶命令修改有問題的aof文件

????? 命令格式 [root@host56 ~]# redis-check-aof --fix /目錄名/日志文件名

?redis-cli? -h 192.168.4.56 -p 6379 shutdown? 停止服務(wù)

[root@host56 ~]# echo "daaaa" >> /var/lib/redis/6379/appendonly.aof?? 在日志追加錯誤格式的命令

[root@host56 ~]# /etc/init.d/redis_6379 start 啟動服務(wù)

Starting Redis server...

[root@host56 ~]# redis-cli? -h 192.168.4.56 -p 6379? 連接服務(wù)

Could not connect to Redis at 192.168.4.56:6379: Connection refused? 提示連接失敗 說明服務(wù)沒啟動

Could not connect to Redis at 192.168.4.56:6379: Connection refused

not connected> exit? 退出登錄提示

?[root@host56 ~]# tail -1 /var/log/redis_6379.log? 查看日志文件的最后1行 (最新的日志信息都記錄在日志文件的末尾) ,會顯示報錯原因的解決辦法

5662:M 26 Nov 16:53:09.530 # Bad file format reading the append only file: make a backup of your AOF file, then use ./redis-check-aof --fix <filename>

?[root@host56 ~]# redis-check-aof --fix /var/lib/redis/6379/appendonly.aof 恢復(fù)日志

0x???????????? ?78: Expected prefix '*', got: 'd'

AOF analyzed: size=126, ok_up_to=120, diff=6

This will shrink the AOF from 126 bytes, with 6 bytes, to 120 bytes

Continue? [y/N]: y? 同意修復(fù)????

Successfully truncated AOF

?[root@host56 ~]# /etc/init.d/redis_6379 start? 啟動服務(wù)

/var/run/redis_6379.pid exists, process is already running or crashed? 提示PID文件存儲

[root@host56 ~]# rm? -rf /var/run/redis_6379.pid 刪除文件

?[root@host56 ~]# /etc/init.d/redis_6379 start 啟動服務(wù)

Starting Redis server...

[root@host56 ~]# redis-cli? -h 192.168.4.56 -p 6379 連接服務(wù)查看數(shù)據(jù)

192.168.4.56:6379> keys *? 查看數(shù)據(jù)

1) "a"

2) "e"

3) "b"

4) "c"

5) "d"

192.168.4.56:6379>

與aof 相關(guān)的配置項 ( vim /etc/redis/6379.conf )

??????????????????? appendfsync? always???? ?//時時記錄,并完成磁盤同步

??????????????????? appendfsync? everysec? ?????//每秒記錄一次,并完成磁盤同步

??????????????????? appendfsync? no?????????? ?? //寫入aof ,不執(zhí)行磁盤同步

??????????????????? auto-aof-rewrite-min-size?? 64mb?? //首次重寫觸發(fā)值

??????????????????? auto-aof-rewrite-percentage? 100?? //再次重寫,增長百分比

.5.6 AOF文件的優(yōu)缺點

??????????????????? AOF優(yōu)點

??????????????????????????? 可以靈活設(shè)置持久化方式

??????????????????????????? 出現(xiàn)意外宕機(jī)時,僅可能丟失1秒的數(shù)據(jù)

? ? ? ? ? ? ? ? ? ? ? AOF缺點

??????????????????????????? 持久化文件的體積通常會大于RDB方式

??????????????????????????? 執(zhí)行fsync策略時的速度可比RDB方式慢


redis安裝 集群 教程的評論 (共 條)

分享到微博請遵守國家法律
南漳县| 广平县| 天台县| 玛曲县| 沙洋县| 老河口市| 新昌县| 瑞金市| 德钦县| 镇赉县| 静宁县| 毕节市| 扬中市| 盐山县| 巨野县| 海晏县| 镇江市| 鞍山市| 泰和县| 三台县| 长沙县| 乳山市| 庐江县| 酒泉市| 永德县| 尼玛县| 麟游县| 齐河县| 甘肃省| 河西区| 商南县| 赫章县| 民县| 石狮市| 岳阳市| 普兰店市| 连山| 克什克腾旗| 共和县| 荥经县| 平乡县|