Forums

Connecting to a remote SQL Server

I'm pretty new to all of this. I have a MS SQL Server that I'm running a database on for my flask web application. I'm now trying to get my app on python anywhere connected to my SQL Server. When I'm running the flask web app on my loacl machine (different machine than the SQL Server). I configure files and then connect using sqlalchemy. I'm not sure how to do this in python anywhere because I can't edit the configuration files. Can anyone help me out?

I use the following connection details (key info redacted).

This is how I set up the three configuration files on my machine:

#/etc/freetds/freetds.conf

[MSSQL]
host = XXX.XXX.XXX
port = 1433
tds version = 4.2

# /etc/odbc.ini
[MSSQL]
Description =MSSQL Server
Driver =FreeTDS
Database=CashFlow
ServerName =MSSQL
Port =1433

#/etc/odbcinst.ini
[FreeTDS]
Description=Free TDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

Python Script to connect using sqlalchemy:

import pymssql
from sqlalchemy import *

connection_string = r"mssql+pymssql://{0}:{1}@MSSQL/CashFlow".format('username','password')
engine= create_engine(connection_string)

It looks from reading the docs for the libraries like you can put the config files in your home directory: freetds docs, pymssql docs

This doesn't seem to work. I've put them in my home directory. The default location is /etc/freetds. I've tried changing that by changing the environment variables:

export SYBBASE=~/
export FREETDS=~/

Yet when I do tsql -C, I still get that it is using the /etc/freetds config file:

Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /etc/freetds
MS db-lib source compatibility: no
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes

Did you name them correctly? With the preceding dot?

I did.

I found that the local user can setup the odbc with .odbc.ini
So here is what I have now:

#~/.odbc.ini
[MSSQL]
Description =MSSQL Server
Driver =libtdsodbc.so
Database=CashFlow
ServerName =MSSQL
Port =1433

and

#~/.freetds.conf
[MSSQL]
host = 192.XXX.XXX.XXX
port = 1433
tds version = 4.2

I try and connect with:

tsql -S MSSQL -p 1433 -U username

I receive the error:

126Error 20009 (severity 9):
    Unable to connect: Adaptive Server is unavailable or does not exist
    OS error 110, "Connection timed out"
There was a problem connecting to the server

I see that you're hitting a 192. IP address. If the second octet of that IP address is 168, then it's a local, non-routeable address and your server is behind a firewall or some sort of NAT translation. There is no way that PythonAnywhere can see that machine unless you open it to the internet.

Thanks Glenn. It's actually not 168, but I have finally isolated the problem and it looks like it's not my connection setup, but I'm at a university and it's the university's firewall that appears to be blocking the connection. When I use a remote computer and VPN into the network and then try to connect it works just fine. I'm working with the university to try and open up a way to access it without having to login through the VPN.

More about....SqlServer Connection

Riyan