How to connect MariaDB database on Synology NAS from SQLalchemy in python? - python

I would like to connect to my MariaDB10 database in a Synology NAS using python SQLalchemy. I installed PhpMyAdmin, and created a database named "test", and a random table called "company". I inserted a few rows of dummy data in the table already through the interface. Here is a snapshot of it.
My code is like this:
# Module Imports
import sqlalchemy
import pymysql
from sqlalchemy.ext.declarative import declarative_base
import config_maria_us
# Define the MariaDB engine using MariaDB Connector/Python
user = "username"
passwd = "password"
host = "192.168.1.111"
db = "test"
port= "3307"
engine = sqlalchemy.create_engine(f'mysql+pymysql://{user}:{passwd}#{host}:{port}/{db}')
sql_df = engine.execute("SELECT * FROM company" ).fetchall()
But this returns an error:
OperationalError: (2003, "Can't connect to MySQL server on '192.168.1.111' ([Errno 61] Connection refused)")
Because of this page, so I keep using create_engine("mysql+pymysql:. It says to connect to a MariaDB database, no changes to the database URL are required.
I followed this page, and tried to install mariadb SQLAlchemy by brew install mariadb SQLAlchemy. But it shows a warning Warning: No available formula with the name "sqlalchemy". Did you mean sqlancer?
Then I ofcourse installed MariaDB Connector/C (by following this page) with brew install mariadb-connector-cand installed PyMySQL with pip install PyMySQL. Actually, to start with, i tried to installed mariadb with brew install mariadb, but after loading a pile of things, it shows failure,
Error: Cannot install mariadb because conflicting formulae are installed.
mysql: because mariadb, mysql, and percona install the same binaries
Please `brew unlink mysql` before continuing.
Unlinking removes a formula's symlinks from /opt/homebrew. You can
link the formula again after the install finishes. You can --force this
install, but the build may fail or cause obscure side effects in the
resulting software.
I did not go on installing it, because i don't know how to "relink" MySQL after the unlink.
That's pretty much it, would anyone please tell me what to do? by running the "engine = ..." syntax, it looks like i at least reached my server, but it still fail to connect as '(pymysql.err.OperationalError) (2003, "Can't connect to MySQL server'

OP probably resolved by himself/herself but in case someone else still face the similar issue. In my case, after following steps, I can have access from python script to mariadb hosted in NAS.
Make sure MariaDB turn on TCP/IP Connection
Make sure your username from working machine IP has permission to the database. You can set this up by
GRANT ALL PRIVILEGES ON database_name.* TO 'username'#'localhost';

Related

Connect Python to MySQL DB

Can't connect my code to my MySQL DB due to the authentication plugin
I'm trying to connect my code to a newly installed MySQL DB. I am aware that on MySQL 8.X and above, the authentication type is set to sha2 by default and I've read that I need to "force" a different authentication plug-in (auth_plugin='mysql_native_password').
well... I did it but I still get the same error message:
"Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported
What else should I do?
Thanks
import mysql.connector as Sql
myDB = Sql.connect(
host="localhost",
user="dbadmin",
password="myPassword",
auth_plugin="mysql_native_password"
)
print(myDB)
This indeed solved my issue:
I had the same problem and passing auth_plugin='mysql_native_password' did not work,
because I accidentally installed mysql-connector instead of mysql-connector-python (via
pip3). Just to leaving this here in case it helps someone.
Authentication plugin 'caching_sha2_password' is not supported

Python SQL connection error (2006, 'SSL connection error: SSL_CTX_set_tmp_dh failed')

I used to connect to my AWS-RDS instance this way
import MySQLdb
db = MySQLdb.connect(host=os.getenv('RDS_ENDPOINT'),
user=os.getenv('RDS_USER'),
passwd=os.getenv('RDS_PWD'),
db=os.getenv('RDS_DB'))
or with the help of sqlalchemy, but today it seem to refuse to work with the error (2006, 'SSL connection error: SSL_CTX_set_tmp_dh failed')
I tried to update all the python packages (mysqlclient, sqlalchemy), reinstall mysqlclient-dev, manually reinstall OpenSSL v1.1.1a but still the same error.
[EDIT]
I manage to connect to the same database using the MySQL CLI
mysql --user=$RDS_USER --host=$RDS_ENDPOINT --password=$RDS_PWD $RDS_DB
[SOLUTION]
It seems that this is a driver issue. I tried with mysqlclient for python 3 and got this error. Next I tried with mysql.connector as recommanded but I got encoding issues (as stated in the sqlalchemy doc). Finally, I ended with the pymysql driver which seems to work with sqlalchemy.
Upgrade mysqlclient package fixed the problem.

error when connecting oracle in python using cx_Oracle

I was trying to connect oracle database using python like below.
import cx_Oracle
conn = cx_Oracle.connect('user/password#host:port/database')
I've faced an error when connecting oracle.
DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help.
I've been struggling to figure it out. I used my user name, password, host, port and database('orcl') for example,
'admin/admin#10.10.10.10:1010/orcl'.
Why coudn't it connect?
Ahh, btw I'm running all the code in azure notebooks.
That error indicates that you are missing a 64-bit Oracle client installation or it hasn't been configured correctly. Take a look at the link mentioned in the error message. It will give instructions on how to perform the Oracle client installation and configuration.
[Update on behalf of Anthony: his latest cx_Oracle release doesn't need Oracle Client libraries so you won't see the DPI-1047 error if you upgrade. The driver got renamed to python-oracledb but the API still supports the Python DB API 2.0 specification. See the homepage.]
This seems a problem with version 6.X.This problem didnot appeared in 5.X.But for my case a little workaround worked.I installed in my physical machine and only thing that i need to do was a pc reboot or reopen the terminal as i have added in the path of environment variables.You can try to install in physical machine instead using azure notebooks.
This error come when your Oracle Client is not installed or LD_LIBRARY_PATH is not set where libclntsh.so is present.
if you have Oracle client installed then search for libclntsh.so and set the LD_LIBRARY_PATH as
"export LD_LIBRARY_PATH=/app/bds/parcels/ORACLE_INSTANT_CLIENT/instantclient_11_2:$LD_LIBRARY_PATH"
Here is the full program to connect Oracle using python.
First, you need to install cx_Oracle. to install it fire the below command.
pip install cx_Oracle
import cx_Oracle
def get_databse_coonection():
try:
host='hostName'
port ='portnumber'
serviceName='sid of you database'
user = 'userName'
password = 'password'
dns = cx_Oracle.makedsn(host,port,service_name=serviceName)
con = cx_Oracle.connect(user, password, dns)
cursor = con.cursor()
query ="select * from table"
cursor.execute(query)
for c in cursor:
print(c)
except cx_Oracle.DatabaseError as e:
print("There is a problem with Oracle", e)
finally:
if cursor:
cursor.close()
if con:
con.close()
get_databse_coonection()

Installing X Protocol in order to use mysql shell - Mac

My aim is to connect a MySQL database to a python web scraper I have written.
I have started the process of installing mysql shell, which seems to be the correct version of mysql for my needs. I am following the documentation for downloading X Protocol and am stuck.
Following the instructions to download using MySQL Shell, it says
navigate to the MySQL binaries location (for example, /usr/bin/ on Linux).
Run the following command:
mysqlsh -u user -h localhost --classic --dba enableXProtocol\
Which when I run this command, after being promted to type in my password:
Creating a Classic Session to 'user#localhost'
Enter password:
I then receive this error:
ERROR: 2003 (HY000): Can't connect to MySQL server on 'localhost' (61)
How do I get around this situation?
P.s. I'm not sure if I should be entering 'user' or my actual username 'Frankie'
Error message indicated that you don't have installed MySQL Server.
Firstly, you need to install or upgrade to MySQL 5.7.12 or higher. How to do it is described here https://dev.mysql.com/doc/refman/5.7/en/installing.html

Superset cannot connect to my MSSQL database

After installing Superset (open source software from Airbnb) on my virtual machine (RHEL, Linux 7.2-11), I cannot add my MSSQL database in the configuration page.
Menu->Sources->Databases->Add
In the SQLAlchemy URI field, I entered :
mssql+pymssql://user:password#host:port/database
Obviously, with my user, password, host, port and database name.
But when I click "Test Connection" I have the following error :
ERROR: {"error": "Connection failed!
The error message returned was:
(pymssql.OperationalError) (18456, 'DB-Lib error message 20018, severity 14:\
General SQL Server error: Check messages from the SQL Server\
DB-Lib error message 20002, severity 9:\
Adaptive Server connection failed (host:port)')"}
I already installed the pymssql package and I do not really know where this error could come from.
I tried to follow these tutorials but, maybe I'm doing it wrong:
http://airbnb.io/superset/installation.html
http://airbnb.io/superset/tutorial.html#connecting-to-a-new-database
What worked for me is the following:
mssql+pymssql://user:pass#address.of.db/?charset=utf8
However, I didn't manage to define a database using this syntax, and in turn couldn't define/find the available tables.
I was also having problem to connect with MSSQL. I was on macOS Catalina. I took the following steps, and it worked:
brew install msodbcsql17 mssql-tools (see: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15)
pip install pyodbc
mssql+pyodbc://UserName:Password#HostIP,Port/DBName?driver=ODBC Driver 17 for SQL Server
Mentioning of 'driver=ODBC Driver 17 for SQL Server' in the end is important. (See: https://stackoverflow.com/a/51266453/13150101)
Please try mssql://user:password#host:port/database

Categories

Resources