之前用的版本是4.0的這次搭建用5.05版本
不同之處,就是在5.0之後的版本可以直接用客戶端工具直接創建集羣,不需要安裝其他工具,很方便.
安裝這個的時候,遇到一個問題,就是當主庫掛了的時候,從庫不會晉升爲主庫…後面排查結果
問題是主機授權masterauth參數沒有添加,導致從庫一直鏈接失敗,從而不能晉升主庫.
一.安裝前準備
關閉SELinux
[root@localhost ~]# setenforce 0
[root@localhost ~]# echo "/usr/sbin/setenforce 0"
[root@localhost ~]# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
[root@localhost ~]# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
設置防火牆
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]#vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
[root@localhost ~]#service iptables restart
時鐘校對
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# echo "#Clock synchronization" >> /var/spool/cron/root
[root@localhost ~]# echo "01 00 * * * /sbin/ntpdate ntp2.aliyun.com;/sbin/hwclock -w" >> /var/spool/cron/root
調整文件打開數
[root@localhost ~]# cat <<EOF > /etc/security/limits.d/20-nproc.conf
> root soft nofile 102400
> root hard nofile 102400
> EOF
安裝gcc
[root@localhost ~]#yum -y install gcc gcc-c++
二.安裝red is
下載redis安裝包
[root@localhost ~]#wget http://download.redis.io/releases/redis-5.0.5.tar.gz
解壓
[root@localhost ~]#tar zxvf redis-5.0.5.tar.gz
[root@localhost ~]#cd redis-5.0.5/
編譯安裝
[root@localhost redis-5.0.5]#make MALLOC=libc
[root@localhost redis-5.0.5]#make && make install
創建文件目錄
[root@localhost redis-5.0.5]#mkdir -p /opt/redis/{db,etc,bin,logs}
將啓動文件拷貝至文件目錄
[root@localhost redis-5.0.5]#cd src/
[root@localhost src]#cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-server redis-cli /opt/redis/bin
修改配置文件/opt/redis/etc/redis.conf
bind 0.0.0.0 #綁定的IP地址,0.0.0.0對所有開放
protected-mode no #關閉保護模式,讓其他主機連接
port 6379 #服務端口
tcp-backlog 511 #tcp連接等待隊列長度
timeout 0
tcp-keepalive 300
daemonize yes #開啓後臺模式
supervised no
pidfile /var/run/redis_6379.pid #進程文件
loglevel notice
logfile /opt/redis/logs/redis.log #日誌文件
databases 1 #數據庫的個數,默認16,由於集羣不支持切換庫,所有隻有一個庫
always-show-logo yes
#指定在多長時間內,有多少次更新操作,就將數據同步到數據文件,可以多個條件配合
save <seconds> <changes>
Redis默認配置文件中提供了三個條件:
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes #後臺備份出錯時,是否停止寫入keys到哈希槽
rdbcompression yes #數據文件是否壓縮
rdbchecksum yes#數據文件執行校驗
dbfilename dump-6379.rdb #指定本地數據庫文件名,默認值爲dump.rdb
dir /opt/redis/db/ #指定本地數據庫存放目錄
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
masterauth 123456 #主機授權密碼
requirepass 123456 #數據庫密碼
maxclients 10000 #最大客戶端連接數
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof" #啓動Redis上啓用了AOF,則會加載AOF,這是具有更好的耐久性保證的文件,相當於MySQL的二進制文件
appendfsync everysec #磁盤上刷新數據,每一秒鐘一次
no-appendfsync-on-rewrite no #如果您有延遲問題,請將其轉換爲"yes"。否則,從持久性的角度來看,設置爲"no"是最安全的選擇。
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb #自動重寫附加文件
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
cluster-enabled yes #開啓集羣
cluster-config-file node1-6379.conf #集羣節點配置文件,由Redis節點自動創建和更新的,非人工創建
cluster-node-timeout 15000 #集羣節點超時,連接不上的時間
cluster-slave-validity-factor 10 #從節點與主節點失聯多久,從節點嘗試升級成主節點。如果設置成正數,則cluster-node-timeout乘以cluster-slave-validity-factor得到的時間,是從節點與主節點失聯後,此從節點數據有效的最長時間,超過這個時間,從節點不會啓動故障遷移
cluster-migration-barrier 1 #主節點需要的最小從節點數,只有達到這個數,主節點失敗時,它從節點纔會進行遷移
cluster-require-full-coverage yes #在部分key所在的節點不可用時,如果此參數設置爲"yes"(默認值), 則整個集羣停止接受操作;如果此參數設置爲”no”,則集羣依然爲可達節點上的key提供讀操作。
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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10 #Redis調用一個內部函數來執行許多後臺任務
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
配置啓動腳本
#!/bin/sh
# chkconfig: 35 10 90
#重要!否則chkconfig不能識別開機啓動服務項;
#”35”爲運行級別,”10”爲啓動優先級,”90”爲關閉優先級
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
EXEC=/opt/redis/bin/redis-server
# CLIEXEC=/usr/local/bin/redis-cli
CLIEXEC=/opt/redis/bin/redis-cli
# PIDFILE=/var/run/redis_${REDISPORT}.pid
PIDFILE=/var/run/redis_${REDISPORT}.pid
# CONF="/etc/redis/${REDISPORT}.conf"
CONF="/opt/redis/etc/redis.conf"
USER=root #添加執行用戶變量
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis-server {start|stop|restart|force-reload}" >&2
exit 1
esac
將腳本放置/etc/init.d/redis-server
設置開機自啓
[root@localhost ~]#chkconfig --add redis-server
[root@localhost ~]#chkconfig --level 35 redis-server on #默認runlevel 35已打開
啓動腳本設置執行權限
[root@localhost ~]#chmod +x /etc/init.d/redis-server
啓動redis
#可通過啓動腳本啓動/關閉服務;或採用”service”指令
[root@localhost ~]#/etc/init.d/redis-server start
創建集羣
在5.0之前該工具是由ruby語言編寫的redis-trib.rb,在使用前需要安裝ruby語言環境。在5.0之後redis摒棄了該工具,將搭建集羣的功能合併到了redis-cli上。
這裏就可以直接創建集羣,不需要在安裝其他工具。
創建集羣需要注意,最少要6個服務,否則創建集羣失敗.
redis-cli --cluster create 110.110.100.35:6379 110.110.100.36:6379 110.110.100.37:6379 110.110.100.38:6379 110.10.1100.39:6379 110.10.100.40:6379 --cluster-replicas 1 -a 123456
參數說明
redis-cli 客戶端工具
–cluster create 創建一個新的集羣
–cluster-replicas 1 每一個主庫對應一個從庫
-a 數據庫密碼
出現下面的字樣說明創建成功
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
登陸驗證
redis-cli -c -p 端口
auth 密碼
刪除主節點
redis-cli --cluster reshard 110.110.100.35:6379
到這裏就搭建完成了
搭建過程中有的問題也有記錄在異常處理