I am building a flask project for internal use at my company. I start by using flask and postgre and used sqlalchemy and set up that connection using the model formats like below:
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
and that worked well. Now for security reasons we are switching to using Microsoft SQL Server with window authentication on an IIS server but i am having a lot of trouble connecting.
I changed the database URI to
SQLALCHEMY_DATABASE_URI = "mssql+pyodbc://server/database"
I've checked the SQL Sever configuration and I am pretty sure that is correct. However I get the error
psycopg2.OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
I have researched this issue and have used the pool_pre_ping solution but I get the same error (shown below)
SQLALCHEMY_ENGINE_OPTIONS = {"pool_pre_ping": True}
I have seen a lot of answers around this stuff that use the sqlalchemy.engine stuff but if possible I was hoping to use all of the db.models I have already written out but if that's not possible I can rewrite some stuff. Any help appreciated.
Windows Authentication will become a problem when you deploy to production. I recommend creating a SQL user to connect to your database.
Try this connection string:
mssql+pyodbc://<UID>:<PWD>#<SERVER>/<DATABASE>?driver=ODBC+Driver+17+for+SQL+Server;Trusted_Connection=no
Also make sure you've installed pyodbc msft docs
pip install pyodbc
Related
I am using .NET Core MVC API (.NET Core 6.0), actually just C# in general.
I need to deploy a .NET API on a linux server. However, it seems to that I can't connect to the SQL Server that is running on the same server.
This is the error from .NET
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.Exception: Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started.
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000001, 11): Resource temporarily unavailable
at System.Net.Dns.GetHostEntryOrAddressesCore(String hostName, Boolean justAddresses, AddressFamily addressFamily, ValueStopwatch stopwatch)
This is the error I get from curl
{"error":true,"message":"Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started."}
Keep in mind that both the API and the database are on the same server.
I think the problem comes from .NET. I am able to connect to the database on the server using the API locally.
This is the connection string in my appsettings.json
"ConnectionStrings": {
"ConnStr": "Data Source=<PUBLICIP>;Initial Catalog=<DBNAME>;User Id=<USER>;Password=<PASS>;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",=
"lockerConStr": ""
}
Any help would be greatly appreciated!
Things I have tried:
Changed Data Source to (local) - (doesn't work)
Added Encrypt= false;Trusted_connection=True; - (doesn't work)
Connect to the SQL Server from local version of the .NET API (successful)
Connect to the SQL Server using a python script running from the same server (successful)
Specified Driver in the connection string (doesn't work)
The python script (not sure if this would help)
import pyodbc
conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
'Server=<PUBLICIP>;'
'Database=<DBNAME>;'
'UID=<USER>;'
'PWD=<PWD>;'
'Trusted_Connection=no;')
cursor = conn.cursor()
cursor.execute('SELECT * FROM users')
I deleted the whole project on the server and cloned the git back.
After carefully reading the error, I noticed that it's running using my local connection string (not sure why, I changed the connection string on the server). I also rebuilt the solution and republished it a few times. Not sure what changed.
But I finally got it to work!
I'm trying to connect SSMS 18.5 to a localhost connection so I can play around with toy data to learn some things. I regularly use it to connect to external databases, but I am having a hard time getting it to work locally.
Here's what I've tried so far:
ATTEMPT 1:
I started by looking at the official documentation from Microsoft on getting this setup from this page.
My problem with this is that when I went to Database Engine --> Local Server Groups --> Tasks --> Register Local Servers I was always greeted with this error message:
I tried reinstalling my program to see if there was a problem with my configuration but that didn't work either.
Then I tried using the Connect Object Explorer and signing on using Windows authentication like so:
But that returned this message:
If I want to get a local connection, what do I do?
I am stuck and can't figure this one out.
I am using VS2017 to deploy a Angular 6 application with a Dot Net Core 2 back end that talks to a SQL Server DB on a remote server. I am not using and ORM or EF system, just straight ADO connections.
I am getting this error =
error: "Internal server errorLogin failed for user 'DOMAIN\MACHINENAME$'."
500 Internal Server Error
I have tried the following things:
making Sure the connection string does not have Integrated Security = true
making sure App Pool is set to No Managed Code
that the app is not set up to use NETWORK SERVICE or LocalSystem as login
This all seemed to start in the past 2 weeks. Before then, while the app was more simple, it was still connecting to DB just fine. So I am not sure what would cause this error.
I do not get this error when using the site locally on my dev machine (loccalhost). Everything connects and gets data just fine there.
This is only when I deploy to our servers...
Ideas?
EDIT FOR QUESTIONS
Connection string = data source=<IPADDRESS>;initial catalog=<DBNAME>;user id=<DBUSERNAME>;password=<DBPW>;Trusted_Connection=True;
You have Trusted_Connection=True; in your connection string; remove it, since you are trying to use a username and password rather than Windows authentication.
I'm trying to use DriverManager.getConnection() to connect to a SQL Server db from a Java application, but I keep getting "Login failed for user" errors with it. I've tried using both com.microsoft.sqlserver.jdbc.SQLServerDriver and net.sourceforge.jtds.jdbc.Driver to connect, but both keep hitting the issue.
Here's the code I'm using to connect:
Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:jtds:sqlserver://SERVERADDRESS:1433;DatabaseName=DBNAME;user=USER;password=PASS");
I know that account has access to that DB, as I've connected to it before from a Python application using pymssql.connect(SERVERADDRESS, USER, PASS, DBNAME) with the same server/DB/creds.
From this article, I eventually managed to get it working using windows authentication with my personal account, but I still can't get it to work using our service account. Does anyone have any insight into why the Python app can connect but the Java one can't?
pymssql is built on top of FreeTDS. Both FreeTDS and jTDS support an older Windows authentication scheme named NTLM, while current versions of Microsoft's JDBC Driver for SQL Server (mssql-jdbc) no longer support that authentication mechanism.
So, given that you've confirmed that pymssql can connect, you should be able to connect from your Java app as Windows user MYDOMAIN\username using jTDS like so:
String myUid = "username", myPwd = "mypassword";
String connUrl = "jdbc:jtds:sqlserver://192.168.1.123:1433/databasename;DOMAIN=MYDOMAIN";
Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd);
To find out the reason of "Login failed for user" one should go to SQL Server error log.
The next row to 18456 error will give you the reason.
The most probably reason of failure in your case is that the server is configurated to use Windows Authentication only
I'm working on setting up MediaWiki but I'm having trouble connecting to the SQL Server database.
Credentials are good but I'm getting the following error:
Cannot access the database: No connection could be made because the target machine actively refused it.
I've followed along with some mentions of installing some additional DLL's to support this but I'm either not setting them up correctly or this isn't the correct way to do this.
Are you are using a MSSQL account or a windows account credentials, make sure your connection is set for the right one or at least set it to accepts both.