How to read a table from MySQL work bench in colab - mysql

I have referred various articles in stackoverflow and external sources but somehow unable to get answer for this. I want to read a table from MySQL workbench database into a dataframe in colab.
1st Method
In this method, first line of code is successfully executed.
Note: I have hidden database, table and password name for security reasons.
Source -
USER = 'root'
PASSWORD = 'PASSWORD'
DATABASE = 'DATABASE'
TABLE = 'TABLE'
connection_string = f'mysql+pymysql://root:PASSWORD/DATABASE'
engine = sqlalchemy.create_engine(connection_string)
I am getting error for second line of code. Is it because my password ends with #786 or some other reasons.
query = f"SELECT * FROM DATABASE.TABLE"
import pandas as pd
df = pd.read_sql_query(query, engine)
**OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '786#3306' ([Errno -2] Name or service not known)") (Background on this error at: https://sqlalche.me/e/14/e3q8)
**
2nd Method
In this method, first line of code is successfully executed.
Note: I have hidden database, table and password name for security reasons.
connection_string = 'mysql+pymysql://root:PASSWORD#3306/DATABASE'
connect_args = {'ssl': {'ca': '/content/rds-ca-2015-root.pem'}}
db = create_engine(connection_string, connect_args=connect_args)
I am getting error for second line of code.
query = """SELECT * FROM DATABASE.TABLE"""
events_df = pd.read_sql(query, con=db)
**FileNotFoundError: [Errno 2] No such file or directory **
My Queries:
1) Why I am getting error for 2nd line of code in both methods?
2) Is there any workaround or alternative approach / codes where I can successfully connect colab with MySQL database and read table?

Related

How to fix "2013.Lost connection to mysql server during query"

At the moment I am working with MySql database using PyMySql(python).
So, when I am sending many requests to my DB, I face the error:
Error Code: 2013. Lost connection to MySQL server during query
If I am trying to get the Information again I face this error:
InterfaceError (0, '')
Here is my code:
with connection.cursor() as cur:
userId = message.from_user.id
cur.execute(f"INSERT INTO UserInfo(UserId, Favourites) VALUES ({message.from_user.id}, 'None');")
connection.commit()
cur.close()
I had already tried changing settings in MySqlWorkbench following those advises. My settings now.
And my code is like in this question.
So, What should I do to deal with this problem?

Extract Data from a MySQL Database View

I am trying to access data from a View in a MySQL database using RStudio. When I connect to the database, it shows the names of all Views in the Connections window. I can even return the names of every View using the dbListTables() function.
But, when I try to run the tbl() function, I get the following error:
Error: nanodbc/nanodbc.cpp:1655: HY000: [MySQL][ODBC 8.0(a) Driver][mysqld-5.5.5-10.3.34-
MariaDB-log]Prepared statement needs to be re-prepared
<SQL> 'SELECT *
FROM `database_view` AS `q01`
WHERE (0 = 1)'
Here are the packages I have loaded:
library(tidyverse)
library(dbplyr)
library(DBI)
library(odbc)
Here is my tbl() code:
tbl(con, "database_view")
Here is my connection code (I replaced the actual values with placeholders in brackets):
con <- DBI::dbConnect(odbc::odbc(),
Driver = "MySQL ODBC 8.0 ANSI Driver",
Server = "[Server]",
UID = "[UID]",
PWD = "[PWD]",
Port = 3306,
Database = "[Database]")
Any help would be much appreciated!

Google Cloud Function with Cloud MySQL Authorisation fails ([Errno 111] Connection refused)

I'm trying to connect to my Google Cloud MySQL database through a Google Cloud Function to read some data. The function build succeeds, but when executed only this is displayed:
Error: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)") (Background on this error at: http://sqlalche.me/e/e3q8)
Here is my connection code:
import sqlalchemy
# Depending on which database you are using, you'll set some variables differently.
# In this code we are inserting only one field with one value.
# Feel free to change the insert statement as needed for your own table's requirements.
# Uncomment and set the following variables depending on your specific instance and database:
connection_name = "single-router-309308:europe-west4:supermarkt-database"
db_name = "supermarkt-database"
db_user = "hidden"
db_password = "hidden"
# If your database is MySQL, uncomment the following two lines:
driver_name = 'mysql+pymysql'
query_string = dict({"unix_socket": "/cloudsql/{}".format(connection_name)})
# If the type of your table_field value is a string, surround it with double quotes. < SO note: I didn't really understand this line. Is this the problem?
def insert(request):
request_json = request.get_json()
stmt = sqlalchemy.text('INSERT INTO products VALUES ("Testid", "testname", "storename", "testbrand", "4.20", "1kg", "super lekker super mooi", "none")')
db = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL(
drivername=driver_name,
username=db_user,
password=db_password,
database=db_name,
query=query_string,
),
pool_size=5,
max_overflow=2,
pool_timeout=30,
pool_recycle=1800
)
try:
with db.connect() as conn:
conn.execute(stmt)
except Exception as e:
return 'Error: {}'.format(str(e))
return 'ok'
I got it mostly from following this tutorial: https://codelabs.developers.google.com/codelabs/connecting-to-cloud-sql-with-cloud-functions#0 . I'm also using Python 3.7, as used in the tutorial.
SQLAlchemy describes it as not necessarily under the control of the programmer.
For context, the account through which is being connected has the SQL Cloud Admin role, and the Cloud SQL Admin API is enabled. Thanks in advance for the help!
PS: I did find this answer: Connecting to Cloud SQL from Google Cloud Function using Python and SQLAlchemy but have no idea where the settings for Firewall with SQL can be found. I didn't find them in SQL > Connection / Overview or Firewall.
Alright so I figured it out! In Edit Function > Runtime, Build and Connection Settings, head over to Connection Settings and make sure "Only route requests to private IPs through the VPC connector" is enabled. The VPC connector requires different authorization.
Also, apparently I needed my TABLE name, not my DATABASE name as the variable DB_NAME. Thanks #guillaume blaquiere for your assistance!

How to connect a mysql server to grafana?

I'm new on grafana and I need to connect my database (mysql) to it.
I tried to configure the custom.ini file as follow,
I added the next section on database
#################################### Database
####################################
type = mysql
host = localhost:3306
name = grafana (db on my sql server)
user = grafana (user on my sql server)
#### If the password contains # or ; you have to wrap it with triple quotes.
Ex “”"#password;"""
password =***** (password of my sql user)
and the next section on session
#################################### Session ####################################
[session]
provider = mysql (at the beginning)
provider_config = grafana:*******#tcp(localhost:3306)/grafana
provider = mysql (at the end)
When I try to connect to the serve I get this error : “Service init failed: Migration failed err: this authentication plugin is not supported”
I’m new on grafana and I don’t know if I have to set more thinks in the custom.ini
If someone can figure out what is wrong here I will be the happiest man on the world :D
Thanks for the time you spent reading this and a big big thanks to the savior !!
I finally find the problem. The fact is that mysql 8 has some permission faeture which brings a lot of issue for connecting to grafana or read files. So I install mysql 5.7 instead and all was fine !

PythonAnywhere error trying to access MySQL database (Access denied)

I have setup an environment in PythonAnywhere with a simple database (new_db), following all the guides and tutorials.
I have a simple python Flask app which is trying to connect to the database using the following code:
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["DEBUG"] = True
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}#{hostname}/{databasename}".format(
username="WazzalJohn",
password="my_password_here",
hostname="WazzalJohn.mysql.pythonanywhere-services.com",
databasename="WazzalJohn$new_db>",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
db = SQLAlchemy(app)
But i keep getting the error:
ProgrammingError: (mysql.connector.errors.ProgrammingError) 1044 (42000): Access denied for user 'WazzalJohn'#'%' to database 'WazzalJohn$new_db>'
I have tried creating a new database but get the same problem. I thought PythonAnywhere handled all these types of issues so don't know what to do... has anyone had the same problem specifically on PythonAnywhere?
I have now tried to log in through a BASH console and got similar message - screen shot is here
In your original code, you have an error in the database name:
databasename="WazzalJohn$new_db>",
...should be:
databasename="WazzalJohn$new_db",
...without the extra ">".
The problem in the screenshot is that you're not specifying a database on the command line; you need to do something like:
mysql -u TWarren -h TWarren.mysql.pythonanywhere-services.com -p 'TWarren$default'
...where 'TWarren$default' should be replaced with the actual database name. The single quotes around the database name are important, BTW -- if you don't use them, Bash will interpret "$default" as an environment variable and mysql will try to connect to the wrong database.