简体   繁体   English

SQL Server未找到服务器或无法访问服务器

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

I am trying to create a new migration for a project, but I keep getting: 我正在尝试为项目创建一个新的迁移,但我一直在:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供程序:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错)

I have the connection string in appsettings.json: 我在appsettings.json中有连接字符串:

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

I tried: 我试过了:

  • confirmed the connection string is correct (it works in another project with a different database name) 确认连接字符串是正确的(它在另一个具有不同数据库名称的项目中工作)

  • confirmed that I can log into SQL with the credentials provided in the conn string 确认我可以使用conn字符串中提供的凭据登录SQL

  • deleting everything from the Up method in the initial migration and re-running the update-database command. 在初始迁移中删除Up方法中的所有内容并重新运行update-database命令。 (same result) (同样的结果)

What else can I try to troubleshoot this? 我还可以尝试解决此问题?

Inside Startup.cs the call to connect: 在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>());
}

This error is usually resolved by the following: 此错误通常通过以下方式解决:

  1. Ensure SQL Server Service is running 确保SQL Server服务正在运行
  2. Ensure that sql server is configured to permit remote connections. 确保将sql server配置为允许远程连接。
  3. If a named instance, make sure SQL Server browser service is running 如果是命名实例,请确保SQL Server浏览器服务正在运行
  4. Examine the SQL Server error log for messages confirming that SQL is listening on the expected network interfaces and ports (for example: 1433) 检查SQL Server错误日志,查看确认SQL正在侦听预期网络接口和端口的消息(例如:1433)
  5. Check to see in web.config or where the connection is coded if port is correct. 检查以查看web.config或端口是否正确时编码连接的位置。
  6. Check firewall settings - add an exception in firewall for port 检查防火墙设置 - 在防火墙中为端口添加例外

This post is quite useful and provides detailed information: 这篇文章非常有用,并提供了详细信息:

Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"? 为什么我会收到“无法连接到服务器 - 与网络相关或特定于实例的错误”?

This question on Database Administrators has some good advice. 关于数据库管理员的 这个问题有一些很好的建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务器未找到或无法访问 - server was not found or was not accessible 错误-尝试连接到SQL Server数据库时出现“找不到或无法访问服务器” - Error - “The server was not found or was not accessible” when trying to connect to sql server database NET 6 - 实体框架核心错误:SQL 服务器未找到或无法访问 - NET 6 - Entity Framework Core Error: SQL Server was not found or was not accessible MVC控制器找不到或无法访问服务器 - MVC Controller The server was not found or not accessible 服务器未找到或无法访问。 验证实例名称正确并且已配置SQL Server - The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured 找不到服务器或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接 - The server was not found or was not accessible.Verify that the instance name is correct and that SQL Server is configured to allow remote connections WPF / EF / SQLite找不到服务器或无法访问服务器 - WPF/EF/SQLite The server was not found or was not accessible 如何捕获和处理“找不到或无法访问服务器” - How to catch and handle “the server was not found or was not accessible” 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 服务器未找到或无法访问。 - A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 找不到我的服务器或无法访问我的服务器 - A network-related or instance-specific error occurred while establishing a connection to SQL Server. My server was not found or was not accessible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM