peewee.OperationalError - MySQL connection refused - mysql

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)")

Related

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

Pymysql can't connect do MySQL DB: hangs at "self.sock.recv_into(b)"?

Currently on macOS 10.15 using python 3.7, with MySQL 8.0.19 installed. Developing in VScode with a virtual environment setup. I've created a local database in phpmyadmin as well. I want to connect to it anyway possible. Script:
import pymysql
print("Before")
connection = pymysql.connect(host='localhost',
user='myUserName', password='myPassword', db='db_name', charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
print("After")
When I run the script, execution hangs indefinitely. After printing "After", I have to quit execution. Trackback is:
Traceback (most recent call last):
File "connect.py", line 5, in <module>
user='myUserName', password='myPassword', db='db_name', charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site-
packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site-
packages/pymysql/connections.py", line 325, in __init__
self.connect()
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site-
packages/pymysql/connections.py", line 598, in connect
self._get_server_information()
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site-
packages/pymysql/connections.py", line 975, in _get_server_information
packet = self._read_packet()
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site-
packages/pymysql/connections.py", line 657, in _read_packet
packet_header = self._read_bytes(4)
File "/Users/name/Documents/Work/Connection Test/env/lib/python3.7/site-
packages/pymysql/connections.py", line 691, in _read_bytes
data = self._rfile.read(num_bytes)
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/
python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
KeyboardInterrupt
No idea why this is occurring. What is happening here? I have searched a ton of questions on stack overflow and none helped me.
Problem was solved by adding the "unix_socket" param in pymysql connect function. See answer here: pymysql can't connect to MySQL on localhost
import pymysql
print("Before")
conn = pymysql.connect(db='w_dev', user='root', passwd='celebrate',
unix_socket="/tmp/mysql.sock")
print("After")
This script worked.

ERPNext OperationalError (2003, "Can't connect to MySQL server on u'localhost' ([Errno 22] Invalid argument)")

I’m getting the below error when trying to install a new site using command
bench --site site1.local install-app erpnext
This is a new installation on Windows Linux SubSustem running Ubuntu.
I’m able to login into mysql through command line using user root and the password which I had set during installation.
pymysql.err.OperationalError: (2003, “Can’t connect to MySQL server on u’localhost’ ([Errno 22] Invalid argument)”)
swadeesh#SWAD-PC:~/frappe-bench$ bench --site site1.local install-app erpnext
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 97, in <module>
main()
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 18, in main
click.Group(commands=commands)(prog_name='bench')
File "/home/swadeesh/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "/home/swadeesh/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/home/swadeesh/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/swadeesh/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/swadeesh/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/swadeesh/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/home/swadeesh/frappe-bench/env/local/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 25, in _func
ret = f(frappe._dict(ctx.obj), *args, **kwargs)
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/commands/site.py", line 169, in install_app
_install_app(app, verbose=context.verbose)
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/installer.py", line 52, in install_app
frappe.clear_cache()
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/__init__.py", line 555, in clear_cache
frappe.cache_manager.clear_user_cache()
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/cache_manager.py", line 42, in clear_user_cache
clear_global_cache()
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/cache_manager.py", line 48, in clear_global_cache
clear_website_cache()
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/website/render.py", line 291, in clear_cache
for method in frappe.get_hooks("website_clear_cache"):
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/__init__.py", line 931, in get_hooks
hooks = _dict(cache().get_value("app_hooks", load_app_hooks))
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/utils/redis_wrapper.py", line 79, in get_value
val = generator()
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/__init__.py", line 905, in load_app_hooks
for app in [app_name] if app_name else get_installed_apps(sort=True):
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/__init__.py", line 869, in get_installed_apps
installed = json.loads(db.get_global("installed_apps") or "[]")
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/database/database.py", line 692, in get_global
return self.get_default(key, user)
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/database/database.py", line 696, in get_default
d = self.get_defaults(key, parent)
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/database/database.py", line 714, in get_defaults
defaults = frappe.defaults.get_defaults(parent)
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/defaults.py", line 77, in get_defaults
globald = get_defaults_for()
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/defaults.py", line 204, in get_defaults_for
where parent = %s order by creation""", (parent,), as_dict=1)
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/database/database.py", line 122, in sql
self.connect()
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/database/database.py", line 75, in connect
self._conn = self.get_connection()
File "/home/swadeesh/frappe-bench/apps/frappe/frappe/database/mariadb/database.py", line 89, in get_connection
local_infile = frappe.conf.local_infile)
File "/home/swadeesh/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/home/swadeesh/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 325, in __init__
self.connect()
File "/home/swadeesh/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 630, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on u'localhost' ([Errno 22] Invalid argument)")
I think you've enabled mysqld_safe mode.
check if you've enable mysqld_safe mode using : sudo ps -aux | grep "mysqld_safe"
if it's appear in output results then you've to disable it using following commands:
Stop mysql service and start it using /etc/init.d/mysql start to start it in normal mode.
Why you're not able to connect using Python Client because root user doesn't need mysql password for login into mysql but you will not be able to connect it using non-root user because of mysqld_safe mode on.

DAL connection string for Web2Py to MySQL on TurnkeyLinux

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 ?

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.