salt icon indicating copy to clipboard operation
salt copied to clipboard

mysql.present fails if database exists, the name contains an underscore, but has wrong charset/collation

Open xpender opened this issue 7 years ago • 9 comments

Description of Issue/Question

If a MySQL database already exists, containing an "_" underscore character in the name, but has wrong charset/collation the state fails.

The def alter_db() statement is escaping the underscore character in the name. But escaping underscore and % in database names seems only be needed on GRANT statements.

https://github.com/saltstack/salt/blame/develop/salt/modules/mysql.py#L1015 https://github.com/saltstack/salt/blame/develop/salt/modules/mysql.py#L571

Setup

test.sls

mysql_test_db:
  mysql_database:
    - present
    - name: test_db_name
    - character_set: utf8mb4

Steps to Reproduce Issue

  • mysql -e "CREATE DATABSE test_db_name CHARSET latin1;"
  • salt-call state.sls test.sls
[DEBUG   ] Doing query: ALTER DATABASE `test\_db\_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
[ERROR   ] An exception occurred in this state: Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1913, in call
    **cdata['kwargs'])
  File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1898, in wrapper
    return f(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/salt/states/mysql_database.py", line 69, in present
    **connection_args)
  File "/usr/lib/python2.7/dist-packages/salt/modules/mysql.py", line 967, in alter_db
    _execute(cur, qry, args)
  File "/usr/lib/python2.7/dist-packages/salt/modules/mysql.py", line 552, in _execute
    return cur.execute(qry)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
InternalError: (1, "Can't create/write to file './test@005c_db@005c_name/db.opt' (Errcode: 2 - No such file or directory)")

Versions Report

Salt Version:
           Salt: 2018.3.3

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.5.3
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.9.4
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.8
   mysql-python: 1.3.7
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.13 (default, Sep 26 2018, 18:42:22)
   python-gnupg: Not Installed
         PyYAML: 3.12
          PyZMQ: 16.0.2
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.4.3
            ZMQ: 4.2.1

System Versions:
           dist: debian 9.7
         locale: UTF-8
        machine: x86_64
        release: 4.9.0-8-amd64
         system: Linux
        version: debian 9.7

(master-less minion)

MySQL Server version: 5.6.43-84.3-log Percona Server (GPL), Release 84.3, Revision 71967c9

xpender avatar Feb 08 '19 14:02 xpender

Any progress on this one?

jajadinimueter avatar Apr 12 '19 13:04 jajadinimueter

Hit the same issue today, on Debian, Salt 2019.2 (python3) with MariaDB 10.

Packages

ii  salt-common              2019.2.0+ds-1
ii  salt-minion              2019.2.0+ds-1 
ii  mariadb-server           10.1.38-0+deb9u1 
ii  python3.5                3.5.3-1+deb9u1
ii  python3-mysqldb          1.3.7-1.1

With this state file:

testing_database_02:
  mysql_database.present:
    - character_set: utf8mb4
    - collate: utf8mb4_general_ci
  1. On the first pass (the database does not exist):
  • it's created correctly, with proper encoding and collate:
MariaDB [(none)]> SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='testing_database_02';
+----------------------------+------------------------+
| DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME |
+----------------------------+------------------------+
| utf8mb4                    | utf8mb4_general_ci     |
+----------------------------+------------------------+
  • the database dir is created as testing_database_02 and the db.opt file on disk is created as /var/lib/mysql/testing_database_02/db.opt
  1. Subsequent runs fail:
[INFO    ] Executing state mysql_database.present for [testing_database_02]
[DEBUG   ] Doing query: SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=%(dbname)s; args: {'dbname': 'testing_database_02'} 
[DEBUG   ] character set differes from utf8mb4 : utf8mb4
[DEBUG   ] collate set differs from utf8mb4_general_ci : utf8mb4_general_ci
[DEBUG   ] Doing query: SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=%(dbname)s; args: {'dbname': 'testing_database_02'} 
[DEBUG   ] Doing query: ALTER DATABASE `testing\_database\_02` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
[DEBUG   ] An exception occurred in this state: (1, 'Can\'t create/write to file \'./testing@005c_database@005c_02/db.opt\' (Errcode: 2 "No such file or directory")')
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/state.py", line 1933, in call
    **cdata['kwargs'])
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 1939, in wrapper
    return f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/salt/states/mysql_database.py", line 69, in present
    **connection_args)
  File "/usr/lib/python3/dist-packages/salt/modules/mysql.py", line 1041, in alter_db
    _execute(cur, qry, args)
  File "/usr/lib/python3/dist-packages/salt/modules/mysql.py", line 611, in _execute
    return cur.execute(qry)
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.InternalError: (1, 'Can\'t create/write to file \'./testing@005c_database@005c_02/db.opt\' (Errcode: 2 "No such file or directory")')
[ERROR   ] An exception occurred in this state: Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/state.py", line 1933, in call
    **cdata['kwargs'])
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 1939, in wrapper
    return f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/salt/states/mysql_database.py", line 69, in present
    **connection_args)
  File "/usr/lib/python3/dist-packages/salt/modules/mysql.py", line 1041, in alter_db
    _execute(cur, qry, args)
  File "/usr/lib/python3/dist-packages/salt/modules/mysql.py", line 611, in _execute
    return cur.execute(qry)
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.InternalError: (1, 'Can\'t create/write to file \'./testing@005c_database@005c_02/db.opt\' (Errcode: 2 "No such file or directory")')

As the logs show, it compares the collate and character set and considers they differ, even when they're the same

[DEBUG   ] character set differes from utf8mb4 : utf8mb4
[DEBUG   ] collate set differs from utf8mb4_general_ci : utf8mb4_general_ci

and then, as @xpender says, it tries to run an ALTER DATABASE, escaping the database name's underscores:

[DEBUG   ] Doing query: ALTER DATABASE `testing\_database\_02` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
[DEBUG   ] An exception occurred in this state: (1, 'Can\'t create/write to file \'./testing@005c_database@005c_02/db.opt\' (Errcode: 2 "No such file or directory")')

javierbertoli avatar Jun 13 '19 10:06 javierbertoli

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

stale[bot] avatar Jan 08 '20 11:01 stale[bot]

Thank you for updating this issue. It is no longer marked as stale.

stale[bot] avatar Jan 08 '20 13:01 stale[bot]

Can confirm issue.

ppeereb1 avatar Feb 13 '20 12:02 ppeereb1

Somewhat more alarmingly, this issue makes it impossible to create a new, fresh database if the name contains an underscore:

test-underscore-db:
  mysql_database.present:
    - name: test_db
    - character_set: utf8mb4
    - collate: utf8mb4_unicode_ci

Trimmed output:

[ERROR   ] Failed to create database test_db
...
----------
          ID: test-underscore-db
    Function: mysql_database.present
        Name: test_db
      Result: False
     Comment: Failed to create database test_db
     Started: 20:17:23.126248
    Duration: 5.596 ms
     Changes:
----------
...

This is on Debian Buster, MariaDB 10.3.22.

smitelli avatar Apr 03 '20 20:04 smitelli

We're hitting the same issue on Salt 3000.3

Oloremo avatar Jul 09 '20 09:07 Oloremo

I think this issue has been fixed somehow since I can't reproduce it in 3002.2 on Debian Buster.

... (which I am actually facing on 2019.2.7 but that's another story of me and deferred updates at work 😅)

kiniou avatar Nov 25 '20 17:11 kiniou

Fixed by #58148 (merged 2022-01-15, commit b43cfed). The underscore-escaping was removed from alter_db; present on 3006.x/3007.x/3008.x/master.

dwoz avatar Jun 29 '26 21:06 dwoz