【问题标题】:SQL Server The server was not found or was not accessibleSQL Server 服务器未找到或无法访问
【发布时间】:2018-09-11 00:31:47
【问题描述】:

我正在尝试为项目创建新的迁移,但我不断收到:

与 SQL Server 建立连接时出现与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定实例)

我在 appsettings.json 中有连接字符串:

  "ConnectionStrings": {
    "DefaultConnection": "Server=xx.xx.xxx.206;Database=AngularASPNETCore2WebApiAuth; User ID = me; Password= not_pwd;Trusted_Connection=False;MultipleActiveResultSets=true"
  },

我试过了:

  • 确认连接字符串正确(它在另一个数据库名称不同的项目中工作)

  • 确认我可以使用 conn 字符串中提供的凭据登录 SQL

  • 在初始迁移中从Up 方法中删除所有内容,然后重新运行update-database 命令。 (结果相同)

我还能尝试解决什么问题?

在 Startup.cs 中调用连接:

public void ConfigureServices(IServiceCollection services)
{
  // Add framework services.
  services.AddDbContext<ApplicationDbContext>(options =>
      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
          b => b.MigrationsAssembly("AngularASPNETCore2WebApiAuth")
          ));

  services.AddSingleton<IJwtFactory, JwtFactory>();

  // Register the ConfigurationBuilder instance of FacebookAuthSettings
  services.Configure<FacebookAuthSettings>(Configuration.GetSection(nameof(FacebookAuthSettings)));

  services.TryAddTransient<IHttpContextAccessor, HttpContextAccessor>();

  // jwt wire up
  // Get options from app settings
  var jwtAppSettingOptions = Configuration.GetSection(nameof(JwtIssuerOptions));

  // Configure JwtIssuerOptions
  services.Configure<JwtIssuerOptions>(options =>
  {
    options.Issuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
    options.Audience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)];
    options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256);
  });

  var tokenValidationParameters = new TokenValidationParameters
  {
    ValidateIssuer = true,
    ValidIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)],

    ValidateAudience = true,
    ValidAudience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)],

    ValidateIssuerSigningKey = true,
    IssuerSigningKey = _signingKey,

    RequireExpirationTime = false,
    ValidateLifetime = true,
    ClockSkew = TimeSpan.Zero
  };

  services.AddAuthentication(options =>
  {
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

  }).AddJwtBearer(configureOptions =>
  {
    configureOptions.ClaimsIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
    configureOptions.TokenValidationParameters = tokenValidationParameters;
    configureOptions.SaveToken = true;
  });

  // api user claim policy
  services.AddAuthorization(options =>
  {
    options.AddPolicy("ApiUser", policy => policy.RequireClaim(Constants.Strings.JwtClaimIdentifiers.Rol, Constants.Strings.JwtClaims.ApiAccess));
    });

  // add identity
  var builder = services.AddIdentityCore<AppUser>(o =>
  {
          // configure identity options
          o.Password.RequireDigit = false;
    o.Password.RequireLowercase = false;
    o.Password.RequireUppercase = false;
    o.Password.RequireNonAlphanumeric = false;
    o.Password.RequiredLength = 6;
  });
  builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole), builder.Services);
  builder.AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();

  services.AddAutoMapper();
  services.AddMvc().AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
}

【问题讨论】:

  • 你能在你使用appsettings连接字符串的地方显示datacontext构造函数的代码吗?那里可能缺少某些东西。
  • blogs.msdn.microsoft.com/sql_protocols/2008/04/30/… BTW 类似的问题被问了多次...
  • 仅供参考不要将术语“数据库”与 sql server 实例(简称实例)混淆。您连接到一个实例,然后访问该实例中的数据库。您关于“在另一个项目中工作”的声明没有添加任何有用的信息。您是连接到默认实例还是命名实例?成功连接后(根据您的声明),您在 SSMS 连接对话框中看到/使用了哪些值?您在几周前发布过同样的问题,但从未回复。

标签: c# asp.net sql-server


【解决方案1】:

此错误通常通过以下方式解决:

  1. 确保 SQL Server 服务正在运行
  2. 确保将 sql server 配置为允许远程连接。
  3. 如果是命名实例,请确保 SQL Server 浏览器服务正在运行
  4. 检查 SQL Server 错误日志中的消息,确认 SQL 正在侦听预期的网络接口和端口(例如:1433)
  5. 检查 web.config 中的内容,或者如果端口正确,则检查连接的编码位置。
  6. 检查防火墙设置 - 在防火墙中为端口添加例外

这篇文章很有用,提供了详细的信息:

Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?

Database Administrators 上的This question 有一些很好的建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    相关资源
    最近更新 更多