Connect to LocalDB\MSSQLLocalDB with Objection.js Knexfile - node.js

I am trying to connect to a LocalDB\MSSQLLocalDB SQLExpress instance in my Node.js application that uses Objection.js/Knex for the data layer.
When I try to run a migration it fails to connect to the database. But I can connect via SSMS without issue.
Here is my Knexfile.js connection settings
module.exports = {
development: {
client: 'mssql',
useNullAsDefault: true,
connection: {
server: '(LocalDB)\\MSSQLLocalDB',
user: 'localadmin',
password: 'password$',
database: 'database'
},
migrations: {
directory: './migrations',
tableName: 'knex_migrations'
},
seeds: {
directory: './seeds'
},
...knexSnakeCaseMappers()
}
};
When I run a migration it gives the following error:
Failed to connect to (LocalDB)\MSSQLLocalDB:1433 - getaddrinfo ENOTFOUND (LocalDB)\MSSQLLocalDB
Error: Failed to connect to (LocalDB)\MSSQLLocalDB:1433 - getaddrinfo ENOTFOUND (LocalDB)\MSSQLLocalDB
at Connection.socketError (D:\Code\backend\node_modules\tedious\lib\connection.js:1393:28)
at D:\Code\backend\node_modules\tedious\lib\connection.js:1153:21
at GetAddrInfoReqWrap.callback (D:\Code\backend\node_modules\tedious\lib\connector.js:195:16)
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:77:17)
I believe this is an issue with tcp/ip settings in SqlExpress but I cannot find an easy way to change those.
Is there any settings for the Knexfile that will allow for a SQLExpress database connection?

LocalDB only accepts named pipes connections. Use proper SQL Express and enable TCP connections

Related

NestJS, Typeorm, SQL Server - failed to connect to localhost:1433 - Could not connect (sequence)

I am trying to connect to a locally running a SQL Server database using Typeorm and nestJS.
I have created a new database in SSMS Express, and created a new login, giving the user db_owner permissions for the given database.
I have installed the following packages:
#nestjs/typeorm": "^9.0.0",
"mssql": "^8.1.2",
This is the error that I receive:
[Nest] 25180 - 07/29/2022, 10:50:42 AM ERROR [ExceptionHandler] Failed to connect to localhost:1433 - Could not connect (sequence)
ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
at connectListener (C:\Projects\lucas\node_modules\mssql\lib\tedious\connection-pool.js:70:17)
at Connection.onConnect (C:\Projects\lucas\node_modules\tedious\src\connection.ts:1763:9)
at Object.onceWrapper (events.js:417:26)
at Connection.emit (events.js:310:20)
at Connection.emit (C:\Projects\lucas\node_modules\tedious\src\connection.ts:1906:18)
at Connection.socketError (C:\Projects\lucas\node_modules\tedious\src\connection.ts:2221:12)
at callback (C:\Projects\lucas\node_modules\tedious\src\connection.ts:2006:21)
at SequentialConnectionStrategy.connect (C:\Projects\lucas\node_modules\tedious\src\connector.ts:118:14)
at Socket.onError (C:\Projects\lucas\node_modules\tedious\src\connector.ts:144:12)
at Socket.emit (events.js:310:20)
This is my configuration for the connection to the database:
TypeOrmModule.forRoot({
type: 'mssql',
host: 'localhost',
port: 1433,
username: 'timetracker_user',
password: 'ihaveagun',
database: 'timetracker',
// entities: [User], // define entities manually
autoLoadEntities: true, // auto loads all entities registered through the forFeature() method
synchronize: true, // should be set to false in production.
}),
The largest issue that I am having is that I cannot find the resources to help me. When googling, I found articles on enabling TCP/IP for SQL Server, however when researching on how to do this, all resources point to MS Server tools. I am doing this on Windows 10 machine and I am using SQL Server Management Studio 18.
At this stage, I simply want to connect to my db locally, this is for learning purposes.
Could I please get some help on how to debug and fix this issue?
Many thanks in advance!

Error: No event 'socketConnect' in state 'SentPrelogin'

I am trying to connect my SQL Server database with node.js using knex but I am facing issue
Error: No event 'socketConnect' in state 'SentPrelogin'
at Connection.dispatchEvent (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1281:26)
at Connection.socketConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1303:10)
at C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1145:12
at Socket.onConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connector.js:106:7)
at Socket.emit (events.js:314:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1131:10)
Emitted 'error' event on Connection instance at:
at Connection.dispatchEvent (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1281:12)
at Connection.socketConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1303:10)
[... lines matching original stack trace ...]
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1131:10)
[nodemon] app crashed - waiting for file changes before starting...
My code is
var knex = require('knex')({
client: 'mssql',
version:"7_1",
connection: {
user: 'sa',
password: 'Admin#123',
server: 'localhost',
database: 'Demo'
}
});
knex.select("*").from("Country")
.then(function (depts){
depts.forEach((dept)=>{ //use of Arrow Function
console.log({...dept});
});
}).catch(function(err) {
// All the error can be checked in this piece of code
console.log(err);
}).finally(function() {
// To close the connection pool
knex.destroy();
});
You need to add a missing dependency:
npm install --save tedious
As of knex v0.95.0 you'll need to use the tedious library instead of mssql when connecting to an MSSQL database. According to the knex upgrade instructions:
MSSQL driver was completely reworked in order to address the multitude of connection pool, error handling and performance issues. Since the new implementation uses tedious library directly instead of mssql, please replace mssql with tedious in your dependencies if you are using a MSSQL database.
Installing the package above should resolve your issue. I also had to set the encrypt option to false when connecting to my local database to avoid this error:
ConnectionError: Failed to connect to localhost:1433 - self signed certificate

Unable to connect to CloudSql from my Kuberernetes Cluster through cloudsqlproxy sidecar

while trying to connect to my cloudsql instance I am getting this error
{
errorno: "ETIMEDOUT",
code: "ETIMEDOUT",
syscall: "connect",
fatal: true
}
this is the log from my cloudsqlproxy container
2020/06/09 15:53:04 current FDs rlimit set to 1048576, wanted limit is 8500. Nothing to do here.
2020/06/09 15:53:04 using credential file for authentication; email=lion-db#estatelion-test-275318.iam.gserviceaccount.com
2020/06/09 15:53:04 Listening on 127.0.0.1:3306 for estatelion-test-275318:us-central1:estatelion
2020/06/09 15:53:04 Ready for new connections
but my nodejs application is unable to connect to it
const connection = mysql.createPool({
connectionLimit: 10,
host: config.get("database").host, // localhost
user: config.get("database").user, // cloud proxy user for my cloudsql instance
password: config.get("database").password, // password
database: config.get("database").db_name, // database
});
Well I found the solution to this
I just updated the Cluster version to 1.15.11-gke.15 from 1.14.* (I dont remember the rest ) and it worked fine.
I think there is some bug in the default GKE version of the deployment.
Thanks Everyone for your gracious time and effort. :-)

Error: Failed to connect to MongoDB. Are you sure your configured Mongo instance is running? When lifting sails

Hi guys im receiving an error
Error: Failed to connect to MongoDB. Are you sure your configured Mongo instance is running? Error details: { [MongoError: failed to connect to server on first connect] name: 'MongoError', message: 'failed to connect to server on first connect' }
I'm using a previous version of Sails since that its the version that they are currently using
Sails ~0.12.4
nodejs -v 4.2.6
npm -v 3.5.2
error: A hook (`orm`) failed to load!
error: Error: Failed to connect to MongoDB. Are you sure your configured Mongo instance is running?
Error details:{ [MongoError: failed to connect to server [192.168.2.45:27017] on first connect] name: 'MongoError',
failed to connect to server [192.168.2.45:27017] on first connect]
name: 'MongoError', message: 'failed to connect to server [192.168.2.45:27017] on first connect' }
My config/connection.js looks like this
module.exports.connections = {
localDiskDb: {
adapter: 'sails-disk'
},
mongodbServer: {
adapter: 'sails-mongo',
host: '192.168.2.45',
port: 27017,
user : 'dbAdmin',
password : 'hiveslab',
database: 'sauce_db'
},
};
I'm stuck on this for a day and I'm pretty much of a newbie here. Hopefully you guys can lead me to the right resolution. Thank you in advance have a good day.

Ghost Blog - Unable to connect with mysql

After deploying the Ghost blogging platform with success, i tried to configure it to use Mysql instead of sqllite3 using this database section of their config page, which says:
Database
By default, Ghost comes configured to use an SQLite database, which
requires no configuration.
Alternatively Ghost can also be used with a MySQL database by changing
the database configuration. You must create a database and user first,
you can then change the existing sqlite config to something like:
database: { client: 'mysql', connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'ghost_db',
charset : 'utf8' }
}
So ok, the setup is straight forward. but i'm still unable to connect ghost with mysql. The error i receive after starting the platform using npm start --production is :
> ghost#0.6.2 start /var/www/ghost
> node index
Migrations: Database initialisation required for version 003
Migrations: Creating tables...
Migrations: Creating table: posts
ERROR: connect ECONNREFUSED
Error: connect ECONNREFUSED
at errnoException (net.js:905:11)
at Object.afterConnect [as oncomplete] (net.js:896:19)
--------------------
at Protocol._enqueue (/var/www/ghost/node_modules/mysql/lib/protocol/Protocol.js:110:48)
at Protocol.handshake (/var/www/ghost/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/var/www/ghost/node_modules/mysql/lib/Connection.js:98:18)
at /var/www/ghost/node_modules/knex/lib/dialects/mysql/index.js:105:16
at tryCatch2 (/var/www/ghost/node_modules/bluebird/js/main/util.js:53:21)
at Promise._resolveFromResolver (/var/www/ghost/node_modules/bluebird/js/main/promise.js:544:13)
at new Promise (/var/www/ghost/node_modules/bluebird/js/main/promise.js:84:37)
at Client_MySQL.acquireRawConnection (/var/www/ghost/node_modules/knex/lib/dialects/mysql/index.js:104:10)
at Object.create (/var/www/ghost/node_modules/knex/lib/pool.js:33:19)
at Object.Pool.createResource (/var/www/ghost/node_modules/knex/node_modules/generic-pool-redux/pool.js:288:12)
I'm not sure what could be wrong, since i have other applications using mysql working without any problems.
Thanks in advance.
connection refused = tcp connection was attempted, but nothing is listening on the port, or was explicitly denied. Unless you explicitly enabled TCP support in mysql (and have the correct ip/port), you should probably be using a local unix-domain socket instead.

Resources