From d2c33db8a8ff688d2be91c2edf3cc99baca3dc8b Mon Sep 17 00:00:00 2001 From: Nandor Kracser Date: Mon, 23 Sep 2019 09:36:01 +0200 Subject: [PATCH] storage/mysql: support pre-5.7.20 instances with tx_isolation only --- storage/sql/config.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/storage/sql/config.go b/storage/sql/config.go index 0095b57d..24467169 100644 --- a/storage/sql/config.go +++ b/storage/sql/config.go @@ -29,6 +29,7 @@ const ( // MySQL error codes mysqlErrDupEntry = 1062 mysqlErrDupEntryWithKeyName = 1586 + mysqlErrUnknownSysVar = 1193 ) // SQLite3 options for creating an SQL db. @@ -307,6 +308,26 @@ func (s *MySQL) open(logger log.Logger) (*conn, error) { return nil, err } + err = db.Ping() + if err != nil { + if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == mysqlErrUnknownSysVar { + logger.Info("reconnecting with MySQL pre-5.7.20 compatibilty mode") + + // MySQL 5.7.20 introduced transaction_isolation and deprecated tx_isolation. + // MySQL 8.0 doesn't have tx_isolation at all. + // https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_transaction_isolation + delete(cfg.Params, "transaction_isolation") + cfg.Params["tx_isolation"] = "'SERIALIZABLE'" + + db, err = sql.Open("mysql", cfg.FormatDSN()) + if err != nil { + return nil, err + } + } else { + return nil, err + } + } + errCheck := func(err error) bool { sqlErr, ok := err.(*mysql.MySQLError) if !ok {