I've got problem with connection string to connect to SQL Server.
Now I have:
from sqlalchemy import create_engine
drv='ODBC Driver 18 for SQL Server'
con_string=f'mssql+pyodbc://{user}:{passwd}#{server_ip}:1433/{db_name}?driver={drv}'
con=create_engine(con_string)
and I get an error
pyodbc.OperationalError Login timeout expired
What am I doing wrong?
UPDATE: I'm doing this from Ubuntu
Try using SQLAlchemy's URL object to build the connection string for you, e.g.:
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
connection_url = URL.create(
"mssql+pyodbc",
username="YourUsername",
password="YourPassw0rd",
host="127.0.0.1",
port=1433,
database="ExampleDb",
query={
"driver": "ODBC Driver 18 for SQL Server",
"Encrypt": "yes",
"TrustServerCertificate": "yes",
},
)
engine = create_engine(connection_url)
Related
I am running Oracle XE 21c in a Docker container and I can connect to it with a JDBC thin connection using the JDBC url jdbc:oracle:thin:#localhost:1521:XE using the SYSTEM account. I can also log into the terminal Docker instance and connect to the database fine.
When I try to connect to it using NodeJS OracleDB example code described here:
https://node-oracledb.readthedocs.io/en/latest/user_guide/connection_handling.html, I cannot get the client to connect using either:
connection = await oracledb.getConnection({
user : "hr",
password : mypw
connectString : "localhost/XEPDB1"
});
or
connection = await oracledb.getConnection({
user : "SYSTEM",
password : "MyPassword",
connectString : "localhost/XE"
});
I get an error ORA-01017: invalid username/password; logon denied.
I can connect to Oracle XE using sqlplus either by logging into the Docker container or from the host Mac using:
sqlplus SYSTEM/Password#localhost:1521/XE
if I change the nodejs code to:
const connection = await oracledb.getConnection({
//user: "SYSTEM",
//password: "Password42",
//connectString: connectionURI.url
connectString: "SYSTEM/Password#localhost:1521/XE"
});
I get the error:
ORA-12154: TNS:could not resolve the connect identifier specified
Any ideas why I can connect with sqlplus but not NodeJS OracleDB?
Unable to connect to mssql server from qt application in ubuntu 20.04
Sql server located in windows 7
The connection is definitely present, because it was possible to connect through DDbeaver.
Can anyone help me in this situation?
Code:
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC3");
db.setConnectOptions();
QString serverName = "566***";
QString ipName = "tcp:192.168.144.101,1433";
QString dbName = "St***";
QString connectionString = QString("DRIVER={ODBC Driver 18 for SQL Server};Server=%1;Database=%2;").arg(ipName).arg(dbName);
db.setDatabaseName(connectionString);
db.setUserName("sa");
db.setPassword("top123TOP");
if (db.open())
{
qDebug() << "Correct connection";
}
else
{
QString error = db.lastError().text();
qDebug() << error;
}
otuput is:
"[Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x2746 [Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection QODBC3: Unable to connect"
odbcinst.ini
[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.0.so.1.1
the following solution helped:
from:https://github.com/microsoft/msphpsql/issues/1112
This was helpful.
Ubuntu 20.04 PHP 7.4 using the 19.10 drivers for sqlsrv
We have a real old SQL server out there . The SQL Server show's version 10.50.2550.0, i think it's SQL Server 2008 R2. I had to use TLSv1 to connect to the server. I also had to do a "systemctl restart apache2" to get it to take affect. TLSv1.1 did not work with my MSSQL server version.
Error message: Connection failed: SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol]Database Connection Error
edit: /etc/ssl/openssl.cnf
1st line in the file added
openssl_conf = default_conf
End of file added
[default_conf]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
MinProtocol = TLSv1
CipherString = DEFAULT#SECLEVEL=1
Not 100% sure why i had to restart apache2 for it to take effect, but I had to.
systemctl restart apache2
reloaded the page and it works
I have pulled a docker image that runs an MS SQL from docker hub:
docker pull mcr.microsoft.com/mssql/server:2017-latest
docker run --name some-db -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD=Aamerge1234 -e MSSQL_PID=Developer -d -p 1433:1433 mcr.microsoft.com/mssql/server:2017-latest
The container runs fine, I added a strong password as required.
This is the python code I am using to connect with the database using pyodbc:
import pyodbc
server = 'localhost:1433'
# database = ''
username = 'sa'
password = 'Aamerge1234'
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER='+server+';UID='+username+';PWD='+ password)
I have omitted the variable database because mssql creates several.
I tried using the following ODBC drivers:
ODBC Driver 17 for SQL Server
SQL Server
However, it always returns error:
OperationalError: ('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]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 am trying to use python in a virtual environment to write a dataframe to sql server. I can read from the server with my pyodbc connection, but can't write to it using that connection, so I'm using a sqlalchemy engine, and credentials stored in a .env file.
Printing the raw string returns:
'mssql+pyodbc://User:Password#Server/Database?trusted_connection=no&driver=ODBC+Driver+17+for+SQL+Server'
but when printing the engine it returns:
'mssql+pyodbc://User:***sword#Server/Database?trusted_connection=no&driver=ODBC+Driver+17+for+SQL+Server'
If I try to connect using the engine, the login timeout expires, which I'm assuming is because the engine isn't passing the correct credentials.
Is there something going on with my string formatting?
import os
from sqlalchemy import create_engine
from dotenv import load_dotenv
load_dotenv()
credentials = [os.getenv('UID'), os.getenv('PWD'), os.getenv('Server'), os.getenv('Database')]
engine = create_engine('mssql+pyodbc://{0}:{1}#{2}/{3}?trusted_connection=no&driver=ODBC+Driver+17+for+SQL+Server'.format(*credentials),fast_executemany=True)
print('mssql+pyodbc://{0}:{1}#{2}/{3}?trusted_connection=no&driver=ODBC+Driver+17+for+SQL+Server'.format(*credentials))
print(engine)
engine.connect()
The __repr__ method for Engine purposely obfuscates the password, replacing it with ***:
import sqlalchemy as sa
connection_url = sa.engine.URL.create(
"mssql+pyodbc",
username="gord",
password="p#ssword",
host="192.168.0.199",
query={
"driver": "ODBC Driver 17 for SQL Server",
},
)
print(connection_url)
# mssql+pyodbc://gord:p%40ssword#192.168.0.199?driver=ODBC+Driver+17+for+SQL+Server
engine = sa.create_engine(connection_url)
print(engine)
# Engine(mssql+pyodbc://gord:***#192.168.0.199?driver=ODBC+Driver+17+for+SQL+Server)
Note that in this particular case the username and password arguments are irrelevant as Trusted_Connection=Yes is being used.
connection_url = sa.engine.URL.create(
"mssql+pyodbc",
host="192.168.0.199",
query={
"driver": "ODBC Driver 17 for SQL Server",
"Trusted_Connection": "Yes",
},
)
This is what you should do....
credentials = [username, password, server, database]
engine = create_engine('mssql+pyodbc://{0}:{1}#{2}/{3}?trusted_connection=no&driver=ODBC+Driver+17+for+SQL+Server'.format(*credentials),fast_executemany=True)
I'm trying to connect node.js to mssql in Windows Authentication mode. I installed the tedious,mssql and msnodesqlv8 modules, but I still can't figure out how to open a connection.
This is what I tried:
var sql = require('mssql');
var config = {
driver: 'msnodesqlv8',
server: 'POPPY-GI\\SQLEXPRESS',
database: 'NodinSports',
options:{
trustedConnection: true,
useUTC: true}}
sql.connect(config).then(function() {
new sql.Request().query('select * from users')
.then(function(recordset){
console.log(recordset);
}).catch(function(err) {
console.log(err);});
}).catch(function(err) {
console.log(err);});
After running I get a long error saying:
`ConnectionError`: Port for `SQLEXPRESS` not found in
`ServerName`;POPPYGI;`InstanceName;SQLEXPRESS;IsClustered`;No;Version;12.0.2000.8;;
at Connection.tedious.once.err (D:\Never Lazy\University\`AN2, SEM 2\WEB\`Projek`\node_modules\`mssql`\lib\`tedious.js:216:17`)
at Connection.g (events.js:291:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at D:\Never Lazy\University\AN2,SEM2\WEB\Projekt\node_modules\tedious\lib\connection.js:570:27
at D:\Never Lazy\University\AN2,SEM2\WEB\Projekt\node_modules\tedious\lib\instance-lookup.js:91:24
at Socket.onMessage (D:\Never Lazy\University\AN2,SEM2\WEB\Projekt\node_modules\tedious\lib\sender.js:140:9)
at emitTwo (events.js:106:13)
at Socket.emit (events.js:191:7)
at UDP.onMessage (dgram.js:549:8)
code: 'EINSTLOOKUP',
I would be really thankful for any help.
FIXED:
In services.msc check if the followings are enabled:
SQL Server(*server_name*) -- in my case `SQLEXPRESS`
SQL Server Browser
SQL Server Agent(*server_name*) -- if you are using `SQLEXPRESS` this doesn't need to run
In SQL Server Configuration Manager -> Protocols for server_name: enable TCP/IP.
To make sure everything will be fine, check the port the server is using (SQL Server Configuration Manager -> SQL Native Client Configuration -> Client Protocols -> double click on TCP/IP -> Default Port ) , and add the port: *your_port* to the code in var config = { ... }.
Lastly, change var sql = require('mssql'); to var sql = require("mssql/msnodesqlv8");
Install the following modules:
"dependencies": {
"msnodesqlv8": "^0.4.14",
"mssql": "^4.1.0"
}
My node version: v8.1.4
const sql = require("mssql/msnodesqlv8");
const main = async () => {
const pool = new sql.ConnectionPool({
server: "myservername",
database: "mydbname",
options: {
trustedConnection: true
}
});
await pool.connect();
const request = new sql.Request(pool);
const query = `SELECT [FirstName]
,[LastName]
,[Email]
FROM [Dev].[Users]`;
const result = await request.query(query);
console.dir(result);
};
main();
(You can do it without async or older versions: https://stackoverflow.com/a/40406998/639575)
The solution given by beatoss works for windows but not for Linux/mac. The msnodesqlv8 driver only works for Windows. For Linux/mac environment, try odbc instead.
It may be too late to answer, but this recently happened with me and it drove me crazy!!! I was trying to connect my db to express and I was working with windows authentication mode. For two long days I kept googling and refreshing until I got this article: https://www.liquidweb.com/kb/troubleshooting-microsoft-sql-server-error-18456-login-failed-user/
So in a nutshell;
First I installed the msnodesqlv8 driver for windows authentication, then in the my server on ssms, I right clicked on my server then in properties and then security, I enabled sql server and windows authentication mode, then in the object explorer, clicked on the plus next to the server, then security, then logins. There I saw sa with a cross next to it. In it's properties, I changed my password to something easier to remember (optional), then in the status, set the login to enable. PHEW!
Now my config code: <br / >
const config = {
user: 'sa',
password: '123',
driver: "msnodesqlv8",
server: 'UZAIR-S_PC\\SQLEXPRESS',
database: 'LearningExpressJS',
options: {
trustedconnection: true,
enableArithAbort : true,
trustServerCertificate: true,
instancename : 'SQLEXPRESS'
},
port: 58018
}
This works finally !!!!!!!!!!!!!!!