Redis远程服务器配置疑问:C#连接失败,bind地址该填什么?
Hey there, I feel your pain—being stuck on a connection issue for 5 days is no fun. Let's break down exactly what's going wrong here, starting with your question about the bind config, then fix that socket error.
First: Clarifying the Redis bind Setting
The bind parameter in Redis tells the service which IP addresses on the cloud server it should listen for connections on. That means:
- You need to enter your cloud server's own IP (either its private internal IP, public external IP, or both)
- Or use
0.0.0.0to allow connections from any IP (great for testing, but avoid this in production unless you lock things down with passwords and security groups) - Do NOT enter your development machine's IP here—this setting has nothing to do with where connections are coming from, only where Redis listens on the server itself.
Step-by-Step Fix for the "Socket Failure on PING" Error
This error almost always means a network block or misconfigured Redis setup. Follow these steps:
Update Redis's Bind Configuration
- SSH into your cloud server and open the Redis config file (usually located at
/etc/redis/redis.conf) - Find the line starting with
bind(it might be commented out with a#). Change it to:
Or if you want to be more restrictive, use your server's public IP:bind 0.0.0.0bind 123.45.67.89 - Restart Redis to apply changes:
sudo systemctl restart redis
- SSH into your cloud server and open the Redis config file (usually located at
Check Cloud Server Security Groups/Firewall
- Make sure your cloud provider's security group (or server firewall) allows incoming traffic on Redis's default port
6379(or whatever port you configured) from your development machine's IP address. This is a super common oversight!
- Make sure your cloud provider's security group (or server firewall) allows incoming traffic on Redis's default port
Verify Redis Password (If Enabled)
- If you set a
requirepassin your Redis config, your C# code must include this password to connect.
- If you set a
Fix Your C# Connection Code
Using StackExchange.Redis (the most popular Redis client for C#), here's a corrected example that should work:private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() => { var configOptions = new ConfigurationOptions { // Replace with your cloud server's public IP and Redis port EndPoints = { "123.45.67.89:6379" }, // Uncomment and set if you have a Redis password // Password = "your-strong-redis-password", ConnectTimeout = 5000, SyncTimeout = 5000 }; return ConnectionMultiplexer.Connect(configOptions); }); public static ConnectionMultiplexer RedisConnection => lazyConnection.Value;
Quick Troubleshooting Tip
If you're still stuck, test the connection from your development machine using telnet or redis-cli first:
telnet your-server-ip 6379 # Or with redis-cli redis-cli -h your-server-ip -p 6379
If this fails, the problem is definitely network-related (security groups, bind config) rather than your C# code.
内容的提问来源于stack exchange,提问作者Ajaya

