vitessio/vitess · error

couldn't generate random MySQL server_id: %v

Error message

couldn't generate random MySQL server_id: %v

What it means

CreateMysqldAndMycnf calls mycnf.RandomizeMysqlServerID to give the tablet a random server_id (avoiding replicate-same-server-id data loss if the tablet was recently a primary). The wrapped error indicates server_id generation or validation failed.

Source

Thrown at go/vt/mysqlctl/cmd.go:44

	"vitess.io/vitess/go/mysql/collations"
	"vitess.io/vitess/go/vt/dbconfigs"
)

// CreateMysqldAndMycnf returns a Mysqld and a Mycnf object to use for working with a MySQL
// installation that hasn't been set up yet.
func CreateMysqldAndMycnf(tabletUID uint32, mysqlSocket string, mysqlPort int, collationEnv *collations.Environment) (*Mysqld, *Mycnf, error) {
	mycnf := NewMycnf(tabletUID, mysqlPort)
	// Choose a random MySQL server-id, since this is a fresh data dir.
	// We don't want to use the tablet UID as the MySQL server-id,
	// because reusing server-ids is not safe.
	//
	// For example, if a tablet comes back with an empty data dir, it will restore
	// from backup and then connect to the primary. But if this tablet has the same
	// server-id as before, and if this tablet was recently a primary, then it can
	// lose data by skipping binlog events due to replicate-same-server-id=FALSE,
	// which is the default setting.
	if err := mycnf.RandomizeMysqlServerID(); err != nil {
		return nil, nil, fmt.Errorf("couldn't generate random MySQL server_id: %v", err)
	}
	if mysqlSocket != "" {
		mycnf.SocketFile = mysqlSocket
	}

	dbconfigs.GlobalDBConfigs.InitWithSocket(mycnf.SocketFile, collationEnv)
	return NewMysqld(&dbconfigs.GlobalDBConfigs), mycnf, nil
}

// OpenMysqldAndMycnf returns a Mysqld and a Mycnf object to use for working with a MySQL
// installation that already exists. The Mycnf will be built based on the my.cnf file
// of the MySQL instance.
func OpenMysqldAndMycnf(tabletUID uint32, collationEnv *collations.Environment) (*Mysqld, *Mycnf, error) {
	// We pass a port of 0, this will be read and overwritten from the path on disk
	mycnf, err := ReadMycnf(NewMycnf(tabletUID, 0), 0)
	if err != nil {
		return nil, nil, fmt.Errorf("couldn't read my.cnf file: %v", err)
	}

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Inspect the wrapped inner error for the root cause.
  2. Ensure the my.cnf template used by mysqlctl includes a server-id entry that can be rewritten.
  3. Check write permissions on the tablet's my.cnf file.
  4. Re-run init after fixing template/permissions.
Defensive patterns

Strategy: validation

Validate before calling

// ensure the template has a server-id line before init
cnf, err := mysqlctl.ReadMycnf(NewMycnf(uid, 0), 0)
if err != nil {
	return err
}
if cnf.ServerID == 0 { /* template missing server-id */ }

Try / catch

m, mycnf, err := mysqlctl.CreateMysqldAndMycnf(uid, ...)
if err != nil && strings.Contains(err.Error(), "couldn't generate random MySQL server_id") {
	// inspect my.cnf template/permissions and re-init
}

Prevention

When it happens

Trigger: Mycnf template lacks a server-id variable to rewrite, repeated collision detection failing, or underlying mycnf validation error during randomization (e.g., invalid my.cnf path or template mismatch).

Common situations: Custom my.cnf templates missing the server-id placeholder, read-only my.cnf file, corrupted existing my.cnf for the tablet UID.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/3f625d501e9128da. Report an issue: GitHub.