zoukankan      html  css  js  c++  java
  • Redis系列-远程连接redis并给redis加锁

    假设两台Redis服务器,ip分别为:192.168.1.101和192.168.1.103,如何在101上通过redis-cli访问103上的redis呢?在远程连接103之前,先讲下redis-cli的几个关键参数:

    用法:redis-cli [OPTIONS] [cmd [arg [arg ...]]]

    -h <主机ip>,默认是127.0.0.1

    -p <端口>,默认是6379

    -a <密码>,如果redis加锁,需要传递密码

    --help,显示帮助信息

    通过对rendis-cli用法介绍,在101上连接103应该很简单:

    [plain] view plain copy
     
    1. [root@xsf001 ~]# redis-cli -h 192.168.1.103 -p 6379  
    2. redis 192.168.1.103:6379>   

    在101上对103设置个个string值 user.1.name=zhangsan

    [plain] view plain copy
     
    1. redis 192.168.1.103:6379> set user.1.name zhangsan  
    2. OK  

            看到ok,表明设置成功了。然后直接在103上登陆,看能不能获取到这个值。

    [plain] view plain copy
     
    1. [root@xsf003 utils]# redis-cli   
    2. redis 127.0.0.1:6379> get user.1.name  
    3. "zhangsan"  

            木错吧,确实是zhangsan,这说明101上连的是103上的redis服务器。当然能够成功连接103是有基本条件的,101上可以喝103上的6379端口通信。

    人人都可以连接redis服务器是很危险的,我们需要给103上的redis设置个密码,怎么设置呢,需要编辑redis的配置文件/etc/redis/6379.conf

    [plain] view plain copy
     
    1. [root@xsf003 utils]# vim /etc/redis/6379.conf   

          找到# requirepass foobared 去掉前面的注释#,并把foobared 替换为你自己的密码:hi, coder 

    [plain] view plain copy
     
    1. requirepass "hi, coder"  

    保存配置文件之后,重启redis服务

    [plain] view plain copy
     
    1. [root@xsf003 utils]# /etc/init.d/redis_6379 stop  
    2. Stopping ...  
    3. Waiting for Redis to shutdown ...  
    4. Redis stopped  
    5. [root@xsf003 utils]# /etc/init.d/redis_6379 start  
    6. Starting Redis server...  

    101上重新连接103并获取user.1.name的值

    [plain] view plain copy
     
    1. [root@xsf001 ~]# redis-cli -h 192.168.1.103 -p 6379  
    2. redis 192.168.1.103:6379> get user.1.name  
    3. (error) ERR operation not permitted  
    4. redis 192.168.1.103:6379>   

            为什么是error呢,当然是因为连接103时没传递密码了,退出重新连

    [plain] view plain copy
     
    1. redis 192.168.1.103:6379> quit  
    2. [root@xsf001 ~]# redis-cli -h 192.168.1.103 -p 6379 -a "hi, coder"  
    3. redis 192.168.1.103:6379> get user.1.name  
    4. "zhangsan"  
  • 相关阅读:
    入门教程: JS认证和WebAPI
    ASP.NET Core 之 Identity 入门(二)
    在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序
    .Net Core+Angular Cli/Angular4开发环境搭建教程
    简单易用的.NET免费开源RabbitMQ操作组件EasyNetQ解析
    Razor
    一个简易的反射类库NMSReflector
    发布 Ionic iOS 企业级应用
    AngularJS中的Provider们:Service和Factory等的区别
    Linux企业运维人员必备150个命令汇总
  • 原文地址:https://www.cnblogs.com/dfsxh/p/6509989.html
Copyright © 2011-2022 走看看