{"record":{"id":"9fadceacf94f7283","repo":"micro/go-micro","slug":"couldn-t-use-database","errorCode":null,"errorMessage":"Couldn't use database","messagePattern":"Couldn't use database","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"store/mysql/mysql.go","lineNumber":151,"sourceCode":"\t}\n\t_, err = result.RowsAffected()\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\nfunc (s *sqlStore) initDB() error {\n\t// Create the namespace's database\n\t_, err := s.db.Exec(fmt.Sprintf(\"CREATE DATABASE IF NOT EXISTS %s ;\", s.database))\n\tif err != nil {\n\t\treturn err\n\t}\n\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))","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/mysql/mysql.go#L133-L169","documentation":"During initDB (called from configure) the store executes `USE <database>` on the freshly opened MySQL connection; failure here means the database does not exist, the user lacks permission, or the connection itself is broken. The store cannot scope its tables without selecting the database.","triggerScenarios":"store configuration (namespace/database option) names a database that was never created, the MySQL user lacks privileges on it, or the TCP connection to MySQL failed silently; surfaced via configure() during store Init/newStore.","commonSituations":"Wrong DB name in store options or MYSQL_DATABASE env var; migration scripts never created the database; MySQL user granted no rights on the target schema; connection dropped between dial and USE.","solutions":["Create the database: CREATE DATABASE <name>;","Verify the database name in store options/environment matches an existing schema","Grant privileges: GRANT ALL ON <db>.* TO '<user>'@'%';","Test MySQL connectivity and credentials with the mysql CLI using the same DSN"],"exampleFix":"// before\nstore := mysql.NewStore(store.Database(\"missing_db\"))\n// after\n// mysql: CREATE DATABASE micro_db;\nstore := mysql.NewStore(store.Database(\"micro_db\"))","handlingStrategy":"validation","validationCode":"// run before configuring the store\nrows, err := db.Query(\"SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?\", dbName)\nif err != nil || !rows.Next() {\n    return fmt.Errorf(\"database %s missing or not accessible\", dbName)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create the database in deployment/migration scripts before the app starts","Match the store database option to MYSQL_DATABASE env config","GRANT the app user privileges on the target schema","Test the DSN with the mysql CLI during CI"],"tags":["mysql","store","configuration","permissions"],"backgroundTag":"unknown-database","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}