-
Notifications
You must be signed in to change notification settings - Fork 193
/
binlog.go
174 lines (150 loc) · 5.33 KB
/
binlog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package check
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/pingcap/tidb-tools/pkg/dbutil"
)
// MySQLBinlogEnableChecker checks whether `log_bin` variable is enabled in MySQL.
type MySQLBinlogEnableChecker struct {
db *sql.DB
dbinfo *dbutil.DBConfig
}
// NewMySQLBinlogEnableChecker returns a Checker.
func NewMySQLBinlogEnableChecker(db *sql.DB, dbinfo *dbutil.DBConfig) Checker {
return &MySQLBinlogEnableChecker{db: db, dbinfo: dbinfo}
}
// Check implements the Checker interface.
func (pc *MySQLBinlogEnableChecker) Check(ctx context.Context) *Result {
result := &Result{
Name: pc.Name(),
Desc: "check whether mysql binlog is enabled",
State: StateFailure,
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
}
value, err := dbutil.ShowLogBin(ctx, pc.db)
if err != nil {
markCheckError(result, err)
return result
}
if strings.ToUpper(value) != "ON" {
result.ErrorMsg = fmt.Sprintf("log_bin is %s, and should be ON", value)
result.Instruction = "ref document: https://dev.mysql.com/doc/refman/5.7/en/replication-howto-masterbaseconfig.html"
return result
}
result.State = StateSuccess
return result
}
// Name implements the Checker interface.
func (pc *MySQLBinlogEnableChecker) Name() string {
return "mysql_binlog_enable"
}
/*****************************************************/
// MySQLBinlogFormatChecker checks mysql binlog_format.
type MySQLBinlogFormatChecker struct {
db *sql.DB
dbinfo *dbutil.DBConfig
}
// NewMySQLBinlogFormatChecker returns a Checker.
func NewMySQLBinlogFormatChecker(db *sql.DB, dbinfo *dbutil.DBConfig) Checker {
return &MySQLBinlogFormatChecker{db: db, dbinfo: dbinfo}
}
// Check implements the Checker interface.
func (pc *MySQLBinlogFormatChecker) Check(ctx context.Context) *Result {
result := &Result{
Name: pc.Name(),
Desc: "check whether mysql binlog_format is ROW",
State: StateFailure,
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
}
value, err := dbutil.ShowBinlogFormat(ctx, pc.db)
if err != nil {
markCheckError(result, err)
return result
}
if strings.ToUpper(value) != "ROW" {
result.ErrorMsg = fmt.Sprintf("binlog_format is %s, and should be ROW", value)
result.Instruction = "please execute 'set global binlog_format=ROW;'"
return result
}
result.State = StateSuccess
return result
}
// Name implements the Checker interface.
func (pc *MySQLBinlogFormatChecker) Name() string {
return "mysql_binlog_format"
}
/*****************************************************/
var (
mysqlBinlogRowImageRequired MySQLVersion = [3]uint{5, 6, 2}
mariadbBinlogRowImageRequired MySQLVersion = [3]uint{10, 1, 6}
)
// MySQLBinlogRowImageChecker checks mysql binlog_row_image
type MySQLBinlogRowImageChecker struct {
db *sql.DB
dbinfo *dbutil.DBConfig
}
// NewMySQLBinlogRowImageChecker returns a Checker
func NewMySQLBinlogRowImageChecker(db *sql.DB, dbinfo *dbutil.DBConfig) Checker {
return &MySQLBinlogRowImageChecker{db: db, dbinfo: dbinfo}
}
// Check implements the Checker interface.
// 'binlog_row_image' is introduced since mysql 5.6.2, and mariadb 10.1.6.
// > In MySQL 5.5 and earlier, full row images are always used for both before images and after images.
// So we need check 'binlog_row_image' after mysql 5.6.2 version and mariadb 10.1.6.
// ref:
// - https://dev.mysql.com/doc/refman/5.6/en/replication-options-binary-log.html#sysvar_binlog_row_image
// - https://mariadb.com/kb/en/library/replication-and-binary-log-server-system-variables/#binlog_row_image
func (pc *MySQLBinlogRowImageChecker) Check(ctx context.Context) *Result {
result := &Result{
Name: pc.Name(),
Desc: "check whether mysql binlog_row_image is FULL",
State: StateFailure,
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
}
// check version firstly
value, err := dbutil.ShowVersion(ctx, pc.db)
if err != nil {
markCheckError(result, err)
return result
}
version, err := toMySQLVersion(value)
if err != nil {
markCheckError(result, err)
return result
}
// for mysql.version < 5.6.2 || mariadb.version < 10.1.6, we don't need to check binlog_row_image.
if (!IsMariaDB(value) && !version.Ge(mysqlBinlogRowImageRequired)) || (IsMariaDB(value) && !version.Ge(mariadbBinlogRowImageRequired)) {
result.State = StateSuccess
return result
}
value, err = dbutil.ShowBinlogRowImage(ctx, pc.db)
if err != nil {
markCheckError(result, err)
return result
}
if strings.ToUpper(value) != "FULL" {
result.ErrorMsg = fmt.Sprintf("binlog_row_image is %s, and should be FULL", value)
result.Instruction = "please execute 'set global binlog_row_image = FULL;'"
return result
}
result.State = StateSuccess
return result
}
// Name implements the Checker interface.
func (pc *MySQLBinlogRowImageChecker) Name() string {
return "mysql_binlog_row_image"
}