diff --git a/pkg/apis/pingcap/v1alpha1/tidbcluster.go b/pkg/apis/pingcap/v1alpha1/tidbcluster.go index 030408af7c9..6d05bd2e123 100644 --- a/pkg/apis/pingcap/v1alpha1/tidbcluster.go +++ b/pkg/apis/pingcap/v1alpha1/tidbcluster.go @@ -733,7 +733,7 @@ func (tc *TidbCluster) IsTLSClusterEnabled() bool { } func (tc *TidbCluster) NeedToSyncTiDBInitializer() bool { - return tc.Spec.TiDB != nil && tc.Spec.TiDB.Initializer != nil && tc.Spec.TiDB.Initializer.CreatePassword && !tc.Status.TiDB.PasswordInitialized + return tc.Spec.TiDB != nil && tc.Spec.TiDB.Initializer != nil && tc.Spec.TiDB.Initializer.CreatePassword && tc.Status.TiDB.PasswordInitialized == nil } func (tc *TidbCluster) Scheme() string { diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index 591e9d62967..5229a61cedb 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -1142,7 +1142,7 @@ type TiDBStatus struct { FailureMembers map[string]TiDBFailureMember `json:"failureMembers,omitempty"` ResignDDLOwnerRetryCount int32 `json:"resignDDLOwnerRetryCount,omitempty"` Image string `json:"image,omitempty"` - PasswordInitialized bool `json:"passwordInitialized,omitempty"` + PasswordInitialized *bool `json:"passwordInitialized,omitempty"` } // TiDBMember is TiDB member diff --git a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go index 91281b1f8a9..9c475dfdadb 100644 --- a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go @@ -5630,6 +5630,11 @@ func (in *TiDBStatus) DeepCopyInto(out *TiDBStatus) { (*out)[key] = *val.DeepCopy() } } + if in.PasswordInitialized != nil { + in, out := &in.PasswordInitialized, &out.PasswordInitialized + *out = new(bool) + **out = **in + } return } diff --git a/pkg/manager/member/tidb_member_manager.go b/pkg/manager/member/tidb_member_manager.go index f174eab3c03..c45acc1d85a 100644 --- a/pkg/manager/member/tidb_member_manager.go +++ b/pkg/manager/member/tidb_member_manager.go @@ -309,6 +309,12 @@ func (m *tidbMemberManager) syncInitializer(tc *v1alpha1.TidbCluster) { db, err = util.OpenDB(ctx, dsn) if err != nil { + if strings.Contains(fmt.Sprint(err), "Access denied") { + klog.Errorf("Can't connect to the TiDB service of the TiDB cluster [%s:%s], error: %s", ns, tcName, err) + val := false + tc.Status.TiDB.PasswordInitialized = &val + return + } if ctx.Err() != nil { klog.Errorf("Can't connect to the TiDB service of the TiDB cluster [%s:%s], error: %s, context error: %s", ns, tcName, err, ctx.Err()) } else { @@ -330,7 +336,8 @@ func (m *tidbMemberManager) syncInitializer(tc *v1alpha1.TidbCluster) { klog.Errorf("Fail to set TiDB password for TiDB cluster %s/%s, err: %s", ns, tcName, err) return } - tc.Status.TiDB.PasswordInitialized = true + val := true + tc.Status.TiDB.PasswordInitialized = &val klog.Infof("Set password successfully for TiDB cluster %s/%s", ns, tcName) } } diff --git a/pkg/util/util.go b/pkg/util/util.go index 51caf5ba0bc..6961f60f5ed 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -454,7 +454,7 @@ func OpenDB(ctx context.Context, dsn string) (*sql.DB, error) { } if err := db.PingContext(ctx); err != nil { db.Close() - return nil, fmt.Errorf("cannot connect to mysql, err: %v", err) + return nil, fmt.Errorf("cannot connect to tidb cluster, err: %v", err) } return db, nil }