micro/go-micro · critical
Couldn't use database
Error message
Couldn't use database
What it means
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.
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
Example fix
// before
store := mysql.NewStore(store.Database("missing_db"))
// after
// mysql: CREATE DATABASE micro_db;
store := mysql.NewStore(store.Database("micro_db")) Defensive patterns
Strategy: validation
Validate before calling
// run before configuring the store
rows, err := db.Query("SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?", dbName)
if err != nil || !rows.Next() {
return fmt.Errorf("database %s missing or not accessible", dbName)
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Couldn't create table
- Couldn't insert record
- failed to prepare delete statement
- failed to prepare read statement
- failed to prepare write statement
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/9fadceacf94f7283.
Report an issue: GitHub.
Appendix: source
Thrown at store/mysql/mysql.go:151
}
_, err = result.RowsAffected()
if err != nil {
return err
}
return nil
}
func (s *sqlStore) initDB() error {
// Create the namespace's database
_, err := s.db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s ;", s.database))
if err != nil {
return err
}
_, err = s.db.Exec(fmt.Sprintf("USE %s ;", s.database))
if err != nil {
return errors.Wrap(err, "Couldn't use database")
}
// Create a table for the namespace's prefix
createSQL := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (`key` varchar(255) primary key, value blob null, expiry timestamp not null);", s.table)
_, err = s.db.Exec(createSQL)
if err != nil {
return errors.Wrap(err, "Couldn't create table")
}
// prepare statements
var prepareErr error
s.readPrepare, prepareErr = s.db.Prepare(fmt.Sprintf("SELECT `key`, value, expiry FROM %s.%s WHERE `key` = ?;", s.database, s.table))
if prepareErr != nil {
return errors.Wrap(prepareErr, "failed to prepare read statement")
}
s.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))View on GitHub (pinned to 24529f1404)