目录
redis 命令大全
redis架构
功能配置 | 10.0.0.65 | 10.0.0.66 | 10.0.0.67 |
---|---|---|---|
hostname | 65 | 66 | 67 |
hsots | cat > /etc/hosts <<EOF 10.0.0.65 65 10.0.0.66 66 10.0.0.67 67 EOF |
cat > /etc/hosts <<EOF 10.0.0.65 65 10.0.0.66 66 10.0.0.67 67 EOF |
cat > /etc/hosts <<EOF 10.0.0.65 65 10.0.0.66 66 10.0.0.67 67 EOF |
主从复制 | 主库 哨兵 |
从库 | 从库 |
集群高可用 | master | slave | slave |
安装
65
rm -rf /root/.ssh
/bin/ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ""
sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.66 -o StrictHostKeyChecking=no
sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.67 -o StrictHostKeyChecking=no
656667
mkdir -p /data/soft
mkdir -p /data/redis_cluster/redis_6379
mkdir -p /opt/redis_cluster/redis_6379/{conf,pid,logs}
cd /data/soft/
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar zxf redis-5.0.0.tar.gz -C /opt/redis_cluster/
ln -s /opt/redis_cluster/redis-5.0.0/ /opt/redis_cluster/redis
cd /opt/redis_cluster/redis
make && make install
yum makecache fast
yum install rubygems -y
gem sources --remove https://rubygems.org/
gem sources -a http://mirrors.aliyun.com/rubygems/
gem update –system
gem install redis -v 3.3.5
ruby -v
gem -v
yum install python-pip gcc -y
pip install --upgrade pip
pip install rdbtools
65
cat > /opt/redis_cluster/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 10.0.0.65 127.0.0.1
port 6379
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
databases 16
dbfilename redis_6379.rdb
dir /data/redis_cluster/redis_6379
save 60 100
save 300 10
save 600 1
EOF
66
cat > /opt/redis_cluster/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 10.0.0.66 127.0.0.1
port 6379
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
databases 16
dbfilename redis_6379.rdb
dir /data/redis_cluster/redis_6379
save 60 100
save 300 10
save 600 1
EOF
67
cat > /opt/redis_cluster/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 10.0.0.67 127.0.0.1
port 6379
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
databases 16
dbfilename redis_6379.rdb
dir /data/redis_cluster/redis_6379
save 60 100
save 300 10
save 600 1
EOF
656667
#启动redis数据库
redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf
#进入redis数据库
redis-cli
#关闭redis数据库 redis-cli shutdown
主从复制
6667
redis-cli
SLAVEOF 10.0.0.65 6379
查看主从复制状态
主库65
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
从库6667
[root@66 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.65
master_port:6379
-----------------------------
[root@67 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.65
master_port:6379
哨兵
65 (主)
cat > /opt/redis_cluster/redis_26379/conf/redis_26379.conf<<EOF
bind 10.0.0.65
port 26379
daemonize yes
logfile /opt/redis_cluster/redis_26379/logs/redis_26379.log
dir /data/redis_cluster/redis_26379
sentinel monitor mymaster 10.0.0.65 6379 2
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 18000
EOF
#注意!此时主从复制状态为一主两从
#启动哨兵:
redis-sentinel /opt/redis_cluster/redis_26379/conf/redis_26379.conf
# 查看哨兵配置文件
cat /opt/redis_cluster/redis_26379/conf/redis_26379.conf
## 配置文件发生变化说明哨兵运行成功
# 注意!不要自己去改动哨兵的配置文件
redis集群
修改配置文件
65
cat > /opt/redis_cluster/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 10.0.0.65 127.0.0.1
port 6379
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
databases 16
dbfilename redis_6379.rdb
dir /data/redis_cluster/redis_6379
cluster-enabled yes
cluster-config-file nodes_6379.conf
cluster-node-timeout 15000
EOF
66
cat > /opt/redis_cluster/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 10.0.0.66 127.0.0.1
port 6379
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
databases 16
dbfilename redis_6379.rdb
dir /data/redis_cluster/redis_6379
cluster-enabled yes
cluster-config-file nodes_6379.conf
cluster-node-timeout 15000
EOF
67
cat > /opt/redis_cluster/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 10.0.0.67 127.0.0.1
port 6379
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
databases 16
dbfilename redis_6379.rdb
dir /data/redis_cluster/redis_6379
cluster-enabled yes
cluster-config-file nodes_6379.conf
cluster-node-timeout 15000
EOF
65、66、67
redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf
发现所有节点
65
redis-cli -h 10.0.0.65 -p 6379 cluster meet 10.0.0.66 6379
redis-cli -h 10.0.0.65 -p 6379 cluster meet 10.0.0.67 6379
redis-cli
#查看节点
127.0.0.1:6379> cluster nodes
分配槽位---槽位共16383
65
redis-cli -h 10.0.0.65 -p 6379 cluster addslots {0..16383}
10.0.0.65:6379> CLUSTER NODES #槽位
26b0bac41e4fff8039152104309340b16cfd3e14 10.0.0.67:6379 master - 0 1562253484470 2 connected
ce47edb5daa884bf5366f902a4e78068813ba0df 10.0.0.66:6379 master - 0 1562253488498 1 connected
f7dd5b276f31507b41f6f6bf41519b1df6491a8f 10.0.0.65:6379 myself,master - 0 0 0 connected 0-16383
高可用
6667
10.0.0.66:6379> CLUSTER REPLICATE f7dd5b276f31507b41f6f6bf41519b1df6491a8f
10.0.0.67:6379> CLUSTER REPLICATE f7dd5b276f31507b41f6f6bf41519b1df6491a8f
10.0.0.65:6379> CLUSTER NODES
ce47edb5daa884bf5366f902a4e78068813ba0df 10.0.0.66:6379 slave f7dd5b276f31507b41f6f6bf41519b1df6491a8f 0 1562254234804 1 connected
26b0bac41e4fff8039152104309340b16cfd3e14 10.0.0.67:6379 slave f7dd5b276f31507b41f6f6bf41519b1df6491a8f 0 1562254235814 2 connected
f7dd5b276f31507b41f6f6bf41519b1df6491a8f 10.0.0.65:6379 master - 0 0 0 connected 0-16383
扩展redis管理脚本
65、66、67
[root@65 ~]# cat redis_shell.sh
#!/bin/bash
USAG(){
echo "sh $0 {start|stop|restart|login|ps|tail} PORT"
}
if [ "$#" = 1 ]
then
REDIS_PORT='6379'
elif
[ "$#" = 2 -a -z "$(echo "$2"|sed 's#[0-9]##g')" ]
then
REDIS_PORT="$2"
else
USAG
exit 0
fi
REDIS_IP=$(hostname -I|awk '{print $1}')
PATH_DIR=/opt/redis_cluster/redis_${REDIS_PORT}/
PATH_CONF=/opt/redis_cluster/redis_${REDIS_PORT}/conf/redis_${REDIS_PORT}.c onf
PATH_LOG=/opt/redis_cluster/redis_${REDIS_PORT}/logs/redis_${REDIS_PORT}.lo g
CMD_START(){
redis-server ${PATH_CONF}
}
CMD_SHUTDOWN(){
redis-cli -c -h ${REDIS_IP} -p ${REDIS_PORT} shutdown
}
CMD_LOGIN(){
redis-cli -c -h ${REDIS_IP} -p ${REDIS_PORT}
}
CMD_PS(){
ps -ef|grep redis
}
CMD_TAIL(){
tail -f ${PATH_LOG}
}
case $1 in
start)
CMD_START
CMD_PS
;;
stop)
CMD_SHUTDOWN
CMD_PS
;;
restart)
CMD_START
CMD_SHUTDOWN
CMD_PS
;;
login)
CMD_LOGIN
;;
ps)
CMD_PS
;;
tail)
CMD_TAIL
;;
*)
USAG
esac