簡體   English   中英

SQL Server未找到服務器或無法訪問服務器

[英]SQL Server The server was not found or was not accessible

我正在嘗試為項目創建一個新的遷移,但我一直在:

建立與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內部調用connect:

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>());
}

此錯誤通常通過以下方式解決:

  1. 確保SQL Server服務正在運行
  2. 確保將sql server配置為允許遠程連接。
  3. 如果是命名實例,請確保SQL Server瀏覽器服務正在運行
  4. 檢查SQL Server錯誤日志,查看確認SQL正在偵聽預期網絡接口和端口的消息(例如:1433)
  5. 檢查以查看web.config或端口是否正確時編碼連接的位置。
  6. 檢查防火牆設置 - 在防火牆中為端口添加例外

這篇文章非常有用,並提供了詳細信息:

為什么我會收到“無法連接到服務器 - 與網絡相關或特定於實例的錯誤”?

關於數據庫管理員的 這個問題有一些很好的建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM