redis安装之后,src和/usr/local/bin目录下多了几个以redis开头的可执行文件,我们统称为redis shell,这些可执行文件可以做很多事情,例如可以启动和停止redis、可以检测和修复redis的持久化文件,还可以检测redis的性能。

启动与配置

启动redis有三种方式:默认配置、运行配置、配置文件

默认配置

这种方法最为简单,不需要做过的配置,直接运行启动命令就可以启动redis server了。

[root@vultr redis]# redis-server 
20311:C 02 Sep 05:17:04.879 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
20311:M 02 Sep 05:17:04.882 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 20311
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

20311:M 02 Sep 05:17:04.885 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
20311:M 02 Sep 05:17:04.886 # Server started, Redis version 3.0.7
20311:M 02 Sep 05:17:04.886 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 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.
20311:M 02 Sep 05:17:04.886 * The server is now ready to accept connections on port 6379

通过以上的输出,可以看到直接使用redis-server启动redis后,会输出一些日志,通过日志可以看到一些信息,上面的例子中可以看到:

- redis的版本是3.0.7 64位的
- redis的端口是6379
- redis建议要使用配置文件来启动

因为直接启动无法自定义配置,所以这种方式是不会再生产环境中使用。

运行启动

redis-server加上要修改配置名和值(可以是多对),没有设置的配置将使用默认配置:

# redis-server --configKey1 configValuel1 --configKey2 configValuel2

例子:如果要用6390作为端口启动redis,可以执行:

[root@vultr redis]# redis-server --port 6390
20315:M 02 Sep 05:22:02.676 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6390
 |    `-._   `._    /     _.-'    |     PID: 20315
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

20315:M 02 Sep 05:22:02.678 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
20315:M 02 Sep 05:22:02.678 # Server started, Redis version 3.0.7
20315:M 02 Sep 05:22:02.678 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 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.
20315:M 02 Sep 05:22:02.679 * DB loaded from disk: 0.000 seconds
20315:M 02 Sep 05:22:02.679 * The server is now ready to accept connections on port 6390

虽然运行配置可以自定义配置,但是如果需要修改的配置较多或者希望将配置保存到文件中,不建议使用这种方法。

配置文件启动

将配置写到指定文件中,例如要把配置写到/opt/redis/redis.conf中,那么只需要执行如下命令即可启动redis。

[root@vultr redis]# cat /opt/redis/redis.conf # 编辑redis配置文件
port 6390 # 定义端口
logfile /var/log/redis/redis.log # 定义日志文件
#dir ./ # 定义redis的工作目录(存放持久化文件和日志文件)
daemonize yes  # 是否以守护进程的方法启动 yes or no

[root@vultr redis]# redis-server /opt/redis/redis.conf  #指定配置文件启动
[root@vultr redis]# cat /var/log/redis/redis.log  # 查看日志文件
20332:M 02 Sep 05:27:53.761 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6390
 |    `-._   `._    /     _.-'    |     PID: 20332
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

20332:M 02 Sep 05:27:53.763 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
20332:M 02 Sep 05:27:53.763 # Server started, Redis version 3.0.7
20332:M 02 Sep 05:27:53.764 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 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.
20332:M 02 Sep 05:27:53.764 * DB loaded from disk: 0.000 seconds
20332:M 02 Sep 05:27:53.764 * The server is now ready to accept connections on port 6390

通过上述输出的日志看到,我们自定义的redis.conf配置文件已经生效了。显然通过配置文件启动的方式提供了更大的灵活性,所以大部分生产环境会使用这种方式启动redis。

操作

现在已经启动了redis服务,接下来我们可以使用redis自带的命令行客户端redis-cli来对redis进行操作了。redis-cli可以使用两种方式来连接操作redis服务器。

交互式方式

通过redis-cli -h {host} -p {port}的方式连接、操作redis服务。之后的操作都是通过交互的方式实现,不需要再执行redis-cli了,例如:

[root@vultr redis]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"

命令方式

命令的方式可以用redis-cli -h ip {host} -p {port} {command}就可以直接得到命令的返回结果,例如:

[root@vultr redis]# redis-cli -h 127.0.0.1 -p 6379 get hello
"world"

这里需要注意两点:
1.如果没有-h参数,那么默认连接127.0.0.1;如果没有-p,那么久是默认的6379端口,也就是说如果-h和-p都没写就是连接127.0.0.1:6379这个redis实例。
2.redis-cli也是学习redis的重要工具

关闭(停止)

redis提供了shutdown命令来停止redis服务,例如要停掉我本机(127.0.0.1:6379)的redis服务,可以执行如下操作。

[root@vultr redis]# redis-cli shutdown
[root@vultr redis]# cat /var/log/redis/redis.log 
20379:M 02 Sep 05:47:03.017 # User requested shutdown... # 用户发送了shutdown命令
20379:M 02 Sep 05:47:03.017 * Removing the pid file. # 已导出pid 文件
20379:M 02 Sep 05:47:03.017 # Redis is now ready to exit, bye bye... # redis退出,关闭

当再次使用redis-cli连接该redis实例的时候,会看到redis已经不能连接了。

[root@vultr redis]# redis-cli 
Could not connect to Redis at 127.0.0.1:6379: Connection refused

此处需要注意三点:
1.redis关闭的过程:断开与客户端的连接、持久化文件生成,是一种相对优雅的关闭方式;
2.除了通过shutdown命令关闭redis服务以外,还可以粗暴的使用kiil -9 redis进程号关闭redis。但是尽量不要粗暴的使用kill -9强制杀死redis服务,不单不会做持久化操作,还会造成缓冲区等资源不能被优雅关闭,代表是否在关闭redis前,生成持久化文件:

[root@vultr redis]# redis-cli shutdown save | nosave
文章目录