replica set connectivity to mysql from python - python

I am trying to connect from python code to mysql configured with replica set to enable high availability.
So far I tried it using SqlAlchemy but not able to have multiple instances for mysql.
Python version : 3.6
Mysql verision : 4.1.4
The connection string which I am using so far :
MYSQL_CONNECTION_STRING=mysql+mysqlconnector://%%s:%%s#localhost:3306/my_database
In normal scenario is it getting connected and working fine but through HA configuration its not getting connected.

Related

How do I connect to a MySQL Database in Python with a RaspberryPi

I'm a student and I'm trying to write some sensor values into a MySQL database.
As IDE I'll be using Inteliji.
First off I started by installing the database Plug-in.
This was done successfully
Next I tried to connect to the data base (see figure below)
Figure of successful connection
Now The next thing I want to do is use a MySQL connector.
Therefore I've installed MySQL onto the r-PI and used following code to implement it.
import mysql.connector
print("Step 1")
cnx = mysql.connector.connect(user='a21ib2a01',
password='secret',
host='mysql.studev.groept.be',
database='a21ib2a01')
Print("Step 2")
When now I run my code the terminal will output:
Step1
For some reason I don't know; the connect function always times my program out with the next occurring errors:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'mysql.studev.groept.be:3306' (110 Connection timed out)
So does anyone know why my connection is successful but I can't connect to it?
Long story short what am I doing wrong and how do I fix this?
Thanks in advance!
Your timeout means the network on your rPi cannot reach -- cannot find a route to -- your MySQL host mysql.studev.groept.be.
If you do traceroute mysql.studev.groept.be in a shell in the rPi you may see what's wrong.
When in a shell on your rPi, can you ssh to any machine in your uni's network? If so, you might be able to use ssh port-forwarding to get a route to the database server.
Do you run intelliJ on the rPi directly, or on your laptop? If you run it on the laptop, it looks like the laptop can find a route to your server but the rPi cannot.
(If this were my project, I'd install a MySQL server on my laptop to reduce the network-engineering hassles of connecting through multiple hops involving a VPN.)

Cannot connect to MySQL server Azure from python

I've been trying to connect to a MySQL database remotely but I keep getting the error:
2055: Lost connection to MySQL server at 'local.mysql.database.azure.com:3306', system error: 1 [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1045)
Using the exact same connection details I am able to connect to the database through MySQL Workbench and through Node. For some reason the connection won't go through in python.
This user's as not created to require SSL.
I've been trying to figure out the problem but I'm drawing blanks.
Below is my python login block.
config = {
'host':'local.mysql.database.azure.com',
'user':'user#local',
'password':'password',
'database':'random_db'
}
I've been able to connect to a MySQL instance on the Google Cloud Platform with the exact same connection block but it doesn't work for the Azure connection. I've looked for differences between the two platforms which could have caused this and the only thing I could find was that the Azure MySQL version was 5.7.32log while the GCP was 8.0.18.
I am also currently running python 3.7.0, not sure if that changes anything.
Is there another module I could use if that is the problem, or do I need to downgrade something? I have no idea.
According to this 5.7.29 is the latest 5.7 supported so 5.7.32 would give wrong version error.

Firebird connection on a local database impossible within a Python script

I can connect to my Firebird database using Firebird ISQL Tool (Firebird 3.0.4) with the following command:
connect "C:\Documents\database.db" user 'USER' password 'PASSWORD';
When I want to do it in a Python script (Python v3.7.7 on a Windows10 64 bits), in a virtual environment including fdb v2.0.1 or even firebirdsql v1.1.3, I can't and I systematically get an error.
import fdb
con = fdb.connect(database="C:\Documents\database.db", user='USER' password='PASSWORD'')
DatabaseError: ('Error while connecting to database:\n- SQLCODE:
-902\n- Unable to complete network request to host "xnet://Global\FIREBIRD".', -902, 335544721)
or
con = fdb.connect(host='localhost', database="D:\Documents\database.db", user= 'USER' password= 'PASSWORD'')
DatabaseError: ('Error while connecting to database:\n- SQLCODE:
-902\n- Unable to complete network request to host "localhost".\n- Failed to establish a connection.', -902, 335544721)
or
con = fdb.connect(dsn="localhost:C:\Documents\database.db", user='USER' password='PASSWORD'')
DatabaseError: ('Error while connecting to database:\n- SQLCODE:
-902\n- Unable to complete network request to host "localhost".\n- Failed to establish a connection.', -902, 335544721)
or
import firebirdsql
con = firebirdsql.connect(host='localhost', database="D:\Documents\database.db", user='USER' password='PASSWORD'')
If you have any idea you are welcome as I am stuck.
The error indicates that the fbclient.dll loaded by FDB does not provide Firebird Embedded, and that you don't have Firebird Server running on your machine.
To address this you must either:
Start Firebird Server (by starting its service or running firebird -a)
If you want to use Firebird Embedded instead of Firebird Server, point FDB to a fbclient.dll that provides Firebird Embedded
Point 2 can be done in several ways. In my answer I'm assuming use of Firebird 3 installed in C:\Program Files\Firebird\Firebird-3.0.5.33220-0_x64. Since Firebird 3, a normal Firebird Server install also provides Firebird Embedded. To point to a Firebird Embedded, you can do the following:
Add the Firebird installation directory to the PATH environment variable (make sure it is listed before %SystemRoot\System32/C:\Windows\System32). On a normal Firebird installation, the fbclient.dll without Firebird Embedded is installed in the System32 folder, and if that gets loaded, you can't use Firebird Embedded.
Use fdb.load_api to load the client library:
fdb.load_api('C:/Program Files/Firebird/Firebird-3.0.5.33220-0_x64/fbclient.dll')
This needs to be done before the first use of fdb.connect, otherwise the library found through the normal search path will be used
Specify the client library using the fb_library_name connection property:
con = fdb.connect(dsn='C:/path/to/yourdatabase.fdb', user='sysdba', password='masterkey',
fb_library_name='C:/Program Files/Firebird/Firebird-3.0.5.33220-0_x64/fbclient.dll')
This property needs to be specified on the first connection made using FDB. Although the existence of the property would suggest this is 'per connection', FDB will always use the first client library loaded (in essence, it works as if you called load_api just before the connect).
If you're using Firebird 2.5 or earlier, you will need to download the specific Firebird 2.5 Embedded package, and point to its fbembed.dll instead of fbclient.dll. For Firebird 2.5 Embedded, adding its location to the path will not work unless you rename or copy its fbembed.dll to fbclient.dll.

issue connecting DB2 database through python IBM_DB api

I'm trying to connect to a database using ibm_db api with python but its throwing this error:
SystemError: returned NULL without setting
an error
I've used the same exact script on another machine and it works fine. So it seems like some issue with this machine. I'm using python 3.7 on this machine and python 3.6 on the other one.
Here is the code:
from ibm_db import connect
connection = connect('DATABASE=DB2test;'
'HOSTNAME=localhost;'
'PORT=50000;'
'PROTOCOL=TCPIP;'
'UID=etl;'
'PWD=123!#123ETLLET;', '', '')
By the way, I'm able to connect the same database using DBeaver.

Install MYSQLdb python module without MYSQL local install

I'm trying to install MYSQLdb on a windows client. The goal is, from the Windows client, run a python script that connects to a MySQL server on a LINUX client. Looking at the setup code (and based on the errors I am getting when I try to run setup.py for mysqldb, it appears that I have to have my own version of MySQL on the windows box. Is there a way (perhaps another module) that will let me accomplish this? I need to have people on multiple boxes run a script that will interact with a MySQL database on a central server.
you could use a pure python implementation of the mysql client like
pymysql
(can be used as a dropin-replacement for MySQLdb by calling pymysql.install_as_MySQLdb())
MySql-Connector
You don't need the entire MySQL database server, only the MySQL client libraries.
It's been a long time since I wrote python db code for windows...but I think something like this should still work.
If you're running the client only on windows machines, install the pywin32 package. This should have an odbc module in it.
Using the windows control / management tools, create an odbc entry for either the user or the system. In that entry, you'll give the connection parameter set a unique name, then select the driver (in this case MySQL), and populate the connection parameters (e.g. host name, etc.) See PyWin32 Documentation for some notes on the odbc module in pywin32.
Also, see this post: Common ways to connect to odbc from python on windows.

Categories

Resources