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.
Related
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?
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!
I am trying to connect to an RDS instance with flask-sqlalchemy. But i cant get it to work. I am getting the error
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1049, "Unknown database 'dbname'")ยจ
My database URI looks like this
SQLALCHEMY_DATABASE_URI = 'mysql://username:password#dbname.xxxxxxxxxxxx.eu-central-1.rds.amazonaws.com:3306/dbname'
I have tried using the pymysql plugin, but it didnt make any difference.
I think my security groups are setup correctly.
On the RDS instance it is this
(I dont know if security groups contain any personnel info)
Try creating the database first:
import pandas as pd
import sqlalchemy
mysql_engine = sqlalchemy.create_engine("mysql://username:password#dbname.xxxxxxxxxxxx.eu-central-1.rds.amazonaws.com:3306")
pd.read_sql_query("create database dbname", con=mysql_engine)
I am currently trying to connect to my MySql database created on AWS with a python program using the library PyMySQL.
import pymysql.cursors
connection = pymysql.connect(host='.blablablablabla.us-east.rds.amazonaws.com',
user='Username',
password='Password',
db='nameofmydb',
)
When I run the above code I get the following error:
pymysql.err.InternalError: (1049, "Unknown database 'nameofmydb'")
What am I doing wrong? The arguments I gave to the function are correct. Could it be a problem with the MySql driver?
Thank you in advance
The message is the standard one, returned by mysql server. It means what it says, it can not find the database. Doesn't look like a connectivity problem or a driver issue: it reaches the server and the server returns a message.
Just make sure you have created the database using the CREATE DATABASE command. Use the mysql CLI client to connect to the database and type SHOW DATABASES to see all the available databases.
I am trying to import to a schema in MySQL workbench but get the following error:
ERROR 2049 (HY000): Connection using old (pre-4.1.1) authentication protocol refused (client option 'secure_auth' enabled)
I had a similar problem trying to connect originally to the schema and solved it by following the fixes in this post but I can't do this import still. I can now access the schema fine, but when I try to do the import it fails with this same error.
Its not a bug, if you use your user account password with old hashing algorithm. you can try with new account with new algorithm password by following step -
set session old_passwords = 'OFF';
create user 'test' IDENTIFIED BY 'TESTPASS';
Now you can connect with this account and try to import. I hope it will help to you.