You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

StackExchange.Redis中能否为主从节点设置不同密码?

StackExchange.Redis主从节点不同密码配置方案

StackExchange.Redis支持为主从节点设置不同密码,但不能直接通过全局的Password属性统一配置,需要为每个端点单独指定对应密码,具体配置方式如下:

1. 使用ConfigurationOptions代码配置

通过SetPasswordForEndpoint方法为每个端点单独绑定密码,示例代码:

var configurationOptions = new ConfigurationOptions
{
    SslProtocols = System.Security.Authentication.SslProtocols.Tls12
};

// 添加主节点并设置对应密码
var masterEndpoint = new DnsEndPoint(cachedserverMaster.Host, cachedserverMaster.Port);
configurationOptions.EndPoints.Add(masterEndpoint);
configurationOptions.SetPasswordForEndpoint(masterEndpoint, cachedserverMaster.Password);

// 添加从节点并设置对应密码
var slaveEndpoint = new DnsEndPoint(cachedserverSlave.Host, cachedserverSlave.Port);
configurationOptions.EndPoints.Add(slaveEndpoint);
configurationOptions.SetPasswordForEndpoint(slaveEndpoint, cachedserverSlave.Password);

// 建立连接
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(configurationOptions);

2. 使用连接字符串配置

直接在每个端点地址后追加password=xxx参数,示例:

string connectionString = $"{cachedserverMaster.Host}:{cachedserverMaster.Port},password={cachedserverMaster.Password}," +
                          $"{cachedserverSlave.Host}:{cachedserverSlave.Port},password={cachedserverSlave.Password}," +
                          "sslprotocols=Tls12";
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(connectionString);

注意事项

  • 每个节点的密码必须配置正确,否则对应节点会连接失败
  • StackExchange.Redis会自动识别主从架构,无需额外指定节点角色
  • 确保所有节点都支持你指定的SSL协议(此处为Tls12)

内容的提问来源于stack exchange,提问作者user1263981

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 00:47:24