Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access denied for user 'root'@'localhost' #194

Open
danielpardorus opened this issue Aug 1, 2018 · 2 comments
Open

Access denied for user 'root'@'localhost' #194

danielpardorus opened this issue Aug 1, 2018 · 2 comments

Comments

@danielpardorus
Copy link

Hi,
I get this error when i exec mysql state for the first time with mariadb. It fails in mysql_database.present function:

root@server1:~# salt-call state.apply mysql
[ERROR   ] An exception occurred in this state: Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1905, in call
    **cdata['kwargs'])
  File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1830, in wrapper
    return f(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/salt/states/mysql_database.py", line 55, in present
    existing = __salt__['mysql.db_get'](name, **connection_args)
  File "/usr/lib/python2.7/dist-packages/salt/modules/mysql.py", line 975, in db_get
    dbc = _connect(**connection_args)
  File "/usr/lib/python2.7/dist-packages/salt/modules/mysql.py", line 333, in _connect
    dbc = MySQLdb.connect(**connargs)
  File "/usr/lib/python2.7/dist-packages/pymysql/__init__.py", line 88, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 679, in __init__
    self.connect()
  File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 891, in connect
    self._request_authentication()
  File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 1097, in _request_authentication
    auth_packet = self._read_packet()
  File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 966, in _read_packet
    packet.check_error()
  File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 394, in check_error
    err.raise_mysql_exception(self._data)
  File "/usr/lib/python2.7/dist-packages/pymysql/err.py", line 120, in raise_mysql_exception
    _check_mysql_exception(errinfo)
  File "/usr/lib/python2.7/dist-packages/pymysql/err.py", line 115, in _check_mysql_exception
    raise InternalError(errno, errorvalue)
InternalError: (1698, u"Access denied for user 'root'@'localhost'")

but second attempt works. Is this a known issue? Am I doing anything wrong?

Thanks in advance.

@danielpardorus
Copy link
Author

danielpardorus commented Aug 20, 2018

Hi,
I was investigating a little about this problem and I think I know what is happening.

Salt mysql module is trying to import MySQLdb and if it fails, pymysql is imported:

try:
    # Trying to import MySQLdb
    import MySQLdb
    import MySQLdb.cursors
    import MySQLdb.converters
    from MySQLdb.constants import FIELD_TYPE, FLAG
    from MySQLdb.connections import OperationalError
except ImportError:
    try:
        # MySQLdb import failed, try to import PyMySQL
        import pymysql
        pymysql.install_as_MySQLdb()
        import MySQLdb
        import MySQLdb.cursors
        import MySQLdb.converters
        from MySQLdb.constants import FIELD_TYPE, FLAG
        from MySQLdb.err import OperationalError
    except ImportError:
        MySQLdb = None`

So, the main problem is that pymysql is not using /var/run/mysqld/mysqld.sock as default, as formula does. That's why first attempt fails, but when second attempt is executed, MySQLdb
library is installed and import MySQLdb success. I think MySQLdb is using /var/run/mysqld/mysqld.sock as default, and all works magically.

If you always use pymysql: pythonpkg: python-pymysql, formula never success.

This is a problem when you perform a fresh install (for CI for example), so I added - connection_unix_socket: '{{ mysql_socket }}' to all states that need a DB connection and now it works with both libraries.

Please, correct me if I'm wrong. I'm new on salt 😇

PD: This tests are executed with vagrant and ubuntu xenial, with most of defaults.
Thanks in advance.

@noelmcloughlin
Copy link
Member

noelmcloughlin commented Aug 26, 2018

I have never seen your specific issue (yet ;-). Just tested on Ubuntu xenial /Fedora today (different problems saltstack-formulas/devstack-formula#2 and saltstack-formulas/devstack-formula#3) But I'm new on mysql 😇too. I presume connection_unix_socket: '{{ mysql_socket }}' is undocumented workaround - how did you figure that out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants