func CreateRootUserIfNotExist()

in pkg/mysqlclient/root_user.go [30:67]


func CreateRootUserIfNotExist(mysqldSfset *appsv1.StatefulSet, rootHost, rootPassword string, ndbOperatorPassword string) error {

	db, err := ConnectToStatefulSet(mysqldSfset, DbMySQL, ndbOperatorPassword)
	if err != nil {
		return err
	}

	if exists, err := rootUserExists(db, rootHost); err != nil {
		return err
	} else if exists {
		return nil
	}

	// The root user with given host does not exist.
	// So, create user in database
	klog.Infof("Creating the root user with host = %s", rootHost)
	query := fmt.Sprintf("create user 'root'@'%s' identified by '%s'", rootHost, rootPassword)
	_, err = db.Exec(query)
	if err != nil {
		klog.Infof("Error executing %s: %s", query, err.Error())
		return err
	}

	query = fmt.Sprintf("grant all on *.* to 'root'@'%s' with grant option", rootHost)
	_, err = db.Exec(query)
	if err != nil {
		klog.Infof("Error executing %s: %s", query, err.Error())
		return err
	}

	_, err = db.Exec("flush privileges")
	if err != nil {
		klog.Infof("Error executing flush privileges: %s", err.Error())
		return err
	}

	return nil
}