尝试将 ASPNET Core WebAPI 连接到 Angular 项目的新 Ubuntu Server 22.04 数据库

问题描述 投票:0回答:0

我知道这个问题在这些板上已经被问过几次了,所以我很抱歉再发布一个。另外,这是我第一次发帖,所以我很紧张,但最重要的是,我很困惑。我有一个 Angular 16 前端网站,它使用 ASPNET.Core 7 后端,**用于**本地连接到 MSSQL 数据库。我最近购买了一台服务器,正在尝试让它运行。

服务器是Ubuntu:22.04 数据库:mysql 8 EntityFrameworkCore 版本 6 ASP.NET Core 7

本地一切都运行良好。

  • 我还在我的 Program.cs 中使用 Cors() 吗?

Program.cs文件:

`` var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ??抛出新的 InvalidOperationException(); builder.Services.AddDbContext(options => options.UseSqlServer(connectionString));

        // Add services to the container.

        builder.Services.AddControllers();
        // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen();

        //ANGULAR
        builder.Services.AddCors();
        var configuration = builder.Configuration;

        var app = builder.Build();

        //ANGULAR
        app.UseCors(options => options.WithOrigins("https://localhost:4200").AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());

``

根据教程,我注释掉了 bind-address

$ grep 'bind-address' vim /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf:#bind-address   = 127.0.0.1 /etc/mysql/mysql.conf.d/mysqld.cnf:#bind-address = 0.0.0.0```

    $ sudo systemctl status mysql
    ●  mysql.service - MySQL Community Server
         Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
         Active: **active** (running) since Sun 2023-07-30 03:02:40 UTC; 2min 2s ago

我使用的是csf,而不是ufw:

    $ grep 3306 /etc/csf/csf.conf
    TCP_IN = "3306"
    TCP_OUT = "3306"
    UDP_IN = "3306"
    UDP_OUT = "3306"

appsettings.json:

    $ grep -i 'defaultconnection' appsettings.json
    "DefaultConnection": "Server=localhost;User Id=<mysql user>;Password=<password>;Database=   <datebasename"

dotnet ef 迁移添加 InitialCreate 工作正常。 当我开始更新数据库时:

    $ dotnet ef database update
    Build started...
    Build succeeded.
    Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error      occurred while establishing a connection to 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. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server:     Could not open a connection to SQL Server)
       at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean     breakConnection, Action`1 wrapCloseInAction)
       at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,     Boolean callerHasConnectionLock, Boolean asyncClose)
       at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds      connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnectionString connectionOptions,    Boolean withFailover)
       at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,    String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer    timeout, Boolean withFailover)
       at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo,   String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance,   SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
       at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout,    SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString   newSecurePassword, Boolean redirectedUserInstance)
       at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String   newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString   userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String   accessToken, DbConnectionPool pool)
       at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions     options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool,    DbConnection owningConnection, DbConnectionOptions userOptions)
       at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey,  DbConnectionOptions userOptions)
       at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection,   DbConnectionOptions userOptions, DbConnectionInternal& connection)
       at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection  owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions,  DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry,    DbConnectionOptions userOptions)
       at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection   outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry,   DbConnectionOptions userOptions)
       at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
       at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext _, TState s)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:00000000-0000-0000-0000-000000000000
A network-related or instance-specific error occurred while establishing a connection to 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. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server: Could not open a connection to SQL Server)

如果您可以提供帮助或向我展示另一个线程,请提前致谢。我已经经历过很多了。

mysql angular entity-framework asp.net-core ubuntu
© www.soinside.com 2019 - 2024. All rights reserved.