Cannot connect to my SQL Server database with NestJS - sql-server

After reading the documentation, I've encountered a problem, I'm getting the error unable to connect to the database. I'm trying to connect to a local SQL Server database, using NestJS. I enabled a user sa and set its password. I also enabled TCP/IP connection and confirmed the port from SQL Server. I'm wondering what am I missing or doing wrong?
#Module({
imports: [
ConfigModule.forRoot({
envFilePath:'.env',
isGlobal: true
}),
TypeOrmModule.forRoot({
type: 'mssql',
host: 'localhost',
port: 1433,
username: 'root',
password: 'root',
database: 'test',
autoLoadEntities: true,
synchronize: true,
}),
],
controllers: [AppController, EstadoProyController],
providers: [AppService],
})
export class AppModule {}

check this items:
1- sql server running
2- check your port or host

Related

Django Backend with two different data sources

I am working on a React web app that will enable the user to store new data rows and edit/udpate old data rows. The reference data, for the React form, is stored in ADLS Gen1 parquet format and the app will write data to a different ADLS location in parquet format.
The question is: How can I setup the Django backend with these two different data source? I want to read some reference data from ADLS Gen1 and write and edit into another ADLS in parquet format.
You can use different databases in django settings like this below :
DATABASES = {
'DB1': {
'ENGINE': 'mssql',
'NAME': 'DB_NAME',
'USER': 'sa',
'PASSWORD': 'PASSWORD',
'HOST': 'HOST',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 11 for SQL Server',
},
},
'DB2': {
'ENGINE': 'mssql',
'NAME': 'DB_NAME',
'USER': 'sa',
'PASSWORD': 'PASSWORD',
'HOST': 'HOST',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 11 for SQL Server',
},
},
}
Then on your ORM you can use tbl.objects.using('DB1').all()

django-mssql-backend - Invalid connection string attribute (0)

OK, I'm slowly going crazy. I want to connect Django 3 to MSSQLv15 Server. Connection works if I use Trustedconnection=yes (using win credentials), it also works on Ubuntu if I'm using FreeTDS v7.4 but it won't work in Windows if I manually insert service account or personal credentials and use sql login. Is it some kind of dependency issue? I have tried various library version combos, but to no avail.
Error message is:
conn = Database.connect(connstr,
django.db.utils.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'DOMAIN\\user'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0)")
Requiremetns.txt
asgiref 3.3.4
Django 3.2
django-mssql-backend 2.8.1
djangorestframework 3.12.4
pip 20.2.3
pyodbc 4.0.30
pytz 2021.1
setuptools 49.2.1
sqlparse 0.4.1
Not working (Win)
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name',
'HOST': 'hostname',
'USER': 'DOMAIN\\user',
'PASSWORD': 'pass',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
}
}
}
Working (Win)
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name',
'HOST': 'hostname',
'USER': '',
'PASSWORD': '',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
'Trusted_Connection': 'yes',
}
}
}
Working (Ubuntu)
django==2.1.0
django-pyodbc-azure-2019
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name',
'HOST': 'hostname',
'USER': 'DOMAIN\\user',
'PASSWORD': 'pass',
'PORT': '1433',
'OPTIONS': {
"driver": "FreeTDS",
"host_is_server": True,
"extra_params": "tds_version=7.4",
}
}
}
ODBC Driver 17 for SQL Server doesn't seem to support authenticating domain users via username and password, where FreeTDS does (see https://stackoverflow.com/a/37702329/9461432). On a non-Windows machine, Microsoft directs you to use Kerberos.
Authenticating via username & password works fine if you're connecting to a user that uses SQL Server authentication (ie. non-domain).

How remove port number ":80" when serving localhost using grunt-contrib-connect

When I serve my project using grunt-contrib-connect, I need it to run using https, and in the browser the root path needs to be:
https://localhost/
I am not familiar with how to set this up using the proxy option in grunt-contrib-connect.
My grunt.initConfig setup is:
connect: {
server: {
options: {
port: 80,
base: 'dist',
protocol: 'https',
livereload: true,
keepalive: true,
debug: true,
hostname: 'localhost',
open: true
}
}
},
```
This serves the project to https://localhost:80/
I will finish setting up livereload once this is working correctly.
Thanks for your help.
For https without a port number in the address bar, port needs to be set to 443, and for livereload you must have simple, self-generated certificates created - they can reside in the project folder as they are in no way connected with security.
The connect/watch setup in my gruntfile is:
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-watch');
// Project configuration.
grunt.initConfig({
//live server
connect: {
server: {
options: {
port: 443,
base: 'dist', //the folder my compiled web files are generated in
protocol: 'https',
livereload: true,
debug: true, //show me what is being served, helpful for debugging
hostname: 'localhost',
open: true //auto-open browser tab - remove if annoying
}
}
},
//watch for changes and recompile / reload
watch: {
dev: {
options: {
livereload: {
port: 35729,
key: grunt.file.read('livereload.key'),
cert: grunt.file.read('livereload.crt')
// you can pass in any other options you'd like to the https server, as listed here: http://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
}
},
files: [ // Files to livereload on
"js/**/*.*",
"less/admin/*.less",
"./*.html",
"./Gruntfile.js",
"snippets/*.*"
],
tasks: [
'dev' //the task to run after a change is detected
]
}
},
.....
This guide helped me create the certificates:
https://www.gilluminate.com/2014/06/10/livereload-ssl-https-grunt-watch/
The :9000 detail did not work for me, I left the livereload port at 35729, and once the server was up and running hit this in a new tab to authorise access to the script using self-generated certificates:
https://localhost:35729/livereload.js?snipver=1
I hope this helps someone ;)

Can't connect to MSSQL using django-pyodbc-azure 2.1.0.0

As the title says. Im getting this error
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Heres my code
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'dbname',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'HOST',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
}
}
DATABASE_CONNECTION_POOLING = False
Check first if you have that driver installed:
import pyodbc
print(pyodbc.drivers())
Download ODBC Driver for SQL Server
Installing the Microsoft ODBC Driver for SQL Server on Linux and macOS

Cant login to mssql via typeorm

Error from cmd
I used the same id and password to connect to the same database using MS SQL Server management studio, but when I connect in typeorm, the error pops out as shown in the attachment.
the following is my script to connect the database
import "reflect-metadata";
import { createConnection } from "typeorm";
createConnection({
type: "mssql",
host: "localhost",
port: 3000,
username: "root",
password: "12345",
database: "ABC",
entities: [
],
extra: {
options: {
encrypt: false
},
},
synchronize: true,
logging: false
}).then(connection => {
// here you can start to work with your entities
}).catch(error => console.log(error));
anyone have any idea on how to solve this?
looks like the port number provided is wrong. Please correct your port number. The default port number for MSSQL is 1433

Resources