Redis单节点多实例
环境
准备一台机器,现在我们可以搭建一个单节点多实例,也就是多个端口,一个端口设置为master,两个或者多个端口设置为slave
环境部署
安装gcc套装
[root@redis01 ~]# yum -y install gcc glibc glibc-kernheaders glibc-common glibc-devel make
升级gcc
[root@redis01 ~]# yum -y install centos-release-scl
[root@redis01 ~]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
[root@redis01 ~]# scl enable devtoolset-9 bash
设置永久升级
[root@redis01 ~]# echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
redis的安装
[root@redis01 ~]# cd /usr/local/src
[root@redis01 ~]# wget http://download.redis.io/releases/redis-6.0.5.tar.gz
[root@redis01 ~]# tar -zxvf redis-6.0.5.tar.gz
[root@redis01 ~]# cd redis-6.0.5/
[root@redis01 ~]# make
[root@redis01 ~]# make install PREFIX=/usr/local/redis
创建项目实例
[root@redis01 ~]# mkdir -p /application/redis/{6380,6381,6382}/{conf,data,log}
单节点准备多个实例,做少两个
[root@qfedu.com redis]# yum -y install tree
[root@qfedu.com redis]# tree
.
├── 6380
│ ├── redis.conf
│ └── redis-server
├── 6381
│ ├── redis.conf
│ └── redis-server
├── 6382
│ ├── redis.conf
│ └── redis-server
├── install-redis.sh
└── start-redis.sh
redis配置文件实例
[root@qfedu.com redis]# vim 6380/redis.conf
bind 0.0.0.0
port 6380
daemonize yes
pidfile /var/run/redis_6380.pid
loglevel notice
logfile "/var/log/redis_6380.log"
dbfilename dump.rdb
dir /application/redis/6380/
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
slowlog-log-slower-than 10000
slowlog-max-len 128
protected-mode no
requirepass redis #本机密码,可以不加密码
[root@qfedu.com redis]# vim 6381/redis.conf
bind 0.0.0.0
port 6381
daemonize yes
pidfile /var/run/redis_6381.pid
loglevel notice
logfile "/var/log/redis_6381.log"
dbfilename dump.rdb
dir /application/redis/6381/
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
slowlog-log-slower-than 10000
slowlog-max-len 128
protected-mode no
requirepass redis
replicaof 192.168.152.133 6380
masterauth redis
# 本机密码
#<masterip> <masterport> # 主节点 Redis 的 IP 和端口 注:低版本 Redis 有可能是 slaveof (只在从节点配置)
replicaof 192.168.152.133 6380
#<master-password> # 主节点上的密码 (只在从节点配置)
masterauth redis
[root@qfedu.com redis]# vim 6382/redis.conf
bind 0.0.0.0
port 6382
daemonize yes
pidfile /var/run/redis_6382.pid
loglevel notice
logfile "/var/log/redis_6382.log"
dbfilename dump.rdb
dir /application/redis/6382/
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
slowlog-log-slower-than 10000
slowlog-max-len 128
protected-mode no
requirepass redis # 本机密码
#<masterip> <masterport> # 主节点 Redis 的 IP 和端口 注:低版本 Redis 有可能是 slaveof (只在从节点配置)
replicaof 192.168.122.166 6380 #master的端口
#<master-password> # 主节点上的密码 (只在从节点配置)
masterauth redis #不加密码可以删除
启动实例
[root@qfedu.com redis]# /usr/local/redis/bin/redis-server /application/redis/6380/redis.conf
[root@qfedu.com redis]# /usr/local/redis/bin/redis-server /application/redis/6381/redis.conf
[root@qfedu.com redis]# /usr/local/redis/bin/redis-server /application/redis/6382/redis.conf
查看状态
[root@redis01 ~]# ss -anput | grep redis
tcp LISTEN 0 511 *:6381 *:* users:(("redis-server",pid=25993,fd=6))
tcp LISTEN 0 511 *:6382 *:* users:(("redis-server",pid=25995,fd=6))
tcp LISTEN 0 511 *:6383 *:* users:(("redis-server",pid=25997,fd=6))
tcp LISTEN 0 511 *:16381 *:* users:(("redis-server",pid=25993,fd=9))
tcp LISTEN 0 511 *:16382 *:* users:(("redis-server",pid=25995,fd=9))
tcp LISTEN 0 511 *:16382 *:* users:(("redis-server",pid=25997,fd=9))
端口显示成功,可以登录
主从复制的验证
[root@redis1 6380]# redis-cli -h 192.168.152.133 -p 6380 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.152.133:6380> slaveof no one
OK
192.168.152.133:6380> info replication
# Replication
role:master
connected_slaves:3
slave0:ip=192.168.152.133,port=6382,state=online,offset=28,lag=1
slave1:ip=192.168.152.133,port=6381,state=online,offset=28,lag=1
slave2:ip=192.168.152.133,port=6383,state=online,offset=28,lag=1
master_replid:ae5cc872d0b56a677d38a64c8e4bc810cfb43a5f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
192.168.152.133:6380> set name luoyinsheng
OK #添加内内容
192.168.152.133:6380> get name
"luoyinsheng" #可以查看到时哟添加的信息
192.168.152.133:6380> exit
[root@redis1 6380]# redis-cli -h 192.168.152.133 -p 6381 -a redis #加密码必须指定密码
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.152.133:6381> info replication
# Replication 查看状态,是master还是slave
role:slave
master_host:192.168.152.133
master_port:6380
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:358
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:ae5cc872d0b56a677d38a64c8e4bc810cfb43a5f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:358
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:358
192.168.152.133:6381> get name
"luoyinsheng"
若在slave上可以查到master的信息,证明自己的单节点多实例部署成功.