简体   繁体   中英

TypeORM connection issue with SQL Server

I have SQL Server instance running on my local machine, when I am trying to connect a database from TypeORM it is throwing below error:

originalError: ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)

Here is my ormconfig.ts file looks like:

{
   "type": "mssql",
   "host": "localhost",
   "username": "",
   "password": "",
   "database": "demoDB",
   "synchronize": true,
   "logging": true,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }
}

and here is my package.json file:

{
   "name": "PLHApi-Dev",
   "version": "0.0.1",
   "description": "Performance Lighthouse Reborn.",
   "devDependencies": {
      "@types/bcryptjs": "^2.4.2",
      "@types/cookie-parser": "^1.4.2",
      "@types/express": "^4.17.9",
      "@types/graphql": "^14.5.0",
      "@types/jsonwebtoken": "^8.5.0",
      "@types/node": "^8.0.29",
      "nodemon": "^2.0.6",
      "ts-node": "3.3.0",
      "typescript": "3.3.3333"
   },
   "dependencies": {
      "apollo-server-express": "^2.19.1",
      "bcryptjs": "^2.4.3",
      "body-parser": "^1.18.1",
      "cookie-parser": "^1.4.5",
      "dotenv": "^8.2.0",
      "express": "^4.15.4",
      "jsonwebtoken": "^8.5.1",
      "mssql": "^4.3.9",
      "reflect-metadata": "^0.1.10",
      "type-graphql": "^1.1.1",
      "typeorm": "0.2.29"
   },
   "scripts": {
      "dev": "nodemon --exec ts-node src/index.ts"
   }
}

when I am trying to run it is throwing an ESOCKET error,

originalError: ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
      at ConnectionError (E:\PLH_API\PLHApi-Dev\node_modules\tedious\lib\errors.js:12:12)
      at Connection.socketError (E:\PLH_API\PLHApi-Dev\node_modules\tedious\lib\connection.js:1024:30)
      at E:\PLH_API\PLHApi-Dev\node_modules\tedious\lib\connection.js:868:25
      at SequentialConnectionStrategy.connect (E:\PLH_API\PLHApi-Dev\node_modules\tedious\lib\connector.js:154:9)
      at Socket.onError (E:\PLH_API\PLHApi-Dev\node_modules\tedious\lib\connector.js:170:16)
      at Socket.emit (events.js:315:20)
      at Socket.EventEmitter.emit (domain.js:482:12)
      at emitErrorNT (internal/streams/destroy.js:92:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
      at processTicksAndRejections (internal/process/task_queues.js:84:21) {
    code: 'ESOCKET'

I am also attaching the SSMS configuration window screenshot,

SSMS 会议

Please help me to resolve the issue.

Thanks in advance.

Verifique o nome da instancia do seu SQL Server, Exemplo: LOCALHOST\MSSQLSERVER.

Veja na documentação oficial do TypeORM

https://typeorm.io/#/connection-options/mssql-connection-options

options.instanceName - The instance name to connect to. The SQL Server Browser service must be running on the database server, and UDP port 1434 on the database server must be reachable. Mutually exclusive with port. (no default).

No seu arquivo ormconfig.json você coloca:

"host": "LOCALHOST",
"options": { 
    "instanceName": "SQLSERVER"
},

you should type port:'', below the host with the port number in the '' and the username of the SQL server you want to connect to in the '' username:'', and the password of the SQL server user login in the password:'',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM