I am attempting to connect to Mssql server that is not local. I am using SQLx and am attempting to connect using the following:
use sqlx::mssql::MssqlPool;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error>
{
let pool = MssqlPool::connect("mssql://server/db?trusted_connection=yes&driver=ODBC+Driver+17+for+SQL+Server")
.await?;
let result = sqlx::query("select top 100 * from db.dbo.tbl")
.execute(&pool)
.await?;
println!("{:?}", result);
Ok(())
}
I omitted server and db names for obvious reasons. Everything builds fine, however I believe my connection string is incorrect. All examples from other questions I have seen require a user/pass, but given this is on company premises, I want to connect in a similar fashion as how I would in Python using pyodbc and the SQL Server driver installed on my machine.
The error when trying to run:
Error: Database(MssqlDatabaseError { message: "Login failed for user 'sa'.", number: 18456, state: 1, class: 14, server: "server", procedure: "", line: 1 })
Related
I have just installed MS SQL server on my system and I am having trouble using it from python. It works fine from the Management Studio, and I can see all my tables. I also have an MySQL server installed and it works fine from python using sqlalchemy. I would like now to use MS SQL as well. Here is what I tried:
from sqlalchemy import create_engine
import pyodbc
server = 'NEW-OFFICE\\NEWOFFICE'
database = 'testdb'
username = 'NEW-OFFICE\user'
password = 'password'
DRIVER = "ODBC Driver 17 for SQL Server"
SERVERNAME = "NEWOFFICE"
INSTANCENAME = "\MSSQLSERVER_ZVI"
First I tried:
SQLALCHEMY_DATABASE_URI = "mssql+pyodbc://{username}:{password}#{hostname}/{database}".format(
username=username,
password=password,
hostname=server,
database=database,
)
Second I tried:
engine = create_engine('mssql+pyodbc://' + server + '/' + database + '?trusted_connection=yes&driver={DRIVER}')
Third I tried:
engine = create_engine(
f"mssql+pyodbc://{username}:{password}#{SERVERNAME}{INSTANCENAME}/{database}?driver={DRIVER}", fast_executemany=True
)
Then the next code for all tries is:
db = SQLAlchemy(app)
db.app = app
db.init_app(app)
engine_container = db.get_engine(app)
engine_container.dispose() # to close db connection
db.engine.connect()
hallDB.query.filter_by(client_id = 1).first()
At the last line I get an exception:
(sqlite3.OperationalError) no such table: hall
Of course table "hall" exists and I can query it in the Management Studio.
I am stuck so any help will be mush appreciated.
An Update
The following code did work, but I want the sqlalchemy model approach, which I am not able to use, as listed above.
u = 'DRIVER=ODBC Driver 17 for SQL Server;SERVER=NEW-OFFICE\user;DATABASE=testdb;Trusted_Connection=yes;'
cursor = pyodbc.connect(u).cursor()
cursor.execute("SELECT * FROM hall;")
row = cursor.fetchone()
while row:
print(row[0])
row = cursor.fetchone()
Maybe this will show what needs to be done in order to connect using sqlalchemy.
An Update 2
The error related to sqlite3 was resolved after I saw the following warning
UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:
I realized that although I set SQLALCHEMY_DATABASE_URI, in the test code, I skipped the line app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
After fixing it, and using:
SQLALCHEMY_DATABASE_URI = r'mssql+pyodbc://NEW-OFFICE\user/testdb;driver=ODBC Driver 17 for SQL Server;Trusted_Connection=yes'
or
SQLALCHEMY_DATABASE_URI = r"mssql+pyodbc://NEW-OFFICE\user/testdb?driver=ODBC Driver 17 for SQL Server?trusted_connection=yes"
the error now is:
Message=(pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC
Driver Manager] Data source name not found and no default driver
specified (0) (SQLDriverConnect)')
(Background on this error at: https://sqlalche.me/e/14/rvf5)
And also a warning:
A Warning: No driver name specified; this is expected by PyODBC when using DSN-less connections
Finally after many frustrating hours I found the problem -> the connection staring requires # at the beginning. The proper one is:
mssql+pyodbc://#NEW-OFFICE\user/testdb?driver=ODBC Driver 17 for SQL Server
I'm trying to connect to the MSSQL Server using Hibernate. Code I've wrote is ok, when I try to connect to the "normal" database, but I'm always getting exception for one database, which has instance.
Error is:
2020-03-06 15:07:01.755 WARN com.mchange.v2.async.ThreadPoolAsynchronousRunner: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector#12fa2433 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
2020-03-06 15:07:01.758 WARN com.mchange.v2.async.ThreadPoolAsynchronousRunner: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector#12fa2433 -- APPARENT DEADLOCK!!! Complete Status:
Managed Threads: 3
Active Threads: 3
Active Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask#5d00c227
on thread: C3P0PooledConnectionPoolManager[identityToken->2sk2t6a81cvnu23110fpe1|11389053]-HelperThread-#0
com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask#1e98ae08
on thread: C3P0PooledConnectionPoolManager[identityToken->2sk2t6a81cvnu23110fpe1|11389053]-HelperThread-#2
com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask#6c17f8ef
on thread: C3P0PooledConnectionPoolManager[identityToken->2sk2t6a81cvnu23110fpe1|11389053]-HelperThread-#1
Pending Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask#6665ca79
com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask#3d2f3d68
Pool thread stack traces:
Thread[C3P0PooledConnectionPoolManager[identityToken->2sk2t6a81cvnu23110fpe1|11389053]-HelperThread-#0,5,main]
java.lang.Thread.sleep(Native Method)
com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2336)
com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2067)
com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1204)
com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:825)
com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)
com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
Using the same login password I can connect to the database but not programmatically, i.e. using sqlcmd.
How I create connection:
public synchronized static SessionFactory getSessionFactory(Logger log, DbServer dbServer, String dbName) {
String url = String.format("jdbc:sqlserver://%s:%d;databaseName=%s", dbServer.getServerName(), dbServer.getPort(), dbName);
return new Configuration().configure()
.setProperty("hibernate.connection.driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
.setProperty("hibernate.default_schema", "dbo")
.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect")
.setProperty("hibernate.connection.username", dbServer.getLogin())
.setProperty("hibernate.connection.password", dbServer.getPassword())
.setProperty("hibernate.connection.url", url)
.buildSessionFactory();
}
dbServer.getServerName() returns "serverName\dbInstance", port is 1433, dbName is master.
Using Microsoft SQL Management Studio I can connect using the same login/password so I'm pretty sure that it's problem with configuration - I'm using sys admin (sa) so it's not permissions issue.
Solution was not to use port when I try connect to database. So connection string looks like this:
String url = String.format("jdbc:sqlserver://%s;instanceName=%s;databaseName=%s", dbServer.getServerName(), dbServer.getInstanceName(), dbName);
I have a MongoDB database I have storing a large dataset. I am trying to import my database into MATLAB to perform data visualization, but am getting strange errors that I have not been able to debug in doing so. I have my database running using mongod on localhost on port 27017.
I do have the Database Toolbox add on for MATLAB, and the MongoDB specific one as well.
Following the example of the MATLAB documentation, I created variables:
server = "0.0.0.0" (I have also tried putting localhost here)
port = 27017
dbname = "NYPDData"
conn = mongo(server, port, dbname)
When I do so, I recieve the error that:
Error using error
Unable to load a message catalog 'mongodb:mongodb'. Please check
the file location and format.
Error in mongo (line 389)
error(message('mongodb:mongodb:DriverError',mongo.extractExceptionMessage(e)));
When I try simple running conn = mongo() with any parameters however, it seems to work, and returns:
conn =
mongo with properties:
Database: ''
UserName: ''
Server: {}
Port:
CollectionNames: {}
TotalDocuments:
If you have any insight on how to solve this error, please let me know. Thank you!
Type this before calling the mongo function : registerrealtimecataloglocation('c:\ProgramData\MATLAB\SupportPackages\R2017b')
It should return 1 if everything goes well. Don't forget to change the previous line by your number of version (R2017b here) with yours.
And then it should find the message in the catalog.
I need to query an on-premises SQL Server Express database from my Azure app service based on Nodejs. I followed this tutorial to add a hybrid connection.
I successfully connected and added this connection to my service. I also added the connection string as:
Server=LOCALHOST\\SQLEXPRESS,1433;Database=smartpointmovil_db;User ID=sa;Password=pass1009
Then, I wrote an easy api in node JS for my azure mobile app service:
"get": function (req, res, next) {
var sql = require("mssql");
var config = {
server: 'LOCALHOST\\SQLEXPRESS',
user: 'sa',
password: 'pass1009',
database: 'smartpointmovil_db',
port: 1433
};
var conn = new sql.Connection(config);
var req=new sql.Request(conn);
conn.connect(function(err){
if(err){
console.log("Error connectig: "+err);
return;
}
req.query('select * from smartpointmovil.cat_cadenas where id=1',function(err,results){
if(err){
console.log("Error during query: "+err);
return;
}
else{
console.log("Success: "+results[0].cadena);
res.json(results[0]);
}
conn.close();
});
});
}
Every time I call this API, I get the following error message:
Error connectig: ConnectionError: Failed to connect to
LOCALHOST:undefined in 15000ms
I did not find the way to define in my code the connection string to be used for the query, so I am including the configuration parameters.
Any idea how to write a node js code to query my local database from an Azure hosted service?
Use the hostname (Computer Name) of your on-prem server when defining the Hybrid Connection. Then use the same name to reference that machine in your App Service code.
e.g.
Server=SQL-SRV-01\SQLEXPRESS,1433; [...]
Hybrid Connections work by hooking the getaddrinfo system call, and it's probably unable to tell apart your LOCALHOST from the actual LOCALHOST (127.0.0.1) of the VM that App Service runs on top of.
Test with sqlcmd.exe from the Kudu Console:
D:\home>sqlcmd -S tcp:SQL-SRV-01,1433 -U {username} -P {password}
-Q "SELECT NAME FROM sys.sysdatabases"
NAME
-------------------
master
MobileServiceZZZ_db
(2 rows affected)
I got it working, these are the changes I did in my code to make it work:
Changed from server name from 'LOCALHOST' to my server host name: 'quandojhv' which is the same name used as host name in the hybrid connection.
I removed the instance name '\SQLEXPRESS' in the server configuration parameter.
New configuration parameters in my code are:
var config = {
server: 'quandojhv',
user: 'sa',
password: 'pass1009',
database: 'smartpointmovil_db',
options: {
encrypt: true
}};
I am trying to connect to a MS SQL Server database from my NodeJS server application and while it works flawlessly on my local machine using localhost/127.0.0.1 when I push it to a real server I get an Auth error.
The error I am getting is as follows:
The login is from an untrusted domain and cannot be used with Windows
authentication
So I am thinking that maybe its different domains but my domains are as follows:
000001.mysubdomain.mydomain.com
ct000002.mysubdomain.mydomain.com
So I'm not a networking guy but I would assume that in this case both the MS SQL Server and my NodeJS Server are actually on the same domain, am I correct in assuming that or incorrect?
Some more info - in both cases the IP addresses share the same first number but the rest are different - SS.XX.XX.XX - Where SS is the same numbers and XX are different - does this suggest that they are in fact on different domains?
In addition to this if it was a domain issue then why would it work on my local machine?
So if we can eliminate that it is not a domain issue then I'm not sure where to go, here is my code, I am using the Tedious-NTLM NodeJS module - https://www.npmjs.com/package/tedious-ntlm
var tds = require("tedious-ntlm"); //Get the tedious model for NTLM connection to the SQL Server
//Set up config for logging into the DB
var config = {
userName: 'myusername', //Username
domainName: "mydomain", //Domain
password: 'mypassword', //Password
server: '000001.mysubdomain.mydomain.com' //Database address
};
function getDataFromSQLServer(callback){
var connection = new tds.Connection(config); //Configure a connection
//When the database is connected to we get this event
connection.on('connect', function(err) {
// If no error, then good to go...
if(err){
console.log('err = ' + err); //Log the error to the console
}
else{
executeStatement(callback); //Execute the test statement
}
}
);
As you can see here when my function is called I log an error, I get an error here when trying to run the code from my server but I have no issue when running it from my local machine.
To get more information on the error I have the following code:
connection.on('errorMessage', function(err){
console.log('full error = ' + JSON.stringify(err)); //Log the error to the console
});
Which gives me the following info:
full error = {"number":18452,"state":1,"class":14,"message":"Login
failed. The login is from an untrusted domain and cannot be used with
Windows
authentication.","serverName":"000001","procName":"","lineNumber":1,"name":"ERROR","event":"errorMessage"}
Again if run on my local machine everything works correctly but if I run from my server I get the errors, I am wondering if this really is a domain issue or if the error message is incorrect and there is something else wrong?