Configuring SQLAlchemy

Hi all!

I’m trying to configure SQLAlchemy to work with my MySQL instance at home and I’m getting the following error:

Jul 17 20:09:21 HA hass[3313]: Exception in thread Thread-1:
Jul 17 20:09:21 HA hass[3313]: Traceback (most recent call last):
Jul 17 20:09:21 HA hass[3313]: File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
Jul 17 20:09:21 HA hass[3313]: self.run()
Jul 17 20:09:21 HA hass[3313]: File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/recorder/__init__.py", line 171, in run
Jul 17 20:09:21 HA hass[3313]: self._setup_connection()
Jul 17 20:09:21 HA hass[3313]: File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/recorder/__init__.py", line 262, in _setup_connection
Jul 17 20:09:21 HA hass[3313]: self.engine = create_engine(self.db_url, echo=False)
Jul 17 20:09:21 HA hass[3313]: File "/srv/hass/hass_venv/lib/python3.4/site-packages/sqlalchemy/engine/__init__.py", line 386, in create_engine
Jul 17 20:09:21 HA hass[3313]: return strategy.create(*args, **kwargs)
Jul 17 20:09:21 HA hass[3313]: File "/srv/hass/hass_venv/lib/python3.4/site-packages/sqlalchemy/engine/strategies.py", line 75, in create
Jul 17 20:09:21 HA hass[3313]: dbapi = dialect_cls.dbapi(**dbapi_args)
Jul 17 20:09:21 HA hass[3313]: File "/srv/hass/hass_venv/lib/python3.4/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 92, in dbapi
Jul 17 20:09:21 HA hass[3313]: return __import__('MySQLdb')
Jul 17 20:09:21 HA hass[3313]: ImportError: No module named 'MySQLdb'

This is the configuration.yaml section:

recorder:
  db_url: mysql://User:Password@ServerIP/HADataBase

I’ve followed all steps from https://home-assistant.io/components/recorder/

Ideas?

1 Like

pip3 install mysqlclient

(this is on the docs as of a few hours ago)

Hey @AlucardZero,
thanks for replying. I saw that after some tests.

When I try to install mysqlclient I get the following error:

hass@HA:~/.homeassistant$ pip3 install mysqlclient
Downloading/unpacking mysqlclient
  Downloading mysqlclient-1.3.7.tar.gz (79kB): 79kB downloaded
  Running setup.py (path:/tmp/pip-build-0rctt3ov/mysqlclient/setup.py) egg_info for package mysqlclient
    /bin/sh: 1: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/tmp/pip-build-0rctt3ov/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/tmp/pip-build-0rctt3ov/mysqlclient/setup_posix.py", line 44, in get_config
        libs = mysql_config("libs_r")
      File "/tmp/pip-build-0rctt3ov/mysqlclient/setup_posix.py", line 26, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    OSError: mysql_config not found
    Complete output from command python setup.py egg_info:
    /bin/sh: 1: mysql_config: not found

Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/tmp/pip-build-0rctt3ov/mysqlclient/setup.py", line 17, in <module>

    metadata, options = get_config()

  File "/tmp/pip-build-0rctt3ov/mysqlclient/setup_posix.py", line 44, in get_config

    libs = mysql_config("libs_r")

  File "/tmp/pip-build-0rctt3ov/mysqlclient/setup_posix.py", line 26, in mysql_config

    raise EnvironmentError("%s not found" % (mysql_config.path,))

OSError: mysql_config not found

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-0rctt3ov/mysqlclient
Storing debug log for failure in /home/hass/.pip/pip.log

My installation is done with the All-In-One Script (just for more information).

Probably install libmysqlclient-dev first

Try this :

pip install PyMySQL

then change your recorder: url to mysql+pymysql://

Just tried myself and works flawless !!

1 Like

@thefrenchmatt - Did you install that as the hass user in the venv or as a root user in your normal environment? This could be the answer for problem we’re having in another thread:

I’d certainly appreciate any help.

I tried different configs and for the moment am not recording through mysql, but it was venv and a local mariadb on another server

1 Like