Skip to content

Commit

Permalink
mysqlctl: Move more to use built in MySQL client
Browse files Browse the repository at this point in the history
executeMysqlScript was still using a `mysql` client binary for
initialization, but there's no reason it needs to do that. We can also
move this to our built in MySQL client to be faster and have less
dependencies here.

Follow up to vitessio#13178 where we already moved things for just the schema
commands.

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
  • Loading branch information
dbussink committed Jun 18, 2023
1 parent 1b412e8 commit 41b05e0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 37 deletions.
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

0 comments on commit 41b05e0

Please sign in to comment.