bash script gives different results - mysql

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?

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

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.

AttributeError in generating peewee model from mysql using pwiz

When i run pwiz to generate a peewee modem from existing database it shows following error:
root#server:~# python -m pwiz -e mysql -P -H 127.0.0.1 mysql
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, 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 "/usr/local/lib/python2.7/dist-packages/pwiz.py", line 202, in <module>
print_models(introspector, tables, preserve_order=options.preserve_order)
File "/usr/local/lib/python2.7/dist-packages/pwiz.py", line 47, in print_models
database = introspector.introspect(table_names=tables)
File "/usr/local/lib/python2.7/dist-packages/playhouse/reflection.py", line 440, in introspect
tables = self.metadata.database.get_tables()
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3089, in get_tables
return [table for table, in self.execute_sql('SHOW TABLES')]
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 2459, in execute_sql
cursor = self.cursor(commit)
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 2445, in cursor
self.connect()
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 2411, in connect
self._state.set_connection(self._connect())
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3083, in _connect
return mysql.connect(db=self.database, **self.connect_params)
AttributeError: 'NoneType' object has no attribute 'connect'
What is the problem? The username and password of database is correct.
PyMySql is not installed. Install it using pip or other available options:
pip install PyMySql
Unfortunately, peewee does not write `PyMySql1 module of python in its dependency list, so it should be installed separately.

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.