相关文章推荐
风流的松树  ·  【Eclipse DDMS】 Can't ...·  2 月前    · 
豁达的课本  ·  解决 Can't bind to ...·  2 月前    · 
阳刚的红茶  ·  javascript - Failed ...·  2 年前    · 
爱看书的大脸猫  ·  C++ 操作DLL的函数 ...·  2 年前    · 
干练的西装  ·  第 1 章 - Azure RTOS ...·  2 年前    · 

启动一个新的Redis实例,监听6380端口,跟之前的6379不冲突:

[root@cs redis]# ps -ef | grep redis-server
root       8622      1  0 10:28 ?        00:00:00 redis-server *:6379
root       8637   8548  0 10:37 pts/1    00:00:00 redis-server *:6380
root       8641   8074  0 10:38 pts/0    00:00:00 grep --color=auto redis-server

配置文件启动

默认的,Redis的配置文件,在Redis的安装目录下,我们可以在启动时使用配置文件启动:

[root@cs redis]# pwd
/opt/software/redis
[root@cs redis]# ls |grep redis.conf 
redis.conf
[root@cs redis]# redis-server redis.conf 

如上示例,使用默认配置启动Redis服务。这里只做演示,工作中我们需要自己维护配置文件。

启动后,可以通过下面命令来查看Redis服务:

ps -ef | grep redis
netstat -antpl|grep redis
redis-cli -h ip -p port ping

对于上述三种启动方式,建议:

  • 生产环境建议选择配置文件启动
  • 单机多实例配置文件可以用端口进行区分
  • 客户端的返回值说明

    当Redis服务启动后,就可以通过Redis客户端连接它了:

    redis-cli -h 10.0.0.200 -p 6379 
    > set hello world
    > get hello 
    "world"
    > exit                  # 退出客户端,或者CTRL + C也行
    

    在与客户端的交互中,通常由以下几种形式的回复。

    状态回复:

    错误回复:

    127.0.0.1:6379> set hello world
    127.0.0.1:6379> get hello
    "world"
    127.0.0.1:6379> hget hello field
    (error) WRONGTYPE Operation against a key holding the wrong kind of value
    

    整数回复:

    127.0.0.1:6379> incr hello
    (integer) 1
    

    字符串回复:

    > get hello 
    "world"
    

    多行字符串回复:

    > mget hello foo
    "world"
    "bar"
    

    可以通过客户端的命令来启动。

    # 方式1,直接调用命令
    [root@cs src]# redis-cli shutdown
    # 方式2,进入客户端,执行命令
    [root@cs src]# redis-cli 
    127.0.0.1:6379> shutdown
    4878:M 26 Dec 19:47:02.368 # User requested shutdown...
    4878:M 26 Dec 19:47:02.368 * Saving the final RDB snapshot before exiting.
    4878:M 26 Dec 19:47:02.370 * DB saved on disk
    4878:M 26 Dec 19:47:02.370 # Redis is now ready to exit, bye bye...
    

    详细配置参考:https://www.cnblogs.com/Neeo/articles/13952586.html

    通常,单机多实例环境下,都是通过端口号来区分各实例,所以我们需要自己维护配置文件,我们通常在Redis目录建立两个目录:

    [root@cs redis]# mkdir -p /data/redis_data/6379
    [root@cs redis]# vim /data/redis_data/6379/redis.conf
    

    现在只需要这些初始配置即可:

    daemonize yes
    port 6379
    logfile "/data/redis_data/6379/redis.log"
    dir "/data/redis_data/6379"
    dbfilename dump.rdb
    
  • daemonize:是否后台运行。
  • port:Redis对外端口号,默认是6379。在单机多实例中,必须配置。
  • logfile:Redis系统日志。
  • dir:Redis工作目录。
  • dbfilename:RDB持久化数据文件。
  • 现在再次使用配置文件启动:

    [root@cs redis]# redis-server /data/redis_data/6379/redis.conf
    

    我们可以通过设置配置文件来决定谁能操作Redis:

    bind 127.0.0.1 10.0.0.200
    requirepass 1234
    
  • bind控制谁能访问,多个IP以空格分割。
  • requirepass控制访问密码。
  • 注意,Redis没有用户名的概念,而且谁都可以登录到Redis,但想操作Redis的话,就要通过Redis的认证了。
    所以,要想通过认证,有两种方式,第一种,登录和认证一起:

    [root@cs 6379]# redis-cli -h 127.0.0.1 -p 6379 -a 1234
    

    第二种, 先登录,完事再认证:

    [root@cs 6379]# redis-cli -h 127.0.0.1 -p 6379 
    127.0.0.1:6379> set hello world                # 认证之前,没有操作权限
    (error) NOAUTH Authentication required.
    127.0.0.1:6379> auth 1234                        # 认证
    127.0.0.1:6379> set hello world
    127.0.0.1:6379> get hello
    "world"
    

    Mac配置redis.conf

    首先将redis的src目录添加到~/.bash_profile环境变量。

    vim /etc/redis.conf bind 0.0.0.0 requirepass 1234 # 重启,然后以配置文件启动 redis-server /etc/redis.conf # 客户端登录进去 redis-cli 127.0.0.1:6379> auth 123

    在线查看和修改配置

    我们可以在线查看和修改Redis的配置,来个示例:

    127.0.0.1:6379> CONFIG GET bind    # 查看指定配置
    1) "bind"
    2) "127.0.0.1 10.0.0.200"
    127.0.0.1:6379> CONFIG GET *       # 查看所有的配置,Redis中大约有70个配置
    127.0.0.1:6379> CONFIG GET b*      # 支持模糊查询
    127.0.0.1:6379> CONFIG SET requirepass 1234   # 在线修改配置
    

    注意,这种修改是临时,当重启Redis服务时,配置将失效,如果要永久生效,还是要将配置写入到配置文件中。

    that's all, see also:

    老男孩-标杆班级-NoSQL-lesson13-Redis缓存技术-运维篇