{"record":{"id":"28310be74f43bae8","repo":"micro/go-micro","slug":"failed-to-prepare-read-statement","errorCode":null,"errorMessage":"failed to prepare read statement","messagePattern":"failed to prepare read statement","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store/mysql/mysql.go","lineNumber":166,"sourceCode":"\n\t_, err = s.db.Exec(fmt.Sprintf(\"USE %s ;\", s.database))\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Couldn't use database\")\n\t}\n\n\t// Create a table for the namespace's prefix\n\tcreateSQL := fmt.Sprintf(\"CREATE TABLE IF NOT EXISTS %s (`key` varchar(255) primary key, value blob null, expiry timestamp not null);\", s.table)\n\t_, err = s.db.Exec(createSQL)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Couldn't create table\")\n\t}\n\n\t// prepare statements\n\tvar prepareErr error\n\n\ts.readPrepare, prepareErr = s.db.Prepare(fmt.Sprintf(\"SELECT `key`, value, expiry FROM %s.%s WHERE `key` = ?;\", s.database, s.table))\n\tif prepareErr != nil {\n\t\treturn errors.Wrap(prepareErr, \"failed to prepare read statement\")\n\t}\n\n\ts.writePrepare, prepareErr = s.db.Prepare(fmt.Sprintf(\"INSERT INTO %s.%s (`key`, value, expiry) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE `value`= ?, `expiry` = ?\", s.database, s.table))\n\tif prepareErr != nil {\n\t\treturn errors.Wrap(prepareErr, \"failed to prepare write statement\")\n\t}\n\n\ts.deletePrepare, prepareErr = s.db.Prepare(fmt.Sprintf(\"DELETE FROM %s.%s WHERE `key` = ?;\", s.database, s.table))\n\tif prepareErr != nil {\n\t\treturn errors.Wrap(prepareErr, \"failed to prepare delete statement\")\n\t}\n\n\treturn nil\n}\n\nfunc (s *sqlStore) configure() error {\n\tnodes := s.options.Nodes\n\tif len(nodes) == 0 {","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/mysql/mysql.go#L148-L184","documentation":"initDB prepares the SELECT statement used by sqlStore.Read; if db.Prepare fails, Read cannot work and configure aborts. Preparation failures usually indicate SQL syntax problems from interpolated names or a broken/dead connection to MySQL.","triggerScenarios":"configure() -> initDB() calls s.db.Prepare(\"SELECT ... WHERE `key` = ?\") and the driver returns an error, typically after a connection drop or with an invalid database.table identifier.","commonSituations":"MySQL connection dropped between CREATE TABLE and Prepare; malformed namespace producing invalid `<db>.<table>` SQL; driver version incompatibility; server-side PREPARE restrictions.","solutions":["Check the wrapped cause for a MySQL error code (connection vs syntax)","Verify database/table names form valid identifiers","Recreate the store or call configure again to reopen the connection","Confirm the connection DSN and MySQL server health"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if err := db.Ping(); err != nil {\n    return fmt.Errorf(\"mysql unreachable before store configure: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := st.Init(opts...); err != nil {\n    if strings.Contains(err.Error(), \"failed to prepare\") {\n        time.Sleep(time.Second)\n        return st.Init(opts...) // reconnect and re-prepare\n    }\n    return err\n}","preventionTips":["Ping MySQL before store configuration","Keep namespaces as valid SQL identifiers","Avoid proxies that disable server-side prepared statements","Retry configure on transient connection failures"],"tags":["mysql","store","prepare","sql"],"backgroundTag":"sql-prepare-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}