salt: bytes error in mysql module

Description of Issue/Question

I recently switch from python2.7 to python 3.6 and a lot of modules are stopped working.

The first issue is pip: #44980 Second issue is mysql

Function: mysql_grants.present
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/local/lib/python3.6/site-packages/salt/state.py", line 1843, in call
                  **cdata['kwargs'])
                File "/usr/local/lib/python3.6/site-packages/salt/loader.py", line 1795, in wrapper
                  return f(*args, **kwargs)
                File "/usr/local/lib/python3.6/site-packages/salt/states/mysql_grants.py", line 150, in present
                  grant, database, user, host, grant_option, escape, **connection_args
                File "/usr/local/lib/python3.6/site-packages/salt/modules/mysql.py", line 1778, in grant_exists
                  grants = user_grants(user, host, **connection_args)
                File "/usr/local/lib/python3.6/site-packages/salt/modules/mysql.py", line 1745, in user_grants
                  tmp = grant[0].split(' IDENTIFIED BY')[0]
              TypeError: a bytes-like object is required, not 'str'
     Started: 10:50:28.599004
    Duration: 11.843 ms
     Changes:

Third issue is with http

 Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/local/lib/python3.6/site-packages/salt/state.py", line 1843, in call
                  **cdata['kwargs'])
                File "/usr/local/lib/python3.6/site-packages/salt/loader.py", line 1795, in wrapper
                  return f(*args, **kwargs)
                File "/usr/local/lib/python3.6/site-packages/salt/states/http.py", line 153, in wait_for_successful_query
                  raise caught_exception  # pylint: disable=E0702
                File "/usr/local/lib/python3.6/site-packages/salt/states/http.py", line 144, in wait_for_successful_query
                  ret = query(name, wait_for=wait_for, **kwargs)
                File "/usr/local/lib/python3.6/site-packages/salt/states/http.py", line 96, in query
                  if match in data.get('text', ''):
              TypeError: a bytes-like object is required, not 'str'
     Started: 10:27:10.800930
    Duration: 301579.96 ms

All the issues I am having have similar errors:

TypeError: a bytes-like object is required, not 'str'

Can someone look at this?

Setup

FreeBSD 11.1

Steps to Reproduce Issue

  1. install package py36-salt
  2. to reproduce pip issue, create a state file with pip.installed and added it to top.sls and run salt state.highstate
  3. to reproduce mysql issue, create a state file with mysql state and run it from highstate.

Versions Report

Salt Version: Salt: 2017.7.2

Dependency Versions: cffi: 1.11.2 cherrypy: Not Installed dateutil: 2.6.1 docker-py: Not Installed gitdb: Not Installed gitpython: Not Installed ioflo: Not Installed Jinja2: 2.10 libgit2: Not Installed libnacl: Not Installed M2Crypto: Not Installed Mako: Not Installed msgpack-pure: Not Installed msgpack-python: 0.4.8 mysql-python: 1.2.5 pycparser: 2.18 pycrypto: 2.6.1 pycryptodome: Not Installed pygit2: Not Installed Python: 3.6.3 (default, Dec 11 2017, 00:25:34) python-gnupg: Not Installed PyYAML: 3.12 PyZMQ: 16.0.3 RAET: Not Installed smmap: Not Installed timelib: Not Installed Tornado: 4.5.2 ZMQ: 4.2.2

System Versions: dist: locale: UTF-8 machine: amd64 release: 11.1-RELEASE-p4 system: FreeBSD version: Not Installed

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 27 (15 by maintainers)

Most upvoted comments

i believe i have the same issue, for me it helped to str() the relevant return-values from MySQLdb’s cur.fetchall(), like so:

diff --git a/salt/modules/mysql.py b/salt/modules/mysql.py
index 889de1c..1e32556 100644
--- a/mysql.py
+++ b/mysql.py
@@ -1766,8 +1766,8 @@ def user_grants(user,
     ret = []
     results = cur.fetchall()
     for grant in results:
-        tmp = grant[0].split(' IDENTIFIED BY')[0]
-        if 'WITH GRANT OPTION' in grant[0] and 'WITH GRANT OPTION' not in tmp:
+        tmp = str(grant[0]).split(' IDENTIFIED BY')[0]
+        if 'WITH GRANT OPTION' in str(grant[0]) and 'WITH GRANT OPTION' not in tmp:
             tmp = '{0} WITH GRANT OPTION'.format(tmp)
         ret.append(tmp)
     log.debug(ret)

Okay…as the fix from @andreasnuesslein works, i’m going to check how i can incorporate it into salt.

You need to pass charset=“utf8mb4” or “utf8” to connection args.