How to enable detailed database connection logging? - python

Our DBA is trying to migrate a Django application's database to a new backend host running Oracle 12. When I put that host's info in the Django settings.py file, I get this error:
DatabaseError: ORA-28547: connection to server failed, probable Oracle Net admin error
The DBA has asked for my help in solving this problem. Is there a way to turn on detailed logging in Django while establishing a database connection? I've seen directions for enabling logging of database queries, but we're not getting that far -- our error is happening sometime during the connection.

Basically you can use a Wireshark or any sniffing tool to check what is the connection string passed to the DB.
But this oracle error will occur when there is an error after the initial handshake to the Oracle DB and further establishing a connection.
Check you oracle_cx for python and the oracle instant client whether the insta client is of the correct version to the DB. Try a connection form instantclient directly to the DB using sqlplus.
Reference:
django docs
DBA forum
django community

Related

PYODBC + MS SQL SERVER connection with Encrypt=yes not connecting

We have a python flask app running on an aws centos ECS instance. We are trying to establish an encrypted connection to our database via PYODBC with odbc 17 on Linux. When running locally we just use the SQL server driver. Currently we have the code:
params = urllib.parse.quote_plus(driver;server;user;pwd;...;Encrypt=yes)
SQLALCHEMY_DATABASE_URI="mssql+PYODBC:///?odbc_connect=%s" %params
We have tls enabled on the server. The connection works locally on windows but not deployed in Linux.
Currently doing a deployment with 'yes' instead of 'true'. We are also about to try with 'trustedserverconnection=yes'. Any insight on this process would be greatly appreciated!
Update: latest error, invalid connection string attribute 'trustservercertificate'
We ended up implementing a second connection param:
TrustServerCertificate=YES
Which is not ideal, obviously, because we want to have good security implementation practices. In future state we will need to set this to false and put our ssl pem file in the Linux ssl store.
Hope this helps someone. Had some issues finding documentation for pyodbc with MS SQL Server.
According to this documentation, pyodbc passes the connection string through to the underlying ODBC driver. Microsoft's
article Using Connection String Keywords with SQL Server Native Client
documents both the Encrypt and TrustServerCertificate attributes. The TrustServerCertificate setting should generally be avoided in production databases; however, it is very useful when testing encrypted connections to a development database that is using a self-signed certificate. For example, the default installation of SQL Server uses a self-signed certificate and will require this setting.
In my mssql+pyodbc connection strings I just append ?Encrypt=yes&TrustServerCertificate=yes as appropriate. Please note, if you already have another setting after a question mark ? then use & instead of ?, for example: ?Trusted_Connection=yes&Encrypt=yes&TrustServerCertificate=yes

App engine MySQL connection to remote db failing

I've got an app engine running and I'm struggling to get the MySQL to connect when it has been deployed. It connects fine on my pc running the dev server, but as soon as I deploy I get this error:
OperationalError: (2004, "Can't create TCP/IP socket (-1)")
Could this be because it is not a cloud sql database ? I've fiddled with a few things like firewall rules and dns things but I honestly just don't know where to even start solving this issue. Some research indicated it might be a TCP/IP vs Unix socket issue which does also kind of make sense as I've got another connection to a cloud sql instance which works fine (using a unix socket). It is a python app, any help is appreciated
You need to enable billing for your project to use socket connections in appengine..

How can uwsgi re-establish connection to remote mysql database whenever mysql services are restarted

I have a python web application, where in, the application connects to a remote database.
Application: flask+uwsgi+nginx .
Database :mysql (remote).
The application exposes rest api for which data is served from remote database.
Everyday after db restoration, mysql service is restarted in the remote database. The connection between my application and remote database breaks, and it starts throwing error message
MySQL server has gone away.
until I manually restart the uwsgi service in my application
sudo service uwsgi restart
The duration between mysql service restart in remote db and uwsgi service restart in my system is the downtime.
Can my application re establish connection as soon as the mysql services are restarted ?
Please suggest any solutions?
It really depends on the way you connect to your database
In case you're using the popular ORMs:
SQLAlchemy ORM
engine = create_engine('mysql+mysqldb://...', pool_recycle=3600)
as stated in the documentation here
Peewee
#app.before_request
def _db_connect():
database.connect()
#app.teardown_request
def _db_close(exc):
if not database.is_closed():
database.close()
as stated in the documentation here
The issue is explained in depth in the mysql documentation here

Django syncdb error for blog tables

i'm following this tutorial: http://www.programmersbook.com/page/21/Django-Beginner-Tutorial-Part-I/
and I added the database details (running django.db.backends.postgresql_psycopg2) and i added the template dirs. But when i do
./manage.py syncdb
i get:
self.connection = Database.connect(**conn_params)
psycopg2.OperationalError: could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Any suggestions on how i can fix this please?
Before connecting to database you need to install database server, and configure it for any user to access.
If you want just follow tutorial use SQLite -it's most simple to configure.
Otherwise, install database server of your choose, create database, configure access, make sure that connection details are correct, and/or DB server is up and running.
Best way is try to connect to server via command line.

python mysql operationalerror

I have a Python script that processes some data and then inserts it into mysql using MySQLdb.
When I run the script on my local server all is well. When I run it on our server I straightaway get
OperationalError: (2006, 'MySQL server has gone away')
Any ideas?
2006, 'MySQL server has gone away' means time out. Most times when it happend to me when I try to connet to external mysql server and this server was not set up to accept external connections (from your localhost). Straightaway response would confirm it.
Check the logs of your MySQL installation and see if it is really gone and for whatever reason. Apart from that check your network connectivity and the network configuration of mysqld (it must listen on the related external network addresses). Not a Python issue.
changed hosting provider and problem went away. Must have been configured poorly.

Categories