redis启动关闭

1. 启动redis服务

(1). 默认配置

执行:redis-server
2309:C 09 Dec 2020 19:12:26.997 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2309:C 09 Dec 2020 19:12:26.997 # Redis version=6.0.3, bits=64, commit=00000000, modified=0, pid=2309, just started
2309:C 09 Dec 2020 19:12:26.997 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2309:M 09 Dec 2020 19:12:26.998 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2309
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2309:M 09 Dec 2020 19:12:26.999 # Server initialized
2309:M 09 Dec 2020 19:12:26.999 * Loading RDB produced by version 6.0.3
2309:M 09 Dec 2020 19:12:26.999 * RDB age 377 seconds
2309:M 09 Dec 2020 19:12:26.999 * RDB memory usage when created 0.95 Mb
2309:M 09 Dec 2020 19:12:26.999 * DB loaded from disk: 0.000 seconds
2309:M 09 Dec 2020 19:12:26.999 * Ready to accept connections

日志会输出到终端,我们可以从日志中看到redis版本、监听的端口号、进程ID,RDB等信息。
其中In order to specify a config file use redis-server /path/to/redis.conf 说明了指定配置文件启动的方法。

(2). 启动时修改配置

启动时加上要修改的配置名和指(可以是多对),没有配置的将使用默认配置,使用redis-server -h or --help 查看可选的配置

redis-server --config1 config_value1 --config2 config_value2 
执行:redis-server --port 3307
2372:C 09 Dec 2020 19:26:07.750 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2372:C 09 Dec 2020 19:26:07.750 # Redis version=6.0.3, bits=64, commit=00000000, modified=0, pid=2372, just started
2372:C 09 Dec 2020 19:26:07.750 # Configuration loaded
2372:M 09 Dec 2020 19:26:07.751 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 3307
 |    `-._   `._    /     _.-'    |     PID: 2372
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2372:M 09 Dec 2020 19:26:07.752 # Server initialized
2372:M 09 Dec 2020 19:26:07.752 * Loading RDB produced by version 6.0.3
2372:M 09 Dec 2020 19:26:07.752 * RDB age 179 seconds
2372:M 09 Dec 2020 19:26:07.752 * RDB memory usage when created 0.95 Mb
2372:M 09 Dec 2020 19:26:07.752 * DB loaded from disk: 0.000 seconds
2372:M 09 Dec 2020 19:26:07.752 * Ready to accept connections

(3). 配置文件启动

如果是编译安装redis的安装包下会有redis.conf配置文件;如果使用yum,apt,brew等命令安装配置文件默认在/etc/redis/redis.conf。建议先备份原始配置文件,再copy一份修改。

执行:redis-server /etc/redis/redis.conf 

2. 停止redis服务

redis-cli [-h host] [-p port] shutdown [nosave|save]或 kill redis_pid
不指定-h,则默认为127.0.0.1
不指定-p,则默认为6379 
默认为save,生成持久化文件
2627:M 09 Dec 2020 19:42:12.256 # User requested shutdown...
2627:M 09 Dec 2020 19:42:12.256 * Saving the final RDB snapshot before exiting.
2627:M 09 Dec 2020 19:42:12.257 * DB saved on disk

redis关闭过程:断开与客户端的连接、持久化文件生成。不要使用kill -9 强制杀死redis服务,不但不会做持久化操作,还会造成缓冲区等资源不能被优雅关闭,极端情况会造成AOF和复制丢失数据的情况。

3. 主节点运行ID

每个Redis节点启动后都会动态分配一个40位的十六进制字符串作为运行ID。运行ID的主要作用是用来唯一识别Redis节点,比如从节点保存主节点的运行ID识别自己正在复制的是哪个主节点。如果只使用ip+port的方式识别主节点,那么主节点重启变更了整体数据集(如替换RDB/AOF文件),从节点再基于偏移量复制数据将是不安全的,因此当运行ID变化后从节点将做全量复制。查看运行ID:

127.0.0.1:6379> info server
# Server
redis_version:6.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:e244045ab2ddaac2
redis_mode:cluster
os:Darwin 20.3.0 x86_64
arch_bits:64
multiplexing_api:kqueue
atomicvar_api:atomic-builtin
gcc_version:4.2.1
process_id:2736
run_id:c89df1261b1a6d2b401008b0fa5b6624cb6a8852
tcp_port:6379
uptime_in_seconds:84988
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:3473414
executable:/private/etc/redis/6379/redis-server
config_file:/etc/redis/6379/redis.conf

Redis关闭再启动后,运行ID会随之改变。debug reload命令可以在不改变运行ID的情况下重启服务,debug reload命令可save当前的rdb文件,并清空当前数据库,重新加载rdb,加载与启动时加载类似,加载过程中只能执行部分只读请求(比如info、ping等)

server 6379 % redis-cli -a 123456 debug reload
OK

你可能感兴趣的:(redis启动关闭)