Sqlalchemy psycopg2 set socket options - sqlalchemy

I'd like to try setting the socket options for a psycopg2 postgres database connection to do abortive connection releases. We are facing an issue similar to that described here: https://www.box.com/blog/container-networking-mystery-missing-rsts/.
There's an example of setting socket options for a MySQL connection here: https://github.com/mozilla/mozpool/blob/master/mozpool/db/pool.py, but I can't seem to access the socket with a psycopg2 connection.
Is it possible to set socket options using a psycopg2 connection?

You can use connection.fileno to get the file number of the socket and do whatever you want with it.

Related

Open connection to remote MySQL using non default (unix) socket

I'm in a situation where I have to push data to a remote database, from a Powershell script.
The database's server uses multiple instances, each one using a specific socket.
I'm trying to open a connection remotely, using the .Net MySQL connector as follow :
$mdir = Split-Path $script:MyInvocation.MyCommand.Path
[void][system.reflection.Assembly]::LoadFrom("$mdir\MySQL.Data.dll")
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = "host=$myHost;port=$Port;uid=$User;pwd=$Pass;database=$Database;Socket=$Socket;Pooling=False"
$connection.Open()
I'm getting an error saying that Socket is not an existing keyword.
I've found reference to that keyword on google, but indeed not in the official documentation.
Example showing what I want to connect to :
Server : db.domain.com
Port : 2345
Socket : /var/sockets/mycustomsocket.sock
Is there a way to do what I want, and if so, what am I doing wrong ?
EDIT
What I was doing wrong is putting too much trust into the technical knowledge of my client, but at least, I learned something !
The case I described is pure nonsense due to the way MySQL works.
When an instance uses a socket, that socket is bound to a specific port. The case that my client described to me that lead to that question is therefore impossible.
It is not possible to have several instances of MySQL listening on the same port, but using different sockets. Wanting to connect to a remote file socket is therefore a non issue, and you should be able to connect using ip/hostname and port.
The Socket parameter in MySQL is supposed to be used locally, as it was noted in the comments.

Flask-SQLAlchemy and Gevent not closing mysql connections

I am currently using Flask-uWSGI-Websockets to provide websocket functionality for my application. I use Flask-SQLAlchemy to connect to my MySQL database.
Flask-uWSGI-Websockets uses gevent to manage websocket connections.
The problem I am currently having is that when a websocket connection is ended, the database connection set up by Flask-SQLAlchemy will keep on living.
I have tried calling db.session.close() and db.engine.dispose() after every websocket connection, but this had no effect.
Calling gevent.monkey.patch_all() at the beginning of my app does not make a difference.
A simple representation of what I am doing is this:
from gevent.monkey import patch_all
patch_all()
from flask import Flask
from flask_uwsgi_websocket import GeventWebSocket
from flask_sqlalchemy import SQLAlchemy
app = Flask()
ws = GeventWebSocket()
db = SQLAlchemy()
db.init_app(app)
ws.init_app(app)
#ws.route('/ws')
def websocket(client):
""" handle messages """
while client.connected is True:
msg = client.recv()
# do some db stuff with the message
# The following part is executed when the connection is broken,
# i tried this for removing the connection, but the actual
# connection will stay open (i can see this on the mysql server).
db.session.close()
db.engine.dispose()
I have same situation. and solution for me located in mysql configuration file my.cnf:
[mysqld]
interactive_timeout=180
wait_timeout=180
you must restart mysql service after save my.cnf.
if you don't want to restart mysql service you can use sql queries:
SET GLOBAL interactive_timeout = 180;
SET GLOBAL wait_timeout = 180;
See also wait_timeout and interactive_timeout on mysql.com

Is it possible to connect MySQL Workbench to H2 in memory database?

I am running an H2 in memory database with following connection url:
"jdbc:h2:mem:my_database;DB_CLOSE_DELAY=-1;MODE=MySQL"
H2 allows to start servers to be able to establish alternative connections to the in memory database:
sysLog.info("Creating web server.");
String[] webServerSettings = new String[]{"-webPort","8085","-webAllowOthers"};
webServer = Server.createWebServer(webServerSettings);
webServer.start();
sysLog.info("Creating tcp server.");
String[] tcpServerSettings = new String[]{"-tcpPort","9095","-tcpAllowOthers"};
tcpServer = Server.createTcpServer(tcpServerSettings);
tcpServer.start();
http://www.h2database.com/javadoc/org/h2/tools/Server.html
After creating a server I am able to show a web console and browse the database content. However, I would prefer to use MySQL Workbench for that purpose and I did not yet succeed to connect MySQL Workbench to such an H2 server.
If I use the connection settings localhost:8085 or localhost:9095 in MySQL Workbench following error occurs:
Lost connection to MySQL server at 'waiting for initial communication packet', system error:10060
My Questions:
Is it possible to use MySQL Workbench (or Navicat for MySQL) to connecto to an H2 database at all? If yes, what are the right settings?
(Alternatives to MySQL Workbench are given here: Frontend tool to manage H2 database)
No, H2 implements PostgreSQL protocols, as stated at the ODBC Driver section of the documentation.

OperationalError Lost connection to MySQL server at 'reading initial communication packet' DigitalOcean

Im having trouble connecting to my sql server on digital ocean through my flask webapp. Im using flask-sqlalchemy to bind the mysql database to Flask.
Im able to access the mysql server through the phpmyadmin interface at myipaddress:5000/phpmyadmin
Since Im using Nginx (I bound it to port 80). Bound Apache to Port 5000. So my phpmyadmin interface is accessible at
myipaddress:5000/phpmyadmin
In my flask app, i specify the
SQLALCHEMY_DATABASE_URI ='mysql://root:password#myipaddress:5000/databasename'
when i try to create the tables on my database using the shell with db.create_all() - it just doesnt respond. The cursor blinks forever and then i get the operational error that i quote on the title afte a few minutes
Im able to get the same setup running on my local dev machine. So i know its not a flask configuration problem but just a mysql access issue. I have my webapp up on digitalocean (Not sure if mysql server is behind a firewall or something like that making it inaccessible
On the
/etc/mysql/my.cnf
for the bind-address under mysql_d section, i tried all possible combinations and restarted the mysql server with no success
i tried localhost, 127.0.0.1, 127.0.0.1:5000, myipaddress for the bind-address (Also tried commenting it out) without any results.
Also i tried to get the user, current_user on the table properties from the mysql command line, it's listed as root#localhost for both
From this post:Lost connection to MySQL server at 'reading initial communication packet', system error: 0; i get the idea that its related to firewall but not sure how to rectify this.
Anyidea how can i connect my flask app to the mysql server ? Any help would be much appreciated
I was specifying the mysql address as
mysql://root:password#myipaddress:5000/databasename
But since my flask app and the mysql server are running on the same server, the flask app was be able to access the mysql server when i replaced the myipaddress:5000 with localhost:5000
mysql://root:password#localhost:5000/databasename
The phpmyadmin is just a web interface for your database, if you're looking at connecting your Flask application to your MySql database, you need to point the SQLALCHEMY_DATABASE_URI to the actual MySQL database, rather then the web interface to that database.
Usually MySql runs on port 3306. I believe that SQLAlchemy is clever enough to know that's the default, so your connection string should just be: SQLALCHEMY_DATABASE_URI ='mysql://root:password#localhost/databasename' or if you are running on a different port/external IP address: SQLALCHEMY_DATABASE_URI ='mysql://root:password#myipaddress:4444/databasename'. Remember, if you're connecting to a MySQL database on an external server (not the same one as your Flask app is running on) you will have to change the configuration to allow that kind of access.

OperationalError, Django Cannot Connect to MySQL Database

I am working a Django tutorial and I want to connect my project to a MySQL database. I did everything as told by the djangoproject tutorial to connect to database. When running python manage.py syncdb , however, I got the
following error:
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
What seems problematic? I have XAMMP downloaded and I have created databases through XAMMP before but I don't know how
that affects the connection with Django.
Thanks.
Actually under the settings.py, I set HOST to "127.0.0.1" and PORT to 3306, filled out the rest of DATABASE
portion and it worked!
If you are using Windows 7, you might run into problems if your MySQL is configured to use named pipes. Set your MySQL config to use TCP instead.
 Once your reconfigured MySQL allows local network connections, your MySQLdb should work over TCP.  
It doesn't seem to be able to use
Windows 7 named pipes, although the "mysql" command line client can.
There may be a bug.  This wouldn't be noticed unless MySQL
was configured without network connections, which is rare.  I set
up MySQL that way on a development machine with a local MySQL instance.
A detailed article here - http://interconnectit.com/764/using-mysql-workbench-with-xampp/ - shows you the screen where you can switch your Connection Method from named pipes (Local Socket/Pipes) to TCP/IP via Xampp.
Alternatively, if using named pipes (windows) or sockets (linux/mac) for your python/django app to connect to your MySQL is important to you and you do not want to change your connection method to TCP/IP, you should try setting the servername to "." in your xampp's my.conf configuration file; instead of "localhost" or "127.0.0.1". Using pipes/sockets bypasses the TCP/IP network protocol, giving slightly better performance.