DAL connection string for Web2Py to MySQL on TurnkeyLinux - mysql

On TurnkeyLinux I have tried to connect a Web2Py app to the MySQL instance (which is up and running) with the following DAL statement (in db.py):
db = DAL('mysql://root:pwd2sql#web2py/dbname', fake_migrate_all=True)
It doesn't work and throws the following ticket:
Failure to connect, tried 5 times: Traceback (most recent call last): File "/var/www/web2py/gluon/dal.py", line 7562, in init self.adapter = ADAPTERSself._dbname File "/var/www/web2py/gluon/dal.py", line 2572, in __init_ if do_connect: self.reconnect() File "/var/www/web2py/gluon/dal.py", line 606, in reconnect self.connection = f() File "/var/www/web2py/gluon/dal.py", line 2570, in connector return self.driver.connect(*driver_args) File "/usr/lib/python2.7/dist-packages/MySQLdb/init.py", line 81, in Connect return Connection(args, *kwargs) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in init super(Connection, self).init(args, **kwargs2) OperationalError: (2003, "Can't connect to MySQL server on 'web2py' (111)")
Does anybody have a connection string sample I can use ?

Related

peewee.OperationalError - MySQL connection refused

I'm using peewee for a Python ORM to a MySQL database and Flask to serve a web site from my local desktop running Ubuntu. I can connect to the database from the command line (mysql test -u user -p), but am getting a connection refused error when trying from Python. This code has worked on my previous Ubuntu PC, and I dumped the database from there, imported into the new PC and granted permissions, so I'm really not sure why it's giving me this.
Model.py:
from peewee import MySQLDatabase, Model, AutoField, CharField, IntegerField, \
ForeignKeyField, BooleanField, TextField, DateTimeField
from flask_security import UserMixin, RoleMixin
class BaseModel(Model):
"""
Base model for the model classes; prevents having to define Meta class with the
database for each Model class. Must call BaseModel._meta.database.init() before
connecting - should be able to just call once on BaseModel before
using derived model classes.
"""
class Meta:
database = MySQLDatabase(None) #init before calling connect()
#Additional code to define various tables
Trying to connect in Python command line:
import Model as mmm
db = mmm.BaseModel._meta.database
db.init('test', host = 'localhost', user = 'mike', passwd = password, port = 3306)
db.connect()
Error stack trace:
Traceback (most recent call last):
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/connections.py", line 569, in connect
sock = socket.create_connection(
File "/usr/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 3035, in connect
self._state.set_connection(self._connect())
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 3933, in _connect
conn = mysql.connect(db=self.database, **self.connect_params)
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/connections.py", line 619, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'aspire' ([Errno 111] Connection refused)")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 3038, in connect
self._initialize_connection(self._state.conn)
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 2873, in __exit__
reraise(new_type, new_type(exc_value, *exc_args), traceback)
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 183, in reraise
raise value.with_traceback(tb)
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 3035, in connect
self._state.set_connection(self._connect())
File "/home/mike/.local/lib/python3.8/site-packages/peewee.py", line 3933, in _connect
conn = mysql.connect(db=self.database, **self.connect_params)
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/home/mike/.local/lib/python3.8/site-packages/pymysql/connections.py", line 619, in connect
raise exc
peewee.OperationalError: (2003, "Can't connect to MySQL server on 'aspire' ([Errno 111] Connection refused)")

Cannot connect to MySQL database in TensorFlow Extended

I am having problems using a MySQL database as a metadata database in a TensorFlow Extended pipeline.
I used the penguin template to set up a very simple pipeline and also set up a MySQL database locally.
The only thing I changed in the code was using
tfx.orchestration.metadata.mysql_metadata_connection_config as the metadata_connection_config input to the pipeline instead of tfx.orchestration.metadata.sqlite_metadata_connection_config
from tfx import v1 as tfx
# [...] Add a simple CsvExampleGen
tfx.dsl.Pipeline(
pipeline_name=pipeline_name,
pipeline_root=pipeline_root,
components=components,
metadata_connection_config=tfx.orchestration.metadata
.mysql_metadata_connection_config(
host="localhost",
port=3306,
database="ml_metadata",
username="root",
password="password"),
beam_pipeline_args=beam_pipeline_args,
)
Running this code results in the following error message:
[2021-11-17 12:02:16,948] {taskinstance.py:1270} INFO - Marking task as FAILED. dag_id=penguin_pipeline, task_id=CsvExampleGen, execution_date=20211117T110208, start_date=20211117T110212, end_date=20211117T110216
[2021-11-17 12:02:16,966] {standard_task_runner.py:88} ERROR - Failed to execute job 101 for task CsvExampleGen
Traceback (most recent call last):
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/task/task_runner/standard_task_runner.py", line 85, in _start_by_fork
args.func(args, dag=self.dag)
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/cli/cli_parser.py", line 48, in command
return func(*args, **kwargs)
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/utils/cli.py", line 92, in wrapper
return f(*args, **kwargs)
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/cli/commands/task_command.py", line 292, in task_run
_run_task_by_selected_method(args, dag, ti)
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/cli/commands/task_command.py", line 107, in _run_task_by_selected_method
_run_raw_task(args, ti)
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/cli/commands/task_command.py", line 180, in _run_raw_task
ti._run_raw_task(
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/utils/session.py", line 70, in wrapper
return func(*args, session=session, **kwargs)
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1332, in _run_raw_task
self._execute_task_with_callbacks(context)
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1458, in _execute_task_with_callbacks
result = self._execute_task(context, self.task)
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1514, in _execute_task
result = execute_callable(context=context)
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/operators/python.py", line 151, in execute
return_value = self.execute_callable()
File "/home/user/anaconda3/lib/python3.8/site-packages/airflow/operators/python.py", line 162, in execute_callable
return self.python_callable(*self.op_args, **self.op_kwargs)
File "/home/user/anaconda3/lib/python3.8/site-packages/tfx/orchestration/airflow/airflow_component.py", line 76, in _airflow_component_launcher
launcher.launch()
File "/home/user/anaconda3/lib/python3.8/site-packages/tfx/orchestration/launcher/base_component_launcher.py", line 191, in launch
execution_decision = self._run_driver(self._input_dict, self._output_dict,
File "/home/user/anaconda3/lib/python3.8/site-packages/tfx/orchestration/launcher/base_component_launcher.py", line 152, in _run_driver
with self._metadata_connection as m:
File "/home/user/anaconda3/lib/python3.8/site-packages/tfx/orchestration/metadata.py", line 152, in __enter__
raise RuntimeError(
RuntimeError: Failed to establish connection to Metadata storage with error: mysql_real_connect failed: errno: , error:
In this log I was using AirFlow, but the same exception message is shown if I run a LocalDagRunner:
Exception has occurred: RuntimeError
Failed to establish connection to Metadata storage with error: mysql_real_connect failed: errno: , error:
I have tried changing the host to "127.0.0.1", but this didn't change anything. Anyone had a similar issue or maybe see an apparent error in my approach?
The MySQL server is version 8.0 and I am using TensorFlow Extended version 1.4.0

bash script gives different results

I have a bash script that starts some python code, running on a RasPi under latest raspbian. It runs fine if I run it manually with sudo, but when it auto runs at boot I get a mysql error. The script is called from a line in /etc/rc.local
The script is
#!/bin/bash
/home/control/solar/v10_1/control.py >> '/home/control/solar/logs/control_X.X_start.log' 2>&1 &
echo "control started" >> '/home/control/solar/logs/control_X.X_start.log'
echo "UID is $UID , EUID is $EUID" >> '/home/control/solar/logs/control_X.X_start.log'
The output to the log after bootup is
UID is 0 , EUID is 0
Traceback (most recent call last):
File "/home/control/solar/v10_1/control.py", line 42, in <module>
import variables # global variables
File "/home/control/solar/v10_1/variables.py", line 13, in <module>
gv.db = MySQLdb.connect("localhost", "solar", "solar", db='solar') # database for logging
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
I have made all the paths absolute, the UID is 0 in both cases, what can cause the difference in behaviour?

versatile database connection with django and mysql

I have an issue with my deployed application on an apache2 with mod_wsgi.
The connection process to the database is exactly successful only once out of 2!
I've named the database 'CKP' and for some reason have to change to 'ckp'.
So when I request a page of the application once out of two, I can read in the error log of apache:
mod_wsgi (pid=16098): Target WSGI script '/u1/msc/vg55/public_html/core_knowledge_web_platform/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=16098): Exception occurred processing WSGI script '/u1/msc/vg55/public_html/core_knowledge_web_platform/wsgi.py'.
Traceback (most recent call last):
File "/u1/msc/vg55/public_html/core_knowledge_web_platform/wsgi.py", line 20, in <module>
command.validate()
File "/usr/lib/python2.6/site-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "/usr/lib/python2.6/site-packages/django/core/management/validation.py", line 102, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 338, in get_server_version
self.cursor()
File "/usr/lib/python2.6/site-packages/django/db/backends/__init__.py", line 250, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 322, in _cursor
self.connection = Database.connect(**kwargs)
File "/usr/lib64/python2.6/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (1044, "Access denied for user 'ckpuser'#'%' to database 'CKP'")
If it could help, I've integrated the improved WSGI script for use with Django given by Graham Dumpleton in his blog.
Thanks for any suggestion.

Sqlalchemy can't connect to a mysql server

My legacy mysql 4.0.20 server is on a windows machine.
I'm developing a new system (python based) on a linux that needs to connect to the legacy server and make queries etc.
I have successfully connected using both plain MySQLdb and django. I'm having trouble connecting using sqlalchemy. Here is the code:
conn_str = "mysql://user:pass#192.168.171.233/dbd"
engine = create_engine(conn_str, echo=True)
metadata = MetaData(engine)
connection = engine.connect()
and the error stack I'm getting:
2011-03-01 08:35:04,613 INFO sqlalchemy.engine.base.Engine.0x...b42c SELECT DATABASE()
2011-03-01 08:35:04,613 INFO sqlalchemy.engine.base.Engine.0x...b42c ()
Traceback (most recent call last):
connection = engine.connect()
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/base.py", line 1811, in connect
return self.Connection(self, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/base.py", line 832, in init
self.connection = connection or engine.raw_connection()
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/base.py", line 1874, in raw_connection
return self.pool.unique_connection()
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/pool.py", line 142, in unique_connection
return _ConnectionFairy(self).checkout()
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/pool.py", line 369, in __init
rec = self._connection_record = pool.get()
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/pool.py", line 213, in get
return self.do_get()
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/pool.py", line 732, in do_get
con = self.create_connection()
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/pool.py", line 147, in create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/pool.py", line 258, in init
l.first_connect(self.connection, self)
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/strategies.py", line 151, in first_connect
dialect.initialize(c)
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/dialects/mysql/base.py", line 1753, in initialize
default.DefaultDialect.initialize(self, connection)
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/default.py", line 159, in initialize
self.returns_unicode_strings = self._check_unicode_returns(connection)
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/default.py", line 205, in _check_unicode_returns
unicode_for_varchar = check_unicode(sqltypes.VARCHAR(60))
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/default.py", line 195, in check_unicode
]).compile(dialect=self)
File "/usr/local/lib/python2.6/dist-packages/MySQL_python-1.2.3c1-py2.6-linux-i686.egg/MySQLdb/cursors.py", line 173, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.6/dist-packages/MySQL_python-1.2.3c1-py2.6-linux-i686.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(60)) AS anon_1' at line 1")
Looks like MySQL is reporting a syntax error in one of your SQL statements:
<snip>
1064, "You have an error in your SQL syntax.
Check the manual that corresponds to your MySQL server
version for the right syntax to use near
'(60)) AS anon_1' at line 1
</snip>
It looks like MySQL is objecting to a statement that includes the string:
(60)) AS anon_1
NOTE: I am not familiar with Sqlalchemy, but it looks to me like the exception (the SQL error from the statement) is getting propagated up to the handler specified in the connection.
FOLLOWUP: It's likely that a query that Sqlalchemy is executing is not compatible with the older version of MySQL. Looks like it's in the "_check_unicode_returns" function.
Have you tried disabling unicode support?
conn_str = "mysql://user:pass#192.168.171.233/dbd?use_unicode=0"