繁体   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