How to connect fdb database using Python in Ubuntu? - python

I am trying to connect fdb database using python. I am remotely connected to ubuntu. I have tried this:
path = '/home/ubuntu/Firebird4.0/A.fdb'
con = fdb.connect(host=host, database=path, user=user, password=pswd2, charset='UTF8')
and many combinations for the path of ubuntu fdb file. How can I solve this problem? Is it about the wrong host, port, password, or something?
I got this error all the time
Traceback (most recent call last):
File "/home/ubuntu/test.py", line 24, in <module>
conn = connection()
File "/home/ubuntu/test.py", line 15, in connection
con = fdb.connect(host=host, database=path, user=user, password=pswd2, charset='UTF8')
File "/home/ubuntu/.local/lib/python3.10/site-packages/fdb/fbcore.py", line 869, in connect
raise exception_from_status(DatabaseError, _isc_status,
fdb.fbcore.DatabaseError: ('Error while connecting to database:\n- SQLCODE: -902\n- Unable to complete network request to host "15.206.100.218".\n- Failed to establish a connection.', -902, 335544721)

Related

Can't connect to MySQL server on 'localhost:3306' (10061) During handling of the above exception in python

i wrote a program to check if program can make connection to MySQL server and if cant connect to server print a error message and this is the program that i wrote
import mysql.connector
is_connected = False
def check_connection():
global is_connected
try:
db = mysql.connector.connect(
host="localhost",
user="root",
passwd=""
)
if db.is_connected():
print("Connected to MySQL database")
is_connected = True
except:
print("Unable to connect to MySQL database check if mysql server is running on defaul port which is 3306")
return is_connected
the whole objective here is print a error message when MySQL server is not running so i stop the server and run the file it works fine when in its own file but when it import to another file and call the check_cconnection method its give this error
Traceback (most recent call last):
File "C:\Users\thevi\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 236, in _open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on 'localhost:3306' (10061)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "t:\Projects\Python_Projects\assignment\anatha.py", line 2, in <module>
from create_db import createDb
File "t:\Projects\Python_Projects\assignment\create_db.py", line 3, in <module>
db = mysql.connector.connect(
File "C:\Users\thevi\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\__init__.py", line 272, in connect
return CMySQLConnection(*args, **kwargs)
File "C:\Users\thevi\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 85, in __init__
self.connect(**kwargs)
File "C:\Users\thevi\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\abstracts.py", line 1028, in connect
self._open_connection()
File "C:\Users\thevi\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 241, in _open_connection
raise errors.get_mysql_exception(msg=exc.msg, errno=exc.errno,
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)
how to ger rid of this error

Error connection to postgres database using psycopg2

I am trying to connect to a postgres database to run some queries for validation. Trying to establish connection via psycopg2. Have tried passing actual values for host instead of variables.
Tried the connection using psql to verify the connection parameters, password etc and they are right too.
But with the code below it always errors out with the below error
conn1 = psycopg2.connect(host=db_host,database=db_instance_name,user=masterusername,port=db_port,password=masterpassword)
cur1 = conn1.cursor()
print ('\n************Connection is successful ************ \n')
Traceback (most recent call last):
File "restored_db_details_draft.py", line 50, in <module>
conn_test = psycopg2.connect(host="***",database="my_db_name",user="master",port="9876",password="****")
File "/Users/me/Library/Python/3.8/lib/python/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.

Cannot Connect To RDS Instance Using Python pymysql Library

I am attempting to connect to an RDS instance using Python's pymysql library. Unfortunately I'm getting an exception back.
Here is my Python code:
import pymysql
#Connect
connection = pymysql.connect(host='jdbc:mysql://dbname.url.us-east-1.rds.amazonaws.com/dbname',
port='3306',
user='username',
password='pass',
database='dbname')
Upon executing it, I receive the following exception:
Traceback (most recent call last):
File "/path/to/file/test.py", line 8, in <module>
database='treemigodb')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymysql/connections.py", line 629, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'jdbc:mysql://dbname.url.us-east-1.rds.amazonaws.com/dbname' ([Errno 8] nodename nor servname provided, or not known)")
Here is what I've tried:
I've opened up the security group on the AWS side to traffic. I had previously performed this connection in Java, with success. AWS is configured as it should be.
I've seen in other examples that the host format tends to vary. Therefore, I've tried:
jdbc:mysql://dbname.url.us-east-1.rds.amazonaws.com/dbname
jdbc:mysql://url.us-east-1.rds.amazonaws.com/dbname
jdbc:mysql://dbname.url.us-east-1.rds.amazonaws.com/
jdbc:mysql://url.us-east-1.rds.amazonaws.com/
dbname.url.us-east-1.rds.amazonaws.com/dbname
url.us-east-1.rds.amazonaws.com/dbname
dbname.url.us-east-1.rds.amazonaws.com/
Is there some potential setup that I could be missing? I've double-checked that every other parameter matches.

Unable to connect to remote sql servers using pypyodbc (Python)

I tried to use pypyodbc module to access remote MS SQL database 2014. However, when I run the below script, I got the error message saying:
Traceback (most recent call last):
File "C:\Users\username\Desktop\myscirpt.py", line 159, i
n <module>
db= pypyodbc.connect('DRIVER='+ driver + ';SERVER='+ client_db_ip +';PO
RT=' + port + ';DATABASE=' + database + ';trusted_connection=yes;')
File "C:\Python27\lib\site-packages\pypyodbc.py", line 2454, in __init__
self.connect(connectString, autocommit, ansi, timeout, unicode_results, read
only)
File "C:\Python27\lib\site-packages\pypyodbc.py", line 2507, in connect
check_success(self, ret)
File "C:\Python27\lib\site-packages\pypyodbc.py", line 1009, in check_success
ctrl_err(SQL_HANDLE_DBC, ODBC_obj.dbc_h, ret, ODBC_obj.ansi)
File "C:\Python27\lib\site-packages\pypyodbc.py", line 987, in ctrl_err
raise DatabaseError(state,err_text)
pypyodbc.DatabaseError: (u'08001', u'[08001] [Microsoft][SQL Server Native Clien
t 11.0]TCP Provider: An existing connection was forcibly closed by the remote ho
st.\r\n')
Below it's a very simple code for the db connection. The point here is that I can manually connect to the client db from the same machine, which is the one that my python script resides at. I'm curious what actually cause such issue?
driver = '{SQL Server Native Client 11.0}'
client_db_ip = 'client_db_ip' # client server ip
port = '1433'
database = 'UPG_TEST_MAIN'
db= pypyodbc.connect('DRIVER='+ driver + ';SERVER='+ client_db_ip +';PORT=' + port + ';DATABASE=' + database + ';trusted_connection=yes;')
print db
db.close()

An error with psycopg2 while trying to connect to a remote host server

I'm writing a script that should connect to postgres DB. My code is as follows:
import psycopg2
conn = connect(host="192.168.89.121")
But this is generating the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL: no hay una l�nea en pg_hba.conf para �192.168.89.3�, usuario �fgc�, base de datos �fgc�, SSL inactivo
Why is it asking for an entry for 192.168.89.3 which is my IP?
Do you have access to the remote PostgreSQL server? The error message appears to be trying to tell you that the configuration of that PostgreSQL server isn't allowing you to make an insecure connection. Look at the pg_hba.conf file for the database on that PostgreSQL server. It determines how different machines can connect to it. That's why it's mentioning your IP -- different rules can exist for different IP addresses.

Categories