Skip to content

Commit afff75f

Browse files
authored
Merge pull request #449 from acekingke/fixRestoreFail
sidecar: Skip `reset slave all` when initialization.
2 parents 58d822a + 90d2951 commit afff75f

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

sidecar/config.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func (cfg *Config) buildXenonConf() []byte {
359359
}
360360

361361
// buildInitSql used to build init.sql. The file run after the mysql init.
362-
func (cfg *Config) buildInitSql() []byte {
362+
func (cfg *Config) buildInitSql(hasInit bool) []byte {
363363
sql := fmt.Sprintf(`SET @@SESSION.SQL_LOG_BIN=0;
364364
CREATE DATABASE IF NOT EXISTS %s;
365365
DROP user IF EXISTS 'root'@'127.0.0.1';
@@ -381,7 +381,7 @@ DROP user IF EXISTS '%s'@'%%';
381381
CREATE USER '%s'@'%%' IDENTIFIED BY '%s';
382382
GRANT ALL ON %s.* TO '%s'@'%%' ;
383383
FLUSH PRIVILEGES;
384-
RESET SLAVE ALL;
384+
385385
`,
386386
cfg.Database, //database
387387
cfg.RootPassword,
@@ -401,6 +401,10 @@ RESET SLAVE ALL;
401401
cfg.User, //drop user
402402
cfg.User, cfg.Password, //create user
403403
cfg.Database, cfg.User) //grant
404+
405+
if hasInit {
406+
sql += "\nRESET SLAVE ALL;\n"
407+
}
404408
return utils.StringToBytes(sql)
405409
}
406410

sidecar/init.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,9 @@ func runInitCommand(cfg *Config) error {
168168
if err = ioutil.WriteFile(initFilePath+"/reset.sql", []byte("reset master;"), 0644); err != nil {
169169
return fmt.Errorf("failed to write reset.sql: %s", err)
170170
}
171-
171+
hasInitialized, _ := checkIfPathExists(path.Join(dataPath, "mysql"))
172172
// build init.sql.
173173
initSqlPath := path.Join(extraConfPath, "init.sql")
174-
if err = ioutil.WriteFile(initSqlPath, cfg.buildInitSql(), 0644); err != nil {
175-
return fmt.Errorf("failed to write init.sql: %s", err)
176-
}
177174

178175
// build extra.cnf.
179176
extraConfig, err := cfg.buildExtraConfig(initSqlPath)
@@ -183,7 +180,7 @@ func runInitCommand(cfg *Config) error {
183180

184181
// Notice: plugin.cnf cannot be copied to /etc/mysql/conf.d when initialized.
185182
// Check /var/lib/mysql/mysql exists. if exists it means that been initialized.
186-
if exists, _ := checkIfPathExists(path.Join(dataPath, "mysql")); exists || strings.HasPrefix(getEnvValue("MYSQL_VERSION"), "5") {
183+
if hasInitialized || strings.HasPrefix(getEnvValue("MYSQL_VERSION"), "5") {
187184
// Save plugin.cnf and extra.cnf to /etc/mysql/conf.d.
188185
saveCnfTo(extraConfPath, extraConfig)
189186
} else {
@@ -228,7 +225,7 @@ func runInitCommand(cfg *Config) error {
228225
// Check datadir is empty.
229226
// if /var/lib/mysql/mysql is empty, then run the restore.
230227
// otherwise , it must be has data, then do nothing.
231-
if exists, _ := checkIfPathExists(dataPath + "/mysql"); !exists {
228+
if !hasInitialized {
232229
if len(cfg.XRestoreFrom) != 0 {
233230
var err_f error
234231
if cfg.CloneFlag {
@@ -241,10 +238,14 @@ func runInitCommand(cfg *Config) error {
241238
return fmt.Errorf("failed to restore from %s: %s", cfg.XRestoreFrom, err)
242239
}
243240
}
244-
241+
// Check has initialized again.
242+
hasInitialized, _ = checkIfPathExists(path.Join(dataPath, "mysql"))
245243
}
246244
}
247-
245+
// Build init.sql after restore
246+
if err = ioutil.WriteFile(initSqlPath, cfg.buildInitSql(hasInitialized), 0644); err != nil {
247+
return fmt.Errorf("failed to write init.sql: %s", err)
248+
}
248249
// build xenon.json.
249250
xenonFilePath := path.Join(xenonPath, "xenon.json")
250251
if err = ioutil.WriteFile(xenonFilePath, cfg.buildXenonConf(), 0644); err != nil {

0 commit comments

Comments
 (0)