Redis 安装
相关网址: https://www.redis.net.cn/order/
Docker 版本启动:
创建工作目录,并在工作目录中 创建响应的文件夹和文件/xxx/redis/{conf,data},在 conf 目录下创建配置文件,
启动(工作目录在 /opt 路径下)
docker 运行的命令:
docker run --name 容器名称 --restart=always -p 6379:6379 -v /opt/redis/data:/data -v /opt/redis/conf/redis.conf:/etc/redis/redis.conf --privileged=true -d 621ceef7494a /etc/redis/redis.conf --appendonly yes
docker-compose 方式启动(同样挂载数据卷,设置密码):
Dockerfile:
ARG REDIS_VERSION=latest FROM redis:${REDIS_VERSION} WORKDIR /data ENTRYPOINT ["redis-server", "--port", "6379", "--appendonly" ,"yes","--requirepass"]
# 构建时候定义默认密码 CMD ["asia8888"]
docker-compose.yml
version: "3" services: iredis: build: context: ./storage/redis dockerfile: Dockerfile container_name: iredis image: iredis:latest restart: always ports: - 6379:6379 volumes: - ./storage/redis/data:/data # 启动的时候定义密码 command: IRedis@1234
执行启动
docker-compose up iredis
打开Redis配置文件:
# Redis configuration file example. # # Note that in order to read the configuration file, Redis must be # started with the file path as first argument: # # ./redis-server /path/to/redis.conf # Note on units: when memory size is needed, it is possible to specify # it in the usual form of 1k 5GB 4M and so forth: # # 1k => 1000 bytes (10进制,与2进制配置说明) # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 bytes # 1g => 1000000000 bytes # 1gb => 1024*1024*1024 bytes # # units are case insensitive so 1GB 1Gb 1gB are all the same. 。。。。。。。。。。。 46 ################################## NETWORK ##################################### 47 48 # By default, if no "bind" configuration directive is specified, Redis listens 49 # for connections from all the network interfaces available on the server. 50 # It is possible to listen to just one or multiple selected interfaces using 51 # the "bind" configuration directive, followed by one or more IP addresses. 52 # 53 # Examples: 54 # 55 # bind 192.168.1.100 10.0.0.1 56 # bind 127.0.0.1 ::1 57 # 58 # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the 59 # internet, binding to all the interfaces is dangerous and will expose the 60 # instance to everybody on the internet. So by default we uncomment the 61 # following bind directive, that will force Redis to listen only into 62 # the IPv4 loopback interface address (this means Redis will be able to 63 # accept connections only from clients running into the same computer it 64 # is running). 65 # 66 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES 67 # JUST COMMENT THE FOLLOWING LINE. 68 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 69 bind 127.0.0.1 (若远程访问,则需要将此处注释) 70 71 # Protected mode is a layer of security protection, in order to avoid that 72 # Redis instances left open on the internet are accessed and exploited. 73 # 74 # When protected mode is on and if: 75 # 76 # 1) The server is not binding explicitly to a set of addresses using the 77 # "bind" directive. 78 # 2) No password is configured. 79 # 80 # The server only accepts connections from clients connecting from the 81 # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain 82 # sockets. 83 # 84 # By default protected mode is enabled. You should disable it only if 85 # you are sure you want clients from other hosts to connect to Redis 86 # even if no authentication is configured, nor a specific set of interfaces 87 # are explicitly listed using the "bind" directive. 88 protected-mode yes(若远程访问,此处改为 no) 132 ################################# GENERAL ##################################### 133 134 # By default Redis does not run as a daemon. Use 'yes' if you need it. (如果需要以后台的形式启动,no 改为 yes) 135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. 136 daemonize no (守护进程启动,即后台形式启动,改为 yes) 。。。。。。。。。。。。 157 # nothing bad happens, the server will start and run normally. 158 pidfile /var/run/redis_6379.pid (进程Id文件位置,设置进程锁文件 , 程序启动的时候创建此文件,退出程序后删除此文件) 159 160 # Specify the server verbosity level. 161 # This can be one of: 162 # debug (a lot of information, useful for development/testing) 163 # verbose (many rarely useful info, but not a mess like the debug level) (verbose冗余模式) 164 # notice (moderately verbose, what you want in production probably) 165 # warning (only very important / critical messages are logged) 166 loglevel notice (日志级别) 167 168 # Specify the log file name. Also the empty string can be used to force 169 # Redis to log on the standard output. Note that if you use standard 170 # output for logging but daemonize, logs will be sent to /dev/null (任何文件写入到 /dev/null ,都将会被丢弃) 171 logfile "" (默认是打印到控制台的,不做保存, > /dev/null 进行丢弃,需要保存可自定义日志文件路径,/opt/Redis/redis.log)指定日志目录时,没有日志文件可以,Redis会自动创建,但是必须有目录, 。。。。。。。。。。。。。。 255 # The working directory. (这是Redis的工作目录,持久化文件存储的位置) 256 # 257 # The DB will be written inside this directory, with the filename specified 258 # above using the 'dbfilename' configuration directive. 259 # 260 # The Append Only File will also be created inside this directory. 261 # 262 # Note that you must specify a directory here, not a file name. 263 dir ./ (Redis持久化文件存储的位置,改为结对路径形式的位置:如:/opt/Redis/DB) 。。。。。。。。 494 ################################## SECURITY ################################### 495 496 # Require clients to issue AUTH <PASSWORD> before processing any other 497 # commands. This might be useful in environments in which you do not trust 498 # others with access to the host running redis-server. 499 # 500 # This should stay commented out for backward compatibility and because most 501 # people do not need auth (e.g. they run their own servers). 502 # 503 # Warning: since Redis is pretty fast an outside user can try up to 504 # 150k passwords per second against a good box. This means that you should 505 # use a very strong password otherwise it will be very easy to break. 506 # 507 # requirepass foobared (设置密码:把注释去掉 如:requirepass 明文密码)
二进制模式安装启动:按配置文件启动:
[root@chxlay bin]# ./redis-server ./redis.conf
查看启动的日志文件(自定义的日志文件路径)
[root@chxlay bin]# vim ../redis.log 8734:C 22 Apr 2020 23:53:02.819 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 8734:C 22 Apr 2020 23:53:02.820 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=8734, just started 8734:C 22 Apr 2020 23:53:02.820 # Configuration loaded 8735:M 22 Apr 2020 23:53:02.852 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 8735 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'
客户端启动测试:
-bash: ./redsi-cli: No such file or directory [root@izt4nfucqy81qzrm4bz741z bin]# ./redis-cli 127.0.0.1:6379> ping (error) NOAUTH Authentication required. (设置了密码,无法直接访问,需要先授权密码) 127.0.0.1:6379> auth asia8888 (密码授权) OK 127.0.0.1:6379> ping PONG 127.0.0.1:6379>
主从复制启动:
每个配置文件中相同的参数是:
|
daemonize yes |
|
dir /usr/local/cluster-redis |
不同的参数有:
|
配置项名称 |
作用 |
取值 |
|
port |
Redis服务器启动后监听的端口号 |
6000 7000 8000 |
|
dbfilename |
RDB文件存储位置 |
dump6000.rdb dump7000.rdb dump8000.rdb |
|
logfile |
日志文件位置 |
/var/logs/redis6000.log /var/logs/redis7000.log /var/logs/redis8000.log |
|
pidfile |
pid文件位置 |
/var/run/redis6000.pid /var/run/redis7000.pid /var/run/redis8000.pid |
准备工作目录:
准备 Redis 集群中每个成员的工作目录
[root@chxlay Redis]# mkdir redis-cluster [root@chxlay Redis]# ll 总用量 16 drwxr-xr-x. 2 root root 4096 4月 22 23:52 bin drwxr-xr-x. 2 root root 4096 4月 23 00:20 DB drwxr-xr-x. 2 root root 4096 4月 23 00:20 redis-cluster -rw-r--r--. 1 root root 3152 4月 23 00:12 redis.log
将原始的配置文件复制到具区中,每个服务器一份配置文件:
[root@chxlay redis-5.0.5]# cp ./redis.conf /opt/Redis/redis-cluster/redis6000.conf [root@chxlay redis-5.0.5]# cp ./redis.conf /opt/Redis/redis-cluster/redis7000.conf [root@chxlay redis-5.0.5]# cp ./redis.conf /opt/Redis/redis-cluster/redis8000.conf
[root@chxlay redis-cluster]# ll 总用量 192 -rw-r--r--. 1 root root 61797 4月 23 00:23 redis6000.conf -rw-r--r--. 1 root root 61797 4月 23 00:24 redis7000.conf -rw-r--r--. 1 root root 61797 4月 23 00:24 redis8000.conf
按照既定计划修改redis6000.conf中的相关配置项
daemonize yes
132 ################################# GENERAL ##################################### 133 134 # By default Redis does not run as a daemon. Use 'yes' if you need it. 135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. 136 daemonize yes
Dbfilename
247 # 248 # RDB files created with checksum disabled have a checksum of zero that will 249 # tell the loading code to skip the check. 250 rdbchecksum yes 251 252 # The filename where to dump the DB 253 dbfilename dump.rdb (改成自定义的名字,如: dump6000.rdb)
dir (文件目录可以集群共用):
255 # The working directory. 256 # 257 # The DB will be written inside this directory, with the filename specified 258 # above using the 'dbfilename' configuration directive. 259 # 260 # The Append Only File will also be created inside this directory. 261 # 262 # Note that you must specify a directory here, not a file name. 263 dir /opt/Redis/DB
port
89 90 # Accept connections on the specified port, default is 6379 (IANA #815344). 91 # If port 0 is specified Redis will not listen on a TCP socket. 92 port 6379 (改成指定的端口号)
logfile
168 # Specify the log file name. Also the empty string can be used to force 169 # Redis to log on the standard output. Note that if you use standard 170 # output for logging but daemonize, logs will be sent to /dev/null 171 logfile "" (如:logfile "/opt/Redis/redis6000.log")
pidfile
155 # 156 # Creating a pid file is best effort: if Redis is not able to create it 157 # nothing bad happens, the server will start and run normally. 158 pidfile /var/run/redis_6379.pid (如改为:redis_6000.pid)
分别以不同的配置文件为参数启动,就启动集群了:
[root@chxlay redis-5.0.5]# /opt/Redis/bin/redis-server /opt/Redis/redis-cluster/redis6000/redis6000.conf [root@chxlay redis-5.0.5]# /opt/Redis/bin/redis-server /opt/Redis/redis-cluster/redis7000/redis7000.conf [root@chxlay redis-5.0.5]# /opt/Redis/bin/redis-server /opt/Redis/redis-cluster/redis8000/redis8000.conf
[root@chxlay ~]# netstat -anp | grep redis tcp 0 0 127.0.0.1:6000 0.0.0.0:* LISTEN 9789/redis-server 1 tcp 0 0 127.0.0.1:7000 0.0.0.0:* LISTEN 9802/redis-server 1 tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN 9865/redis-server 1
登录客户端查看(未设置主从关系,每个服务器都自认为是主服务器)
每个服务器都认为自己是 master
[root@chxlay ~]# /opt/Redis/bin/redis-cli -p 6000 127.0.0.1:6000> info replication # Replication role:master connected_slaves:0 master_replid:9c382e4806e92aab5b500957aaeafbdbda7689ee master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6000> [root@chxlay redis-5.0.5]# /opt/Redis/bin/redis-cli -p 7000 127.0.0.1:7000> info replication # Replication role:master connected_slaves:0 master_replid:86481e41dfdd2aca4e0addc3c7f601ec7648cffa master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:7000> [root@chxlay redis8000]# /opt/Redis/bin/redis-cli -p 8000 127.0.0.1:8000> info replication # Replication role:master connected_slaves:0 master_replid:af961225b1f95e79548629a99de07be9c218d52e master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:8000>
手动设置集群主从服务关系(从服务器宕机以后重启后会恢复为主服务器角色)
127.0.0.1:7000> slaveof 127.0.0.1 6000 OK 127.0.0.1:7000> info replication # Replication role:slave master_host:127.0.0.1 master_port:6000 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:14 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:70347fa8b86cf86eb4b944ae019a6b9165e4aa9c master_replid2:0000000000000000000000000000000000000000 master_repl_offset:14 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:14 127.0.0.1:7000>
127.0.0.1:8000> slaveof 127.0.0.1 6000 OK 127.0.0.1:8000> info replication # Replication role:slave master_host:127.0.0.1 master_port:6000 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:42 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:70347fa8b86cf86eb4b944ae019a6b9165e4aa9c master_replid2:0000000000000000000000000000000000000000 master_repl_offset:42 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:29 repl_backlog_histlen:14 127.0.0.1:8000>
127.0.0.1:6000> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=7000,state=online,offset=182,lag=1 slave1:ip=127.0.0.1,port=8000,state=online,offset=182,lag=1 master_replid:70347fa8b86cf86eb4b944ae019a6b9165e4aa9c master_replid2:0000000000000000000000000000000000000000 master_repl_offset:182 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:182 127.0.0.1:6000>
手动设置主从关系,若从服务器宕机,再次重启,则失去了主从关系,再次启动的从服务器角色变为master ,
若主服务器宕机,从服务器不会改变角色,依然是 slave,此时服务器集群只可以读数据,不可写数据,主服务器再次启动,回复主从关系
取消主从关系
SLAVEOF NO ONE
哨兵模式(一个哨兵也是一个服务器)
主观下线
1台哨兵检测到某节点服务器下线。
客观下线
认为某个节点服务器下线的哨兵服务器达到指定数量。这个数量后面在哨兵的启动配置文件中指定。
简单起见我们只配置一台哨兵。我们所需要做的就是创建一个哨兵服务器运行所需要的配置文件。 sentinel.conf
|
格式 |
sentinel monitor 为主机命名 主机IP 主机端口号 将主机判定为下线时需要Sentinel同意的数量 |
|
例子 |
sentinel monitor(固定值) mymaster(主服务器起名) 127.0.0.1 6000(主服务器IP:端口号) 1(客观下线数量,有一个哨兵判定主服务器下线就判定为下线) |
启动哨兵
/***/***/bin/redis-server /***/***/***/sentinel.conf --sentinel
[root@chxlay redis-cluster]# /opt/Redis/bin/redis-server /opt/Redis/redis-cluster/sentinel.conf --sentinel 10635:X 23 Apr 2020 01:50:10.803 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 10635:X 23 Apr 2020 01:50:10.803 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=10635, just started 10635:X 23 Apr 2020 01:50:10.803 # Configuration loaded 10635:X 23 Apr 2020 01:50:10.805 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 10635 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 10635:X 23 Apr 2020 01:50:10.807 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 10635:X 23 Apr 2020 01:50:10.808 # Sentinel ID is 95b9d3b3099c42ee1050ab02f5bb53aca0d6dfce 10635:X 23 Apr 2020 01:50:10.808 # +monitor master mymaster 127.0.0.1 6000 quorum 1 10635:X 23 Apr 2020 01:50:10.809 * +slave slave 127.0.0.1:7000 127.0.0.1 7000 @ mymaster 127.0.0.1 6000 10635:X 23 Apr 2020 01:50:10.810 * +slave slave 127.0.0.1:8000 127.0.0.1 8000 @ mymaster 127.0.0.1 6000
+sdown master mymaster 127.0.0.1 6379 【主观下线】
+odown master mymaster 127.0.0.1 6379 #quorum 1/1【客观下线】
……
+vote-for-leader 17818eb9240c8a625d2c8a13ae9d99ae3a70f9d2 1【选举leader】
……
+failover-state-send-slaveof-noone slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster
127.0.0.1 6379【把一个从机设置为主机】
-------------挂掉的主机又重新启动---------------------
-sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381【离开主观下线状态】
+convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381【转换为从机】
可以发福的关闭各个服务器,查看主从关系的变化
127.0.0.1:8000> SHUTDOWN
not connected> quit
本文来自博客园,作者:Vermeer,转载请注明原文链接:https://www.cnblogs.com/chxlay/p/15092308.html

浙公网安备 33010602011771号