部署Nginx 部署Redis 安裝Redis redis事物 伺服器管理命令 慢查詢日誌 主從複製 Redis-Sentinel cluser分區叢集 安裝python 操作redis資料 部署Django程式
部署Nginx
配置epel源
參看阿里網https://opsx.alibaba.com/mirror
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
##epel 配置方法###1、備份(如有配置其他epel源)mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backupmv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup2、下載新repo 到/etc/yum.repos.d/epel(RHEL 7) wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repoepel(RHEL 6) wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repoepel(RHEL 5) wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-5.repo
yum repolist
[root@safly ~]# yum repolistLoaded plugins: fastestmirrorepel | 4.7 kB 00:00 (1/3): epel/x86_64/group_gz | 266 kB 00:01 (2/3): epel/x86_64/updateinfo | 917 kB 00:05 (3/3): epel/x86_64/primary_db | 6.3 MB 00:20 Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.comrepo id repo name statusbase/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 9,591epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_ 12,509extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 448updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 2,416repolist: 24,964[root@safly ~]#
安裝
yum install sl cowsay -y
安裝nginx
wget http://nginx.org/download/nginx-1.12.2.tar.gz yum install pcre-devel openssl-devel -y #編譯安裝三部曲 : ./configure make make install tar xf nginx-1.12.2.tar.gz cd nginx-1.12.2 ./configure --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_modulemakemake install
#檢查文法 /application/nginx-1.12.2/sbin/nginx -t#啟動nginx/application/nginx-1.12.2/sbin/nginx
重啟
[root@safly nginx-1.12.2]# /application/nginx-1.12.2/sbin/nginx -s reload
部署Redis 安裝Redis
安裝redis
[root@safly /]# lsapplication boot dev home lib64 mnt proc run sbin sys usr wyfbin data etc lib media opt root safly srv tmp var[root@safly /]# cd /application/[root@safly application]# lsnginx-1.12.2[root@safly application]# wget http://download.redis.io/releases/redis-3.2.10.tar.gz[root@safly application]# tar xzf redis-3.2.10.tar.gz[root@safly application]# lsnginx-1.12.2 redis-3.2.10 redis-3.2.10.tar.gz[root@safly application]# mv redis-3.2.10 redis[root@safly application]# rm -rf redis-3.2.10.tar.gz [root@safly application]# lsnginx-1.12.2 redis[root@safly application]# cd redis[root@safly redis]# makesrc/redis-server
用戶端串連測試
[root@oldboyedu-s6 redis]# src/redis-cli 127.0.0.1:6379> 127.0.0.1:6379> set foo barOK127.0.0.1:6379> 127.0.0.1:6379> get foo"bar"127.0.0.1:6379> 127.0.0.1:6379> shutdown
配置redis
daemonize yesport 1111logfile /var/log/redis.logdbfilename dump.rdbdir /data/redisappendonly yesbind 10.0.0.200 127.0.0.1requirepass 123
擷取當前配置
CONFIG GET *
通過配置啟動redis
添加完成後重啟redis [root@oldboyedu-s6 ~]# /application/redis/src/redis-cli -p 1111127.0.0.1:1111> shutdown /application/redis/src/redis-server /etc/redis.conf 登入測試:[root@safly /]# /application/redis/src/redis-cli -h 10.0.0.128 -p 111110.0.0.128:1111>
密碼認證登陸
/application/redis/src/redis-cli -h 10.0.0.200 -a 123 -p 1111或者: /application/redis/src/redis-cli -h 10.0.0.200 -p 1111 auth 123
或者
(error) NOAUTH Authentication required.127.0.0.1:1111> auth 123OK
持久化
在 /etc/redis.conf中添加以下內容dbfilename dump.rdbdir /data/redissave 900 1save 300 10save 60 10000
redis事物
DISCARD
取消事務,放棄執行事務塊內的所有命令。
EXEC
執行所有事務塊內的命令。
MULTI
標記一個事務塊的開始。
UNWATCH
取消 WATCH 命令對所有 key 的監視。
WATCH key [key …]
監視一個(或多個) key ,如果在事務執行之前這個(或這些) key 被其他命令所改動,那麼事務將被打斷。
multi
[root@safly /]# /application/redis/src/redis-cli -p 1111 -a 123127.0.0.1:1111> multiOK127.0.0.1:1111> discardOK127.0.0.1:1111> multiOK127.0.0.1:1111> set foo barQUEUED127.0.0.1:1111> exec1) OK
watch
127.0.0.1:1111> get ticket(nil)127.0.0.1:1111> set ticket 1OK127.0.0.1:1111> get ticket"1"127.0.0.1:1111> watch ticketOK127.0.0.1:1111> multiOK127.0.0.1:1111> decr ticketQUEUED127.0.0.1:1111> exec1) (integer) 0127.0.0.1:1111>
另開一個用戶端
127.0.0.1:1111> clear127.0.0.1:1111> multiOK127.0.0.1:1111> decr ticketQUEUED127.0.0.1:1111> exec1) (integer) -1127.0.0.1:1111> get ticket"-1"127.0.0.1:1111>
伺服器管理命令
InfoClinet listClient kill ip:portconfig get *CONFIG RESETSTAT 重設統計CONFIG GET/SET 動態修改DbsizeFLUSHALL 清空所有資料 select 1FLUSHDB 清空當前庫MONITOR 監控即時指令SHUTDOWN 關閉伺服器save將當前資料儲存SLAVEOF host port 主從配置SLAVEOF NO ONESYNC 主從同步ROLE返回主從角色
慢查詢日誌
Slow log 是 Redis 用來記錄查詢執行時間的日誌系統。slow log 儲存在記憶體裡面,讀寫速度非常快可以通過改寫 redis.conf 檔案或者用 CONFIG GET 和 CONFIG SET 命令對它們動態地進行修改slowlog-log-slower-than 10000 超過多少微秒CONFIG SET slowlog-log-slower-than 100CONFIG SET slowlog-max-len 1000 儲存多少條慢日誌CONFIG GET slow*SLOWLOG GETSLOWLOG RESET
主從複製
使用非同步複製。
一個主伺服器可以有多個從伺服器。
從伺服器也可以有自己的從伺服器。
複製功能不會阻塞主伺服器。
可以通過複製功能來讓主伺服器免於執行持久化操作,由從伺服器去執行持久化操作即可。
配置主從複製
準備兩個或兩個以上redis執行個體
mkdir -p /data/6380/mkdir -p /data/6381/mkdir -p /data/6382/
三套設定檔樣本:vim /data/6380/redis.conf----------------bind 127.0.0.1 10.0.0.128port 6380daemonize yespidfile /data/6380/redis.pidloglevel noticelogfile "/data/6380/redis.log"dbfilename dump.rdbdir /data/6380appendonly noappendfilename "appendonly.aof"appendfsync everysecslowlog-log-slower-than 10000slowlog-max-len 128protected-mode novim /data/6381/redis.conf----------------bind 127.0.0.1 10.0.0.128port 6381daemonize yespidfile /data/6381/redis.pidloglevel noticelogfile "/data/6381/redis.log"dbfilename dump.rdbdir /data/6381appendonly noappendfilename "appendonly.aof"appendfsync everysecslowlog-log-slower-than 10000slowlog-max-len 128protected-mode novim /data/6382/redis.conf----------------bind 127.0.0.1 10.0.0.128port 6382daemonize yespidfile /data/6382/redis.pidloglevel noticelogfile "/data/6382/redis.log"dbfilename dump.rdbdir /data/6382appendonly noappendfilename "appendonly.aof"appendfsync everysecslowlog-log-slower-than 10000slowlog-max-len 128protected-mode no啟動:/application/redis/src/redis-server /data/6380/redis.conf/application/redis/src/redis-server /data/6381/redis.conf/application/redis/src/redis-server /data/6382/redis.conf查看啟動狀態netstat -lnp|grep 638ps -ef |grep redis
關聯主從
[root@safly ~]# /application/redis/src/redis-cli -p 6381127.0.0.1:6381> SLAVEOF 127.0.0.1 6380OK127.0.0.1:6381> exit[root@safly ~]# /application/redis/src/redis-cli -p 6382127.0.0.1:6382> SLAVEOF 127.0.0.1 6380OK127.0.0.1:6382> exit[root@safly ~]#
主從庫資料複製
主庫
[root@safly ~]# /application/redis/src/redis-cli -p 6380127.0.0.1:6380> keys *(empty list or set)127.0.0.1:6380> set foo strOK
從庫
[root@safly ~]# /application/redis/src/redis-cli -p 6381127.0.0.1:6381> keys *(empty list or set)127.0.0.1:6381> get foo"str"127.0.0.1:6381>
Redis-Sentinel
Redis-Sentinel是Redis官方推薦的高可用性(HA)解決方案,當用Redis做Master-slave的高可用方案時,假如master宕機了,Redis本身(包括它的很多用戶端)都沒有實現自動進行主備切換,而Redis-sentinel本身也是一個獨立啟動並執行進程,它能監控多個master-slave叢集,發現master宕機後能進行自動切換
功能:
監控(Monitoring):
Sentinel 會不斷地檢查你的主伺服器和從伺服器是否運作正常。
提醒(Notification):
當被監控的某個 Redis 伺服器出現問題時, Sentinel 可以通過 API 向管理員或者其他應用程式發送通知。
自動故障遷移(Automatic failover):
當一個主伺服器不能正常工作時, Sentinel 會開始一次自動故障遷移操作, 它會將失效主伺服器的其中一個從伺服器升級為新的主伺服器, 並讓失效主伺服器的其他從伺服器改為複製新的主伺服器; 當用戶端試圖串連失效的主伺服器時, 叢集也會向用戶端返回新主伺服器的地址, 使得叢集可以使用新主伺服器代替失效伺服器。
sentinel配置叢集
mkdir /data/26380cp /application/redis/src/redis-sentinel /data/26380cd /data/26380vim sentinel.confport 26380dir "/tmp"sentinel monitor mymaster 127.0.0.1 6380 1sentinel down-after-milliseconds mymaster 5000sentinel config-epoch mymaster 0啟動 ./redis-sentinel ./sentinel.conf
python3串連redis sentinel叢集
cluser分區叢集
安裝
EPEL源安裝ruby支援yum install ruby rubygems -y使用國內源gem sources -a http://mirrors.aliyun.com/rubygems/ gem sources --remove http://rubygems.org/gem sources -lgem install redis -v 3.3.3
如果無法使用,可以使用aliyun
gem sources -a http://mirrors.aliyun.com/rubygems/
gem sources –remove http://rubygems.org/
配置
建立節點目錄:mkdir -p /data/7000mkdir -p /data/7001mkdir -p /data/7002mkdir -p /data/7003mkdir -p /data/7004mkdir -p /data/7005
設定檔添加:---------------------