I'm trying to connect to a SQL Server named instance from python 3.4 on a remote server, and get an error.
File "C:\Scripts\Backups Integrity Report\Backup Integrity Reports.py", line 269, in
conn = pymssql.connect(host=r'hwcvcs01\HDPS', user='My-office\romano', password='PASS', database='CommServ')
File "pymssql.pyx", line 636, in pymssql.connect (pymssql.c:10178)
pymssql.OperationalError: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')
Other SQLs are connected without a problem. Also I manage to connect to the SQL using the Management Studio, from the same remote server.
Tried different ports, tried to connect to the host itself rather than the instance, and also tried pypyodbc.
What might be the problem?
For a named instance that looks like this:
myhost\myinstance,1435
You can connect via pymssql with the following:
pymssql.connect(host='myhost', server='myinstance', port='1435', user='user', password='pw')
According to the pymssql documentation on the pymssql Connection class, for a named instance containing database theDatabase, looking like this:
myhost\myinstance
You could connect as follows:
pymssql.connect(host=r'myhost\myinstance', database='theDatabase', user='user', password='pw')
The r-string is a so-called raw string that does not treat the '' as an escape.
Related
Developing a desktop application with tkinter, I had an error connecting the database to it.
I'm new to this, I was working locally but I don't know what would happen if I want to share my app with other people. The app would not have access to this database.
I tried to create a database in render.com, once created I made the connection to it through psycopg2 passing the data (hostname, port, db_name, username, and password), but it throws me the error psycopg2:OperationalError.
I know that I am doing something wrong and I have been investigating but I have not been able to find the solution to this. So, when deploying a tkinter app, what database could it use? so that everyone can use the app correctly
I would appreciate any help you can give me, thanks in advance.
Here are the connection settings, which are the same settings from the database created on render.com (I've hidden the password and host):
conn = psycopg2.connect(
dbname='tkinter_app',
user='tkinter_app_user',
password='#############',
host='dpg-cfkimi9mbjsn9eclo3dg-a',
port='5432',
)
And here is the error log:
Traceback (most recent call last):
File "c:\Users\luigi\Desktop\Programacion\tkinter-postgresql-desktop\src\student.py", line 134, in <module>
display_students()
File "c:\Users\luigi\Desktop\Programacion\tkinter-postgresql-desktop\src\student.py", line 27, in display_students
conn = psycopg2.connect(
File "C:\Users\luigi\Desktop\Programacion\tkinter-postgresql-desktop\venv\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "dpg-cfkimi9mbjsn9eclo3dg-a" to address: Unknown host
When I created the database, I copied all the configurations as they were, investigating I read that the host should be the External Database URL showed in the rander database configuration, but it didn't work either and it also throws me the error: Unknown Server error
I also try to connect the database in pgAdmin but throws me the same error Unknown host
Python 3.8
Mysql 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
Hi,
I want to connect to a distant mysql server using python's mysqldb connector and paramiko's sshtunnelforwarder.
I can connect to the database remotely without any problems by executing the following:
Connecting to database using mysql password authentication
server = new_ssh_server(config)
with server:
print('Connection', server.local_bind_address)
cnx = MySQLdb.connect(host = '127.0.0.1',
port = server.local_bind_port,
user = config['user'],
passwd = config['password'],
db = config['db'])
Queries work, I can read/write to database, no problem.
I would like to connect to database without supplying mysql password, by using mysql auth_socket authentication method, through ssh.
My attempts at this can be resumed by the following code:
Connecting to database using mysql auth_socket authentication
with server as tunnel:
print('Tunnel:', tunnel.local_bind_address)
cnx = MySQLdb.connect(host = 'localhost', user = 'hillbilly', password = '', db='tutut')#, unix_socket="/tmp/mysql.sock")
res = pd.read_sql('select * from users;', cnx)
print(res)
Which throws the following error:
File "connect_ssh_mysql_auth_socket.py", line 12, in <module>
cnx = MySQLdb.connect(host = 'localhost', user = 'hillbilly', password = '', db='rsotest2')#, unix_socket="/tmp/mysql.sock")
File "...../lib/python3.8/site-packages/MySQLdb/__init__.py", line 84, in Connect
return Connection(*args, **kwargs)
File "...../lib/python3.8/site-packages/MySQLdb/connections.py", line 179, in __init__
super(Connection, self).__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
I have and existing mysqld.sock on the distant server that I symlinked to /tmp/mysql.sock, but the error remains. I have also added the next line to /etc/mysql/mysql.conf.d/mysql.cnf:
socket=/run/mysqld/mysqld.sock
But I still get the same error when trying to connect remotely.
Specifying the unix_socket='/run/mysqld/mysqld.sock' to mysqldb connector (commented in Connecting to database using mysql auth_socket authentication) does not fix the issue.
I seem to misunderstand the use of mysql.sock, mysqld.sock.
I was not able to find nor create a mysql.sock socket.
Is what I am trying to do possible? I remember reading somewhere that unix sockets only work locally, does this mean it is not achievable?
Any help/explanation would be appreciated.
(EDIT AND CLOSING)
So this is not possible. Following this thread, auth_socket needs local access to the socket file (usually /tmp/mysql.sock) to run autentication tests, so not accessible through ssh tunneling.
Authentication to remote mysql server using auth_socket plugin is not possible through sshtunnel, as the plugin requires local access to the socket file. See this thread for more information.
I need to create a linked server in MS SQL Server for 2 AWS RDS servers,
from Python 2.7. For this I use the pymssql library. The SQL commands are executed with a SQL Client without problems, the linked servers are created, they can be consulted that exist and can be used.
The problem occurs when you execute a SQL creation in Python (from an instance AWS Linux, in the same AZ), then you get the following error message.
Simple SQL queries from python (such as a Select) work without problems using pymssql.
The same happens when a stored procedure is created in the database and it called from python, if the instructions to create the linked servers are included then the same error is received. If you create the procedure in the sql in python the result is similar.
Does anyone know how to do it? Thank you very much for the help.
Linkedserver Commands:
EXEC master.dbo.sp_addlinkedserver #server = N'linkserver1', #srvproduct=N'', #provider=N'SQLNCLI', #datasrc=N'aa.bb.cc.dd';
EXEC master.dbo.sp_dropserver #server = N'linkserver1';
EXEC sys.sp_linkedservers;
Python Code:
**import pymssql
def main():
conn = pymssql.connect(server, user, clave, base)
cursor = conn.cursor()
cursor.execute("EXEC master.dbo.sp_addlinkedserver #server = N'RDSPrivate01', #srvproduct=N'', #provider=N'SQLNCLI', #datasrc=N'win-01.nnnnnn.us-west-1.rds.amazonaws.com'")
if __name__== "__main__":
main()**
Message Error:
cursor.execute("EXEC master.dbo.sp_addlinkedserver #server =
N'RDSPrivate01', #srvproduct=N'', #provider=N'SQLNCLI',
#datasrc=N'win-01.nnnnnnnnnnn.us-west-1.rds.amazonaws.com'") File
"src/pymssql.pyx", line 468, in pymssql.Cursor.execute
pymssql.OperationalError: (15002, "The procedure
'sys.sp_addlinkedserver' cannot be executed within a transaction.
DB-Lib error message 20018, severity 16:\nGeneral SQL Server error:
Check messages from the SQL Server\n")
Links used:
https://aws.amazon.com/blogs/database/implement-linked-servers-with-amazon-rds-for-microsoft-sql-server/
http://www.pymssql.org/en/stable/pymssql_examples.html#important-note-about-cursors
When I try to connect to my postgres server via psycopg2 with a bit of code like this (running python as "username"):
psycopg2.connect(database="apis_master")
I get an error
psycopg2.OperationalError: FATAL: Ident authentication failed for user "username"
But when I run psql straight from the command line (as user "username") like so:
psql -d apis_master
I connect with no problem.
I can't see what is different between these two connection methods. Am I missing some configuration option with psycopg2?
Your command line user and your python user are different. When you instantiate psycopg2 object, pass user credentials:
db = psycopg2.connect(
host = 192.168.0.1, # example IP, use 'localhost' if local, otherwise the IP of the server where Postgre is located.
database = 'apis_master'
user = 'my_db_user',
password = 'my_db_password'
)
This should solve your connection problem.
The problem was that there were two PGSQL instances running on this system. One was 8.4 and not configured to ident with my username, the other was 9.1 and had my username. Command line psql was automatically picking 9.1 running on a non-standard port while psycopg2 was using the default port. Specifying the port in the psycopg2 connection line fixed the problem.
I am working with MySQL-python package. I am able to execute MySQL dependent scripts from command line, however doing the same through browser (Apache CGI) yields the following error:
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/var/www/html/temp.py in ()
9
10 # Establich a connection
11 db = MySQLdb.connection(host="127.0.0.1", user="root", passwd="", db="inserv")
12
13 # Run a MySQL query from Python and get the result set
db undefined, MySQLdb = <module 'MySQLdb' from
'/usr/lib64/python2.6/site-packages
/MySQLdb/__init__.pyc'>, MySQLdb.connection = <type '_mysql.connection'>,
host undefined, user undefined, passwd undefined
<class '_mysql_exceptions.OperationalError'>:
(2003, "Can't connect to MySQL server on '127.0.0.1' (13)")
args = (2003, "Can't connect to MySQL server on '127.0.0.1' (13)")
message = ''
I have been stuck in this situation for past few days. MySQL commands issued through php based sites execute appropriately and I can, also, login to MySQL from command line. The problem seem to be with Python CGI only.
I have also tried the same with oursql package and there seems to be a similar problem. How can I address this situation?
Edit
As per #Real's answer I have edited my code to use MySQLdb.connect() but the problem still persist and traceback ends with:
2003, "Can't connect to MySQL server on '127.0.0.1' (13)"
You should be using connect, not connection. Mysqldb.connect() returns a connection object but you appear to be calling mysqldb.connection(). See The docs for an example of how to do it.