Django database connection with Host and Instances with SQL Server - python

I'm using SQL Server as the backend for the database. In the setting.py file, I need to use the host name with instance. Due to this, I got the error given below:
The above exception (('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (10061)')) was the direct cause of the following exception:
# "settings.py"
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'xxxxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'PORT': '1433',
'HOST': 'aaa\bbb',(hostname\instance)
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
},
}
}
How could I resolve this error and connect with my database?

Your post is near two years ago, but I faced the same issue now.
By looking on https://github.com/michiya/django-pyodbc-azure/issues/201
It seems that the problem comes from the way the port is included in the connection string. The issue will be solved when the port is specified in this way:
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'xxxxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'HOST': 'aaa\bbb',(hostname\instance)
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
},
'extra_params': 'PORT=1433'
I tried it, it works fine.

Since your settings.py looks fine,I think its a general issue that can be caused from multiple wrong configurations on the database side, such as:
TCP/IP Connections are disabled in the SQL Server Configuration Management
In TCP/IP properties in the "IP ALL" section port 1433 could not be configured, so it refuses to your requests
One of many SQL windows services suddenly could be stopped
I found this stack overflow post very useful with multiple answers that covers each of the points above.

You can use the latest package mssql-django to connect Django to MSSQL(SQL Server) with SQL Server Authentication. *I use SQL Server 2019 Express.
So, try this code below. *"ENGINE" must be "mssql" and keep it blank for "PORT" because there will be error if setting any port number e.g. "2244", "9877" or even "1433" which is the default port number of MSSQL:
# "settings.py"
DATABASES = {
'default': {
'ENGINE': 'mssql', # Must be "mssql"
'NAME': 'xxxxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'HOST': 'aaa\bbb',(hostname\instance)
'PORT': '', # Keep it empty
# 'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}

Related

How to fix 'Named Pipes Provider: Could not open a connection to SQL Server [53]'?

I can't connect MSsql database to django.
When I trying to do with python, It works well, but when I try to run server on Django I get error.
Pyodbc version : 4.0.27;
Django version : 2.1.13;
Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64).
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': '<djangoBanks>',
'HOST': '<DESKTOP-0FKB14V>',
'USER': '<test>',
'PASSWORD': '<12345>',
'OPTIONS': {
'driver': "ODBC Driver 17 for SQL Server",
'Trusted_Connection' : 'Yes',
}
}
}
When I try to run python manage.py runserver I get this error:
'08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named
Pipes Provider: Could not open a connection to SQL Server [53]. (53)
(SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL
Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17
for SQL Server]Invalid connection string attribute (0); [08001]
[Microsoft][ODBC Driver 17 for SQL Server]A network-related or
instance-specific error has occurred while establishing a connection
to SQL Server. Server is not found or not accessible. Check if
instance name is correct and if SQL Server is configured to allow
remote connections. For more information see SQL Server Books Online.
(53)'
I edited 'Options' and then it started work.
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'djangoBanks',
'HOST': 'DESKTOP-0FKB14V',
'USER': 'test',
'PASSWORD': '12345',
'OPTIONS': {
'driver': "ODBC Driver 17 for SQL Server",
'SERVER' : 'DESKTOP-0FKB14V',
'DATABASE' : 'djangoBanks',
'UID' : 'test',
'PWD' : '12345',
'Trusted_Connection' : 'Yes',
}
}
}

OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)") [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am using google app engine
My settings file
import pymysql # noqa: 402
pymysql.install_as_MySQLdb()
if os.getenv('GAE_APPLICATION', None):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/connectionstring',
'USER': 'myusername',
'PASSWORD': 'mypasswor',
'NAME': 'mydbname',
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'username',
'PASSWORD': 'mypassword',
'NAME': 'dbname',
}
}
Is is working fine..
Now I try to implement google authentication so used social-auth-app-django
after redirecting from google it shows the following error:
OperationalError: (2003, "Can't connect to MySQL server on 'localhost'
([Errno 111] Connection refused)") connect
(/env/lib/python3.7/site-packages/pymysql/connections.py:629)
Edit the following configuration file
/etc/my.cnf
add this line: bind-address=127.0.0.1
it should fix the issue

PyCharm: [08004] The server requested password-based authentication, but no password was provided

Can't figure out why is PyCharm keeping raises this error:
I have set everything including Django server configuration and everything works - DB connection, remote host connection, django server etc.
Don't know if it's database problem. Connection works properly. The DB is remote on DigitalOcean droplet.
in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'our_db',
'USER': 'django',
'HOST': '<ip>',
'PASSWORD': '<pwd>',
'PORT': 5432,
'ATOMIC_REQUESTS': True,
}
}
I have connection to remote Ubuntu 16.04 where I use ssh key to authenticate.
Do you know where is the problem?

Cannot establish connection to sql-server using pyodbc on Windows 10

Getting this error when trying to run development server on django.
django.db.utils.Error: ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect)')
Here is my settings.py databases
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'mydata',
'USER': 'user#mydata',
'PASSWORD': '**password**',
'HOST': 'mydata.database.windows.net',
'PORT':'**port**',
'OPTIONS': {
'driver': 'SQL Server',
'host_is_server': True,
'MultipleActiveResultSets': False,
'Encrypt': True,
'TrustServerCertificate': False,
'Connection Timeout': 30,
'Persist Security Info': False,
},
},
}
I have django-pyodbc and django-pyodbc-azure installed.
Using django version 1.11.
Any help would be awesome! Thank you.
EDIT 1
I changed driver to SQL Sever and updated the driver. Now I am getting this error.
django.db.utils.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect)')
I have the database set up on Azure.
According to your error information, there are two possible reason which caused the issue, as below.
The driver option value should be ODBC Driver 13 for SQL Server as the figure below from Azure portal. Please change it and try again.
The access denied error may be caused by not adding your client IP to the firewall of Azure SQL Database to allow access, as the figure below.
Hope it helps.

Can't connect to MySQL server(Connection refused) , trying to install connection between Django and MySQL

I am trying to install connection between MySQL and Django, as far as the old method is only applicable to 2.x python, I use Python 3.4 and mysql.connector.django module https://dev.mysql.com/doc/connector-python/en/connector-python-django-backend.html
Also following example from here http://aarvik.dk/how-to-get-started-with-django-on-top-of-python-3-4-mysql-mariadb-5-5/( MySQL Engine section)
I am getting error after commands below
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL
server on '127.0.0.1:8000' (61 Connection refused)
$ python manage.py migrate
$ python manage.py runserver 8000
Settings.py:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'closerdb',
'HOSTNAME': '127.0.0.1',
'PORT':'8000',
'USER': 'root',
'PASSWORD': 'closerdbpassword',
'OPTIONS': {
'autocommit': True,
},
}
}
Having changed the port, I get the same error
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (61 Connection refused)
What have I missed?
The default port for MySQL is 3306.
Your Django app is running on port 8000, change that value in your DB config.
It should look like this:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'closerdb',
'HOSTNAME': '127.0.0.1',
'PORT':'3306',
'USER': 'root',
'PASSWORD': 'closerdbpassword',
'OPTIONS': {
'autocommit': True,
},
}
}

Categories