func reinitConfigCmd(subFlags *flag.FlagSet, args []string) error { // There ought to be an existing my.cnf, so use it to find mysqld. mysqld, err := mysqlctl.OpenMysqld(uint32(*tabletUID), dbconfigFlags) if err != nil { return fmt.Errorf("failed to find mysql config: %v", err) } defer mysqld.Close() if err := mysqld.ReinitConfig(context.TODO()); err != nil { return fmt.Errorf("failed to reinit mysql config: %v", err) } return nil }
func startCmd(subFlags *flag.FlagSet, args []string) error { waitTime := subFlags.Duration("wait_time", 5*time.Minute, "how long to wait for startup") subFlags.Parse(args) // There ought to be an existing my.cnf, so use it to find mysqld. mysqld, err := mysqlctl.OpenMysqld(uint32(*tabletUID), dbconfigFlags) if err != nil { return fmt.Errorf("failed to find mysql config: %v", err) } defer mysqld.Close() ctx, cancel := context.WithTimeout(context.Background(), *waitTime) defer cancel() if err := mysqld.Start(ctx); err != nil { return fmt.Errorf("failed start mysql: %v", err) } return nil }
func teardownCmd(subFlags *flag.FlagSet, args []string) error { waitTime := subFlags.Duration("wait_time", 5*time.Minute, "how long to wait for shutdown") force := subFlags.Bool("force", false, "will remove the root directory even if mysqld shutdown fails") subFlags.Parse(args) // There ought to be an existing my.cnf, so use it to find mysqld. mysqld, err := mysqlctl.OpenMysqld(uint32(*tabletUID), dbconfigFlags) if err != nil { return fmt.Errorf("failed to find mysql config: %v", err) } defer mysqld.Close() ctx, cancel := context.WithTimeout(context.Background(), *waitTime) defer cancel() if err := mysqld.Teardown(ctx, *force); err != nil { return fmt.Errorf("failed teardown mysql (forced? %v): %v", *force, err) } return nil }
func startCmd(subFlags *flag.FlagSet, args []string) error { waitTime := subFlags.Duration("wait_time", 5*time.Minute, "how long to wait for startup") var mysqldArgs flagutil.StringListValue subFlags.Var(&mysqldArgs, "mysqld_args", "List of comma-separated flags to pass additionally to mysqld") subFlags.Parse(args) // There ought to be an existing my.cnf, so use it to find mysqld. mysqld, err := mysqlctl.OpenMysqld(uint32(*tabletUID), dbconfigFlags) if err != nil { return fmt.Errorf("failed to find mysql config: %v", err) } defer mysqld.Close() ctx, cancel := context.WithTimeout(context.Background(), *waitTime) defer cancel() if err := mysqld.Start(ctx, mysqldArgs...); err != nil { return fmt.Errorf("failed start mysql: %v", err) } return nil }
func main() { defer exit.Recover() defer logutil.Flush() dbconfigFlags := dbconfigs.AppConfig | dbconfigs.DbaConfig | dbconfigs.FilteredConfig | dbconfigs.ReplConfig dbconfigs.RegisterFlags(dbconfigFlags) flag.Parse() // We'll register this OnTerm handler before mysqld starts, so we get notified // if mysqld dies on its own without us (or our RPC client) telling it to. mysqldTerminated := make(chan struct{}) onTermFunc := func() { close(mysqldTerminated) } // Start or Init mysqld as needed. ctx, cancel := context.WithTimeout(context.Background(), *waitTime) tabletDir := mysqlctl.TabletDir(uint32(*tabletUID)) if _, statErr := os.Stat(tabletDir); os.IsNotExist(statErr) { // Generate my.cnf from scratch and use it to find mysqld. log.Infof("tablet dir (%s) doesn't exist, initializing", tabletDir) var err error mysqld, err = mysqlctl.CreateMysqld(uint32(*tabletUID), *mysqlSocket, int32(*mysqlPort), dbconfigFlags) if err != nil { log.Errorf("failed to initialize mysql config: %v", err) exit.Return(1) } mysqld.OnTerm(onTermFunc) if err := mysqld.Init(ctx, *initDBSQLFile); err != nil { log.Errorf("failed to initialize mysql data dir and start mysqld: %v", err) exit.Return(1) } } else { // There ought to be an existing my.cnf, so use it to find mysqld. log.Infof("tablet dir (%s) already exists, starting without init", tabletDir) var err error mysqld, err = mysqlctl.OpenMysqld(uint32(*tabletUID), dbconfigFlags) if err != nil { log.Errorf("failed to find mysql config: %v", err) exit.Return(1) } mysqld.OnTerm(onTermFunc) if err := mysqld.Start(ctx); err != nil { log.Errorf("failed to start mysqld: %v", err) exit.Return(1) } } cancel() servenv.Init() defer servenv.Close() // Take mysqld down with us on SIGTERM before entering lame duck. servenv.OnTerm(func() { log.Infof("mysqlctl received SIGTERM, shutting down mysqld first") ctx := context.Background() mysqld.Shutdown(ctx, false) }) // Start RPC server and wait for SIGTERM. mysqlctldTerminated := make(chan struct{}) go func() { servenv.RunDefault() close(mysqlctldTerminated) }() select { case <-mysqldTerminated: log.Infof("mysqld shut down on its own, exiting mysqlctld") case <-mysqlctldTerminated: log.Infof("mysqlctld shut down gracefully") } }