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
- [Email protected] ~]# redis-cli-h 192.168.1.103-p 6379
- Redis 192.168.1.103:6379>
Set each string value to 103 on 101 User.1.name=zhangsan
[Plain]View Plaincopy
- Redis 192.168.1.103:6379> Set User.1.name Zhangsan
- 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
- [Email protected] utils]# REDIS-CLI
- Redis 127.0.0.1:6379> Get User.1.name
- "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
- [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
- Requirepass "Hi, coder"
After saving the configuration file, restart the Redis service
[Plain]View Plaincopy
- [[email protected] utils]#/etc/init.d/redis_6379 stop
- Stopping ...
- Waiting for Redis to shutdown ...
- Redis stopped
- [[email protected] utils]#/etc/init.d/redis_6379 start
- Starting Redis Server ...
101 Reconnect 103 and get the value of User.1.name
[Plain]View Plaincopy
- [Email protected] ~]# redis-cli-h 192.168.1.103-p 6379
- Redis 192.168.1.103:6379> Get User.1.name
- (Error) ERR Operation not permitted
- 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
- Redis 192.168.1.103:6379> quit
- [Email protected] ~]# redis-cli-h 192.168.1.103-p 6379-a "Hi, coder"
- Redis 192.168.1.103:6379> Get User.1.name
- "Zhangsan"
Redis Series-Remotely connect to Redis and lock Redis