Redis Series-Remotely connect to Redis and lock Redis

Source: Internet
Author: User

Suppose two redis servers, IP: 192.168.1.101 and 192.168.1.103, how do I access Redis on 101 through REDIS-CLI on the 103? before remote connection 103, let's talk about some key parameters of Redis-cli:

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]

-H < host Ip>, default is 127.0.0.1

-p < port;, default is 6379

-A < Password, if Redis is locked, you need to pass the password

--help, displaying Help information

By introducing the RENDIS-CLI usage, connecting 103 on 101 should be simple:

[Plain]View Plaincopy
    1. [Email protected] ~]# redis-cli-h 192.168.1.103-p 6379
    2. Redis 192.168.1.103:6379>

Set each string value to 103 on 101 User.1.name=zhangsan

[Plain]View Plaincopy
    1. Redis 192.168.1.103:6379> Set User.1.name Zhangsan
    2. Ok

See OK to indicate that the setup was successful. Then login directly on the 103 to see if you can get to this value.

[Plain]View Plaincopy
    1. [Email protected] utils]# REDIS-CLI
    2. Redis 127.0.0.1:6379> Get User.1.name
    3. "Zhangsan"

Wood is wrong, it is really zhangsan, this shows that 101 is connected to the 103 Redis server.

Of course can successfully connect 103 is the basic condition, 101 can drink 103 on the 6379 port communication.

It's dangerous for everyone to connect to a Redis server, and we need to set a password for Redis on 103.

How to set it up, you need to edit the Redis configuration file/etc/redis/6379.conf

[Plain]View Plaincopy
    1. [Email protected] utils]# vim/etc/redis/6379.conf

Find # Requirepass foobared Remove the previous comment # and replace the foobared with your own password: Hi, coder

[Plain]View Plaincopy
    1. Requirepass "Hi, coder"

After saving the configuration file, restart the Redis service

[Plain]View Plaincopy
    1. [[email protected] utils]#/etc/init.d/redis_6379 stop
    2. Stopping ...
    3. Waiting for Redis to shutdown ...
    4. Redis stopped
    5. [[email protected] utils]#/etc/init.d/redis_6379 start
    6. Starting Redis Server ...

101 Reconnect 103 and get the value of User.1.name

[Plain]View Plaincopy
    1. [Email protected] ~]# 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>

Why is the error, of course, because the connection 103 did not pass the password, quit reconnecting

[Plain]View Plaincopy
    1. Redis 192.168.1.103:6379> quit
    2. [Email protected] ~]# 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"

Redis Series-Remotely connect to Redis and lock Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.