Flask SQLAlchemy DB connections with MYSQL - python

I am using falsk SQLAlchemy with MYSQL with all default configuration(pool size and timeout)
I don't understand why do I get out of DB connections from the MYSQL DB?
My server is not that heavy loaded.
Can someone please explain how the flask sql alchemy get and release DB connections?
If I have a pool thread of 20 on my apache mod_wsgi server that means by theory that i can have 20 db connection opens all the time and that's it no?
How flask sql alchemy handle close and restore those connections.
Thanks

try this code
import sqlalchemy
dbhost = 'localhost' #host name
dbuser = 'root' #mysql username
dbpass = 'admin' #mysql password
dbname = 'mytable' #database name
engine = sqlalchemy.create_engine('mysql://'+dbuser+':'+dbpass+'#'+dbhost ) # connect to server
DB_URI = 'mysql://' + dbuser + ':' + dbpass + '#' + dbhost + '/' +dbname
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']=DB_URI
db = SQLAlchemy(app)

Related

NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:oracle.oracledb

When I Try to connect oracle server with SQLAlchemy. I'm getting this error.
NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:oracle.oracledb
from sqlalchemy.engine import create_engine
DIALECT = 'oracle'
SQL_DRIVER = 'oracledb'
USERNAME = 'username' #enter your username
PASSWORD = 'password' #enter your password
HOST = 'host url' #enter the oracle db host url
PORT = 1533 # enter the oracle port number
SERVICE = 'service name' # enter the oracle db service name
ENGINE_PATH_WIN_AUTH = DIALECT + '+' + SQL_DRIVER + '://' + USERNAME + ':' + PASSWORD +'#' + HOST + ':' + str(PORT) + '/?service_name=' + SERVICE
engine = create_engine(ENGINE_PATH_WIN_AUTH)
#test query
import pandas as pd
test_df = pd.read_sql_query('SELECT * FROM global_name', engine)
any different method to connect?
For completeness (since the answer is already in comments): with SQLAlchemy 1.4 add this to your top level script file:
import sys
import oracledb
oracledb.version = "8.3.0"
sys.modules["cx_Oracle"] = oracledb
and then proceed as if you were using cx_Oracle. The create_engine() should begin with oracle: like:
# SQLAlchemy 1.4 with python-oracledb or cx_Oracle
engine = create_engine('oracle://...
The sys.modules etc snippet isn't needed for SQLAlchemy 2.0. With this version create_engine() should begin with oracle+oracledb: like:
# SQLAlchemy 2.0 with python-oracledb
engine = create_engine('oracle+oracledb://...
These posts are good references:
Using python-oracledb 1.0 with SQLAlchemy, Pandas, Django and Flask
Using SQLAlchemy 2.0 (development) with python-oracledb for Oracle Database

create new mysql database and tables using sqlalchemy

I'd like to create new database and new tables to this database using sqlalchemy. and I have to write create_engine twice, is there any easier writing method to do these things?
my code is here:
from sqlalchemy import create_engine
import pymysql
user = 'admin'
password = ''
host = 'database-1.czswegfdjhpn.us-east-1.rds.amazonaws.com'
port = 3306
engine = create_engine(url="mysql+pymysql://{0}:{1}#{2}:{3}".format(
user, password, host, port))
conn = engine.connect()
print('connect successfull')
conn.execute("commit")
conn.execute('create database covid_19')
database = 'covid_19'
engine1 = create_engine(url="mysql+pymysql://{0}:{1}#{2}:{3}/{4}".format(
user, password, host, port, database))
conn1 = engine.connect()
print('connect successfull')
df.to_sql(name="covid_19_world_cases_deaths_testing",con=engine1, if_exists='append', index=False, chunksize=200)
Haven’t tested this, however could you simply call
conn.execute(“USE covid_19”)
to change the database of the existing connection?

Flask-MySQL ignore host parameter

I'm trying to write a Flask miniapp to connect with a MySQL database. To connect with the database, I use flask-mysql, calling it as this way
from flask import Flask, render_template, redirect
from flaskext.mysql import MySQL
SECRET_KEY='5f352379324c22463451387a0aec5d2f'
app = Flask(__name__)
app.secret_key = SECRET_KEY
mydb = MySQL(app, prefix="mydb", host='172.17.0.2', user='testusr', password='test', db='testDB')
(...)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
But, when I run the application, it shows this error
pymysql.err.OperationalError: (1044, "Access denied for user 'testusr'#'%' to database 'testDB'")
If I run it from a Docker container, I get a similar error, replacing "%" with "localhost" (in this case, it's not able to connect, due to the database is out of the container)
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 99] Cannot assign requested address)")
172.17.0.1 - - [23/Jun/2021 18:00:29] "GET / HTTP/1.1" 500 -
I tried to initialize flask-mysql using other this way, with the same error
app.config['MYSQL_HOST'] = '172.17.0.2'
app.config['MYSQL_USER'] = 'testusr'
app.config['MYSQL_PASSWORD'] = 'test'
app.config['MYSQL_DB'] = 'testDB'
mydb = MySQL(app)
Where can be the error?
Solved changing the connector from flask-mysql to mysql-connector
import mysql.connector
SECRET_KEY='5f352379324c22463451387a0aec5d2f'
app = Flask(__name__)
app.secret_key = SECRET_KEY
mydb = mysql.connector.connect(
host="172.17.0.2",
user="testusr",
password="test",
database="testDB"
)

How do you authorize ECS to create table in RDS using Python and Postgres?

I'm using psycopg2 to create a db from an ECS instance to a RDS server.
This works on a local db but not for RDS:
def create_table(host_name,database_name,user_name,database_password):
#database_name is the target db - not the 'postgres' db we use for auth
conn = connect(
dbname = 'postgres',
user = user_name,
host = host_name,
password = database_password
)
conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
cursor.execute('CREATE DATABASE ' + '"' + str(database_name) + '"')
Using the same user I can create a db from PGAdmin (CREATE DATABASE test2;) but not in ECS. The ECS error says: psycopg2.errors.InsufficientPrivilege: permission denied to create database. How is it possible that the same user is allowed to created a db from PGAdmin but not in ECS?

How to connect to external MySQL without using models.py in Django

I have my report dashboard that I'm developing using Django and I want to connect it to my MySQL database without using models.py or migrations.
Basically, here's what I've done so far.
views.py
import configparser
config = configparser.ConfigParser()
config.read('db.ini')
def getConnection():
host = config['ord']['host']
port = config['ord']['port']
database = config['ord']['database']
password = config['ord']['password']
user = config['ord']['user']
connection = pymssql.connect(host=host, user=user, password=password, database=database)
cursor = connection.cursor()
print("Connection Succesfull")
return cursor
def fill_weight(request):
try:
sql = u """
SELECT * FROM fill_weight;
"""
sql =sql.encode('utf-8')
cursor.execute(sql)
cursor.fetchall()
db.ini
[oof_ord]
host = localhost
port = 3306
database = xxxxxxxxxxx
user = xxxxxxxxxxx
password = xxxxxxxxxxx
default-character-set = utf8
The reason why I want to do this is because I don't have the official database that I'm going to use for this system and I want to easily access it by putting it's database credentials to my db.ini and access it to show the data without doing any migrations.
Is there anyone that can help me ?

Categories

Resources