Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysqlctl: Move more to use built in MySQL client #13338

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions go/vt/mysqlctl/mysqld.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ func (mysqld *Mysqld) Init(ctx context.Context, cnf *Mycnf, initDBSQLFile string
return err
}
if initDBSQLFile == "" { // default to built-in
if err := mysqld.executeMysqlScript(params, strings.NewReader(config.DefaultInitDB)); err != nil {
if err := mysqld.executeMysqlScript(ctx, params, config.DefaultInitDB); err != nil {
return fmt.Errorf("failed to initialize mysqld: %v", err)
}
return nil
Expand All @@ -663,7 +663,11 @@ func (mysqld *Mysqld) Init(ctx context.Context, cnf *Mycnf, initDBSQLFile string
return fmt.Errorf("can't open init_db_sql_file (%v): %v", initDBSQLFile, err)
}
defer sqlFile.Close()
if err := mysqld.executeMysqlScript(params, sqlFile); err != nil {
script, err := io.ReadAll(sqlFile)
if err != nil {
return fmt.Errorf("can't read init_db_sql_file (%v): %v", initDBSQLFile, err)
}
if err := mysqld.executeMysqlScript(ctx, params, string(script)); err != nil {
return fmt.Errorf("can't run init_db_sql_file (%v): %v", initDBSQLFile, err)
}
return nil
Expand Down Expand Up @@ -1006,34 +1010,25 @@ func deleteTopDir(dir string) (removalErr error) {
return
}

// executeMysqlScript executes a .sql script from an io.Reader with the mysql
// command line tool. It uses the connParams as is, not adding credentials.
func (mysqld *Mysqld) executeMysqlScript(connParams *mysql.ConnParams, sql io.Reader) error {
dir, err := vtenv.VtMysqlRoot()
if err != nil {
return err
}
name, err := binaryPath(dir, "mysql")
if err != nil {
return err
}
cnf, err := mysqld.defaultsExtraFile(connParams)
// executeMysqlScript executes the contents of an SQL script as a string.
// It uses the connParams as is, not adding credentials.
func (mysqld *Mysqld) executeMysqlScript(ctx context.Context, connParams *mysql.ConnParams, sql string) error {
connector := dbconfigs.New(connParams)
conn, err := connector.Connect(ctx)
if err != nil {
return err
}
defer os.Remove(cnf)
args := []string{
"--defaults-extra-file=" + cnf,
"--batch",
"--default-character-set=utf8mb4",
}
env, err := buildLdPaths()
defer conn.Close()

_, more, err := conn.ExecuteFetchMulti(sql, -1, false)
if err != nil {
return err
}
_, _, err = execCmd(name, args, env, dir, sql)
if err != nil {
return err
for more {
_, more, _, err = conn.ReadQueryResult(0, false)
if err != nil {
return err
}
}
return nil
}
Expand Down
15 changes: 2 additions & 13 deletions go/vt/mysqlctl/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,11 @@ var autoIncr = regexp.MustCompile(` AUTO_INCREMENT=\d+`)

// executeSchemaCommands executes some SQL commands. It uses the dba connection parameters, with credentials.
func (mysqld *Mysqld) executeSchemaCommands(ctx context.Context, sql string) error {
conn, err := getPoolReconnect(ctx, mysqld.dbaPool)
if err != nil {
return err
}
defer conn.Recycle()
_, more, err := conn.ExecuteFetchMulti(sql, 0, false)
params, err := mysqld.dbcfgs.DbaConnector().MysqlParams()
if err != nil {
return err
}
for more {
_, more, _, err = conn.ReadQueryResult(0, false)
if err != nil {
return err
}
}
return nil
return mysqld.executeMysqlScript(ctx, params, sql)
}

func encodeEntityName(name string) string {
Expand Down