In Linux, the Redis service is installed.
In Windows, the XAMPP environment is installed for PHP to run.
The PHP code is as follows:
<?php
$redis = new Redis();
$redis->connect('192.168.1.4', 6379);
$redis->set('tag', 'hello');
echo 'name:', $redis->get('tag');
?>
When executing the above code, the error is as follows:
Fatal error: Uncaught exception ‘RedisException’ with message ‘Redis server went away’ in xxxx RedisException: Redis server went away in xxxxxx
Error analysis and solutions
The reason for the error is simple: it fails to connect to the Redis service. Due to Redis's security policy, only local access is allowed by default. You need a simple configuration to allow external network access.
Modify the Redis configuration file and comment out all bind information.
# bind 192.168.1.100 10.0.0.1 # bind 192.168.1.8 # bind 127.0.0.1
After the modification, you need to restart the Redis service.
Modify the Linux firewall (iptables) to open your Redis service port, which is 6379 by default.
-A INPUT -m state –state NEW -m tcp -p tcp –dport 6379 -j ACCEPT …… -A INPUT -j REJECT –reject-with icmp-host-prohibited
Please note that you must place the Redis firewall configuration before REJECT. Then execute.service iptables restart。
At this point, accessing the code above will connect to the Redis service and display correctly.
About bind
Looking at articles online, this is often translated as "Specify that Redis only accepts requests from this IP address; if not set, it will handle all requests. In production environments, it is best to set this item." This explanation will totally confuse beginners, and is even wrong. The English original text here is:
# If you want you can bind a single interface, if the bind option is not # specified all the interfaces will listen for incoming connections. # bind 127.0.0.1
Here it explains that bind is for the interface, that is, the network interface. A server can have one network interface (commonly called a network card) or multiple. For example, if the machine has two network cards with addresses 192.168.205.5 and 192.168.205.6, and if bind 192.168.205.5, then only that network card address accepts external requests; if not bound, both network card ports accept requests.
OK, not sure if I made it clear, let me give another example. In my experiment above, I commented out the bind item. Actually I had another solution. Since my Redis server address is 192.168.1.4, if I don't comment out the bind item, what else can I do? I can configure as follows:
# bind 192.168.1.4
Many people mistakenly think that the bound IP should be the IP of the request source. Actually it is not. What should be bound here is the IP on which your Redis server itself accepts requests.
Original address: https://blog.csdn.net/hel12he/article/details/46911159