I am trying to connect to an oracle database in Python using create_engine. This database does not have a username or password.
I see this is the format now:
oracle_db = sqlalchemy.create_engine('oracle://user:pass#server').
However, if this connection has NO username or password, how would the connection string look? I've tried DMIT_connection = create_engine('oracle+cx_oracle://#....) with no luck. When I go to write a pandas df to the database using to_sql I get the error below because I cannot get the connection right give that there is no username or password.
The error occurs because this database has no username (picked up from the localhost machine) and there is no password.
The error I get is this: DatabaseError: (cx_Oracle.DatabaseError) ORA-12545: Connect failed because target host or object does not exist (Background on this error at: http://sqlalche.me/e/14/4xp6)
Let me know authentication type used. If its external authentication , picking credentials from wallet, you can try sample code mentioned here
How to use SqlAlchemy to connect Database similar to cx_oracle when we use external authorization like wallets with TNS(net service name)
Related
I'm using sqlalchemy trying to connect to Teradata via ODBC as I need to be able to read/write to Teradata.
from sqlalchemy import create_engine
import sqlalchemy_teradata
user = 'user'
pasw='pasw'
host = 'host'
# connect
td_engine = create_engine("teradata://"+user+":"+pasw+"#"+host+"/?authentication=ODBC?driver=Teradata")
#execute sql
sql = "select * from table"
result = td_engine.execute(sql)
However I get the following error.
(teradata.api.DatabaseError) (0, '[28000] [Teradata][ODBC Teradata Driver] User Specified Mechanism for Logon is Not Available')
(Background on this error at: http://sqlalche.me/e/4xp6)
The link provided is not very informative unless I'm missing something. The error is from Teradata but I'm not sure what it actually means. It looks like it saying that I can't use ODBC? Any suggestions or alternatives?
I am trying to connect to Hive with Kerberos authentication. But I am getting an error 'NotImplementedError: authMechanism is either not supported or not implemented'
I guess I need to set authMechanism to Kerberos somehow.
import pyhs2
with pyhs2.connect(host='wx0000',
port=10000,
authMechanism="Kerberos",
user='xxxxx',
password='******',) as conn:
From the source authMechanism is case sensitive, so you'll need it to be "KERBEROS".
This connection string will work as long as the user running the script has a valid kerberos ticket or key tab:
import pyhs2
with pyhs2.connect(host='beeline_hostname',
port=10000,
authMechanism="KERBEROS") as conn:
with conn.cursor() as cur:
print cur.getDatabases()
Username, password and any other configuration parameters are not passed through the KDC.
When I run this query to bulkinsert a file on a shared drive to SQL server 2008 with username and password (not Windows authentication), I get these errors. DBA, system admins and network guys are all denying these errors are related to their teams and I am lost... Can anyone please help me to identify where the issue is? When I run bulkinsert with database username and password, what authentication does SQL server use to open the file?
Run this on MS Management Studio
BULK INSERT DatabaseName.dbo.TableName
FROM '\\shared_server\parent\child\file_name.txt'
WITH(FIRE_TRIGGERS, DATAFILETYPE='char', FIELDTERMINATOR='\t',ROWTERMINATOR='\n', FIRSTROW=2);
and I get
Cannot bulk load because the file "\\shared_server\parent\child\file_name.txt" could not be opened. Operating system error code 5(Access is denied.).
Run this on python
import pyodbc
database = 'DatabaseName'
username = 'username'
password = 'password'
server = 'server_name'
failover = 'failover_server_name'
cnxn_string = 'DRIVER={SQL Server Native Client 10.0};SERVER=%s;FAILOVER_PARTNER=%s;DATABASE=%s;UID=%s;PWD=%s;CHARSET=UTF8' % (server, failover, database, username, password)
cnxn = pyodbc.connect(cnxn_string)
cursor = cnxn.cursor()
query = r"""
BULK INSERT Estimates.dbo.FundamentalsIS
FROM '\\shared_server\parent\child\file_name.txt'
WITH(FIRE_TRIGGERS, DATAFILETYPE='char', FIELDTERMINATOR='\t',ROWTERMINATOR='\n', FIRSTROW=2);
"""
cursor.execute(query)
cursor.commit()"
and I get
ProgrammingError: ('42000', '[42000] [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot bulk load because the file "\\shared_server\parent\child\file_name.txt" could not be opened. Operating system error code 1326(Logon failure: unknown user name or bad password.). (4861) (SQLExecDirectW)')
Could the MS SQL server 2008 possibly be on a different security group (or have different settings) than the shared drives, where the file is located?
Because the bulk insert operation is run on the MS Management studio server side, it might not have access to the file, the 'access denied' leads me to believe DB server cannot get to shared file drive, and possibly does not have permission to access it. Likewise, even if using python to execute the BULK INSERT statement, the DB server still needs to have access to where ever the file is located.
I had a similar issue in the past, because the DB server could not get to the shared file, located elsewhere. My workaround was to use local computer to read in the file and run the insert queries using python. It sounds like the local environment has access to both and can be used as the central communication hub. You might have to do something similar to
https://stackoverflow.com/a/6482610/3761363
https://stackoverflow.com/a/11219626/3761363
I'm connecting Hive use pyhs2. But the Hive server required Kerberos authentication. Anyone knows how to convert the JDBC string to pyhs2 parameter? Like:
jdbc:hive2://biclient2.server.163.org:10000/default;principal=hive/app-20.photo.163.org#HADOOP.HZ.NETEASE.COM?mapred.job.queue.name=default
I think it will be something like this:
pyhs2.connect(host='biclient2.server.163.org',
port=10000,
authMechanism="KERBEROS",
password="something",
user='your_user#HADOOP.HZ.NETEASE.COM')
I'm also doing the same, I still not succeed, but at least having a meaningful errorcode:
(Server hive/xxx#yyy.COM not found in Kerberos database)
This connection string will work as long as the user running the script has a valid kerberos ticket:
import pyhs2
with pyhs2.connect(host='biclient2.server.163.org',
port=10000,
authMechanism="KERBEROS") as conn:
with conn.cursor() as cur:
print cur.getDatabases()
Username, password and any other configuration parameters are not
passed through the KDC.
How can I connect to a SQL Server database using user login/password that is in another domain?
If I use my account to connect to DB, it works fine:
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=server_name;DATABASE=testdb;UID=MY_Domain\\username;PWD=pass; Trusted connection=YES')
But I need to use another user's credentials like
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=server_name;DATABASE=testdb;UID=Another_Domain\\username;PWD=pass; Trusted connection=YES')
When I try the latter I get an error of "failed login for MY_Domain\username" rather than
for the user "Another_Domain\username".
In both cases by running SQL Server Management Studio I can use Windows Authentication to connect to the db.
You can not pass a UID and Password and set Trusted_connection=True (your second connection string) to connect as a (impersonated) windows user. You can either connect as a SQL Server user (username and password) or as a windows authenticated user (trusted connection).
Your code should impersonate the windows user (as SSMS does) and then set Trusted_connection=True only.
This MSDN page WindowsIdentity.Impersonate has an example.
Since this works from SSMS it suggests there is the necessary trust between the domains.