redis-部署=高可用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis-部署=高可用相关的知识,希望对你有一定的参考价值。
centos6.9环境部署
mkdir -p /data --创建目录
下载安装包:wget http://download.redis.io/releases/redis-3.2.6.tar.gz
解压:tar xf redis-3.2.6.tar.gz
进入解压目录进行编译:cd redis-3.2.6 && make
进入到/data目录:mkdir redis6380
复制到/redis6380目录:
cp redis-3.2.6/src/redis-server redis6380/
cp redis-3.2.6/redis.conf redis6380/
[[email protected] redis6380]# foo1=grep ‘^[^#|$]‘ redis.conf
&& echo "${foo1}" >redis.conf
配置文件: 1 bind 127.0.0.1
2 protected-mode yes
3 requirepass root
4 port 6380
5 tcp-backlog 511
6 timeout 0
7 tcp-keepalive 300
8 daemonize yes
9 supervised no
10 pidfile /var/run/redis_6380.pid
11 loglevel notice
12 logfile "redis6380.log"
启动:[[email protected] redis6380]# ./redis-server ./redis.conf
进行连接:
[[email protected] redis6380]# cd ../redis-3.2.6/src/
[[email protected] src]# ./redis-cli -p 6380
127.0.0.1:6380>
创建软连接:
[[email protected] src]# ln -s /data/redis-3.2.6/src/redis-cli /usr/bin/redis-cli
[[email protected] src]# cd
[[email protected] ~]# redis-cli -p 6380
127.0.0.1:6380>
进行用户认证:
127.0.0.1:6380> auth root
OK
redis主从切换
127.0.0.1:6380> info replication
Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6380> SHUTDOWN
not connected> exit
[[email protected] ~]#
创建redis6381目录
[[email protected] data]# ls
redis-3.2.6 redis-3.2.6.tar.gz redis6380
[[email protected] data]# mkdir redis6381
[[email protected] data]# cp redis-3.2.6/src/redis-server redis6381/
[[email protected] data]# cp redis-3.2.6/redis.conf redis6381
[[email protected] data]# cd redis6381
[[email protected] redis6381]# ls
redis.conf redis-server
[[email protected] redis6381]# foo1=grep ‘^[^#|$]‘ redis.conf
&& echo "${foo1}" >redis.conf
6381目录下的redis.conf配置文件进行一下修改
bind 127.0.0.1
protected-mode yes
requirepass root
port 6381
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6381.pid
loglevel notice
logfile "redis6381.log"
进行连接验证:
[[email protected] redis6381]# ./redis-server redis.conf
[[email protected] redis6381]# redis-cli -p 6381
127.0.0.1:6381> auth root
OK
127.0.0.1:6381> info replication
Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381> SHUTDOWN
not connected> exit
创建redis6382目录:
[[email protected] data]# cp redis-3.2.6/src/redis-server redis6382/
[[email protected] data]# cp redis-3.2.6/redis.conf redis6382
[[email protected] data]# ll
总用量 1528
drwxrwxr-x 6 root root 4096 12月 6 2016 redis-3.2.6
-rw-r--r-- 1 root root 1544806 12月 6 2016 redis-3.2.6.tar.gz
drwxr-xr-x 2 root root 4096 3月 8 19:06 redis6380
drwxr-xr-x 2 root root 4096 3月 8 19:19 redis6381
drwxr-xr-x 2 root root 4096 3月 8 19:21 redis6382
[[email protected] data]# cd redis6382
[[email protected] redis6382]# ls
redis.conf redis-server
[[email protected] redis6382]# foo1=grep ‘^[^#|$]‘ redis.conf
&& echo "${foo1}" >redis.conf
修改配置文件:
bind 127.0.0.1
protected-mode yes
requirepass root
port 6382
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6382.pid
loglevel notice
logfile "redis6382.log"
测试连接
[[email protected] redis6382]# ./redis-server redis.conf
[[email protected] redis6382]# redis-cli -p 6382
127.0.0.1:6382> auth root
OK
127.0.0.1:6382> info replication
Replication
role:master ----主
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6382>
此时三个都将成为主
此时我想让6381成为主,首先,先把6382和6380SHUTDOWN.
在6381连接窗口输入slaveof no one、让6381称为主,然后再启动6380和6382在连接窗口输入slaveof 127.0.0.1 6381(意思是指向主)
[[email protected] data]# mkdir s26383
[[email protected] data]# cp redis-3.2.6/src/redis-sentinel s26383/
[[email protected] data]# cp redis-3.2.6/sentinel.conf s26383/
[[email protected] data]#cd s26383/
[[email protected] s26383]# ls
redis-sentinel sentinel.conf
[[email protected] s26383]# foo1=grep ‘^[^#|$]‘ sentinel.conf
&& echo "${foo1}" >sentinel.conf
[[email protected] s26383]# cat sentinel.conf
port 26383
dir /tmp
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
启动:./redis-sentinel sentinel.conf
[[email protected] redis6381]# ll
总用量 8804
-rw-r--r-- 1 root root 92 3月 8 21:14 dump.rdb
-rw-r--r-- 1 root root 1173896 3月 8 21:37 redis6381.log
-rw-r--r-- 1 root root 1201 3月 8 19:15 redis.conf
-rwxr-xr-x 1 root root 7826304 3月 8 19:09 redis-server
[[email protected] redis6381]# cd ..
[[email protected] data]# cp -r redis6381/ redis6384
[[email protected] data]# ls
redis-3.2.6 redis-3.2.6.tar.gz redis6380 redis6381 redis6382 redis6384 s26383
[[email protected] data]# cd redis6384
[[email protected] redis6384]# ll
总用量 8820
-rw-r--r-- 1 root root 92 3月 8 21:37 dump.rdb
-rw-r--r-- 1 root root 1194800 3月 8 21:37 redis6381.log
-rw-r--r-- 1 root root 1201 3月 8 21:37 redis.conf
-rwxr-xr-x 1 root root 7826304 3月 8 21:37 redis-server
[[email protected] redis6384]# cat redis.conf |head -14
bind 127.0.0.1
protected-mode yes
requirepass root
port 6384
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6384.pid
loglevel notice
启动
[[email protected] redis6384]# ./redis-server ./redis.conf
[[email protected] redis6384]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Prog
tcp 0 0 127.0.0.1:6380 0.0.0.0: LISTEN 8303/./r
tcp 0 0 127.0.0.1:6381 0.0.0.0: LISTEN 8397/./r
tcp 0 0 127.0.0.1:6382 0.0.0.0: LISTEN 8402/./r
tcp 0 0 0.0.0.0:26383 0.0.0.0: LISTEN 8441/./r
tcp 0 0 127.0.0.1:6384 0.0.0.0: LISTEN 8469/./r
tcp 0 0 0.0.0.0:22 0.0.0.0: LISTEN 1223/ssh
tcp 0 0 :::26383 ::: LISTEN 8441/./r
tcp 0 0 :::22 ::: LISTEN 1223/ssh
[[email protected] redis6384]# redis-cli -p 6384
127.0.0.1:6384> auth root
OK
127.0.0.1:6384> slaveof 127.0.0.1 6380
OK
以上是关于redis-部署=高可用的主要内容,如果未能解决你的问题,请参考以下文章