Redis基础篇03 — 单机模式的启动和停止

概述

在 Redis 的单机模式下,有三种方式启动:

  • 前台启动
  • 后台启动
  • 带配置文件且以守护进程的方式后台启动(推荐)

前台启动和停止

此种方式主要用来做测试,使用也是非常简单,直接键入 redis-server 命令即可:

Shell > /usr/local/redis/bin/redis-server
2789:C 20 Dec 2023 20:41:02.772 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2789:C 20 Dec 2023 20:41:02.772 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2789:C 20 Dec 2023 20:41:02.772 * Redis version=7.2.3, bits=64, commit=00000000, modified=0, pid=2789, just started
2789:C 20 Dec 2023 20:41:02.772 # Warning: no config file specified, using the default config. In order to specify a config file use /usr/local/redis/bin/redis-server /path/to/redis.conf
2789:M 20 Dec 2023 20:41:02.772 * Increased maximum number of open files to 10032 (it was originally set to 1024).
2789:M 20 Dec 2023 20:41:02.772 * monotonic clock: POSIX clock_gettime
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 7.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2789
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

2789:M 20 Dec 2023 20:41:02.773 * Server initialized
2789:M 20 Dec 2023 20:41:02.773 * Ready to accept connections tcp

在输出的信息中,可以见到 Redis 运行在 standalone mode(单机模式)下,进程的 PID 为 2789 ,端口占用 6379,下方则是一些日志信息。

若要退出前台的 Redis ,ctrl + c 则可以退出。

后台启动和停止

# > 表示标准输出重定向,将标准输出(stdout)以覆盖的方式丢入到回收站,& 表示将程序放入到后台运行
Shell > nohup /usr/local/redis/bin/redis-server > /dev/null &

# 查阅后台的工作情况
Shell > jobs -l
[1]+  2801 运行中               nohup /usr/local/redis/bin/redis-server > /dev/null &

# 停止
Shell > killall redis-server
Shell > jobs -l
[1]+  2801 已完成               nohup /usr/local/redis/bin/redis-server > /dev/null

带配置文件且以守护进程的方式进行启动与停止

# 创建用于存放配置文件的目录
Shell > mkdir -p /usr/local/redis/conf/

# 将压缩包里的配置文件复制到目录中
Shell > cp -p /usr/local/src/redis-7.2.3/redis.conf /usr/local/redis/conf/

# 修改配置文件的这个参数的值 —— daemonize yes
## 关于这个配置文件里的参数,后续会专门介绍
Shell > vim  /usr/local/redis/conf/redis.conf
...
daemonize yes
...

修改完毕后,则可以进行启动:

Shell > /usr/local/redis/bin/redis-server  /usr/local/redis/conf/redis.conf

Shell > ss -tulnp
Netid   State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process
udp     UNCONN   0        0              127.0.0.1:323           0.0.0.0:*      users:(("chronyd",pid=732,fd=5))
udp     UNCONN   0        0                  [::1]:323              [::]:*      users:(("chronyd",pid=732,fd=6))
tcp     LISTEN   0        128              0.0.0.0:22            0.0.0.0:*      users:(("sshd",pid=756,fd=3))
tcp     LISTEN   0        511            127.0.0.1:6379          0.0.0.0:*      users:(("redis-server",pid=2950,fd=6))
tcp     LISTEN   0        128                 [::]:22               [::]:*      users:(("sshd",pid=756,fd=4))
tcp     LISTEN   0        511                [::1]:6379             [::]:*      users:(("redis-server",pid=2950,fd=7))

# 停止 Redis 实例
Shell > killall redis-server

若您需要开机自启动,则可以这样做:

Shell > chmod a+x /etc/rc.local

Shell > vim /etc/rc.local
...
/usr/local/redis/bin/redis-server  /usr/local/redis/conf/redis.conf &> /dev/null
...
用一杯咖啡支持我们,每一篇 [文档] 都经过我们实操,并非从网上一味的copy,期间花费了大量的心思,希望能够帮忙到您。
暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇