Skip to content

Commit b54a488

Browse files
committed
cluster,config,sidecar,utils: support the MySQL8.0 #176
1 parent acaf8f0 commit b54a488

File tree

7 files changed

+151
-82
lines changed

7 files changed

+151
-82
lines changed

cluster/cluster.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ func (c *Cluster) Validate() error {
6363
if utils.StringInArray(c.Spec.MysqlOpts.User, []string{"root", utils.ReplicationUser, utils.OperatorUser, utils.MetricsUser}) {
6464
return fmt.Errorf("spec.mysqlOpts.user cannot be root|%s|%s|%s", utils.ReplicationUser, utils.OperatorUser, utils.MetricsUser)
6565
}
66-
66+
// MySQL8 do not support TokuDB
67+
// https://www.percona.com/blog/2021/05/21/tokudb-support-changes-and-future-removal-from-percona-server-for-mysql-8-0/
68+
if c.Spec.MysqlVersion == "8.0" && c.Spec.MysqlOpts.InitTokuDB {
69+
return fmt.Errorf("TokuDB is not supported in MySQL 8.0 any more, the value in Cluster.spec.mysqlOpts.initTokuDB should be set false")
70+
}
6771
// https://github.com/percona/percona-docker/blob/main/percona-server-5.7/ps-entry.sh#L159
6872
// ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'127.0.0.1'.
6973
if c.Spec.MysqlOpts.RootHost == "127.0.0.1" {

cluster/syncer/config_map.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ func NewConfigMapSyncer(cli client.Client, c *cluster.Cluster) syncer.Interface
5050
if err != nil {
5151
return fmt.Errorf("failed to create mysql configs: %s", err)
5252
}
53-
53+
data_special, err := buildSpecialMysqlConf(c)
5454
cm.Data = map[string]string{
55-
"my.cnf": data,
55+
"my.cnf": data,
56+
"special.cnf": data_special,
5657
}
5758

5859
return nil
@@ -65,6 +66,10 @@ func buildMysqlConf(c *cluster.Cluster) (string, error) {
6566
sec := cfg.Section("mysqld")
6667

6768
c.EnsureMysqlConf()
69+
if c.Spec.MysqlVersion == "8.0" {
70+
delete(mysqlCommonConfigs, "query_cache_size")
71+
delete(mysqlStaticConfigs, "query_cache_type")
72+
}
6873

6974
addKVConfigsToSection(sec, mysqlSysConfigs, mysqlCommonConfigs, mysqlStaticConfigs, c.Spec.MysqlOpts.MysqlConf)
7075

@@ -86,6 +91,20 @@ func buildMysqlConf(c *cluster.Cluster) (string, error) {
8691
return data, nil
8792
}
8893

94+
// Build the Special Cnf file.
95+
func buildSpecialMysqlConf(c *cluster.Cluster) (string, error) {
96+
cfg := ini.Empty(ini.LoadOptions{IgnoreInlineComment: true})
97+
sec := cfg.Section("mysqld")
98+
99+
addKVConfigsToSection(sec, mysqlSpecialConfig)
100+
data, err := writeConfigs(cfg)
101+
if err != nil {
102+
return "", err
103+
}
104+
105+
return data, nil
106+
}
107+
89108
// addKVConfigsToSection add a map[string]string to a ini.Section
90109
func addKVConfigsToSection(s *ini.Section, extraMysqld ...map[string]string) {
91110
for _, extra := range extraMysqld {

cluster/syncer/variables.go

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,75 +27,82 @@ var log = logf.Log.WithName("cluster.syncer")
2727

2828
// mysqlSysConfigs is the map of mysql system configs.
2929
var mysqlSysConfigs = map[string]string{
30-
"default-time-zone": "+08:00",
31-
"slow_query_log_file": "/var/log/mysql/mysql-slow.log",
32-
"read_only": "ON",
33-
"binlog_format": "row",
34-
"plugin-load": "\"semisync_master.so;semisync_slave.so;audit_log.so;connection_control.so\"",
35-
"log-bin": "/var/lib/mysql/mysql-bin",
36-
"log-timestamps": "SYSTEM",
37-
"innodb_open_files": "655360",
38-
"open_files_limit": "655360",
39-
"rpl_semi_sync_master_enabled": "OFF",
40-
"rpl_semi_sync_slave_enabled": "ON",
41-
"rpl_semi_sync_master_wait_no_slave": "ON",
42-
"rpl_semi_sync_master_timeout": "1000000000000000000",
43-
"gtid-mode": "ON",
44-
"enforce-gtid-consistency": "ON",
45-
"slave_parallel_type": "LOGICAL_CLOCK",
46-
"relay_log": "/var/lib/mysql/mysql-relay-bin",
47-
"relay_log_index": "/var/lib/mysql/mysql-relay-bin.index",
48-
"master_info_repository": "TABLE",
49-
"relay_log_info_repository": "TABLE",
50-
"slow_query_log": "1",
51-
"tmp_table_size": "32M",
52-
"tmpdir": "/var/lib/mysql",
53-
"audit_log_file": "/var/log/mysql/mysql-audit.log",
54-
"audit_log_exclude_accounts": "\"root@localhost,root@127.0.0.1," + utils.ReplicationUser + "@%," + utils.MetricsUser + "@%\"",
55-
"audit_log_buffer_size": "16M",
30+
"default-time-zone": "+08:00",
31+
"slow_query_log_file": "/var/log/mysql/mysql-slow.log",
32+
"read_only": "ON",
33+
"binlog_format": "row",
34+
"log-bin": "/var/lib/mysql/mysql-bin",
35+
"log-timestamps": "SYSTEM",
36+
"innodb_open_files": "655360",
37+
"open_files_limit": "655360",
38+
39+
"gtid-mode": "ON",
40+
"enforce-gtid-consistency": "ON",
41+
"slave_parallel_type": "LOGICAL_CLOCK",
42+
"relay_log": "/var/lib/mysql/mysql-relay-bin",
43+
"relay_log_index": "/var/lib/mysql/mysql-relay-bin.index",
44+
"master_info_repository": "TABLE",
45+
"relay_log_info_repository": "TABLE",
46+
"slow_query_log": "1",
47+
"tmp_table_size": "32M",
48+
"tmpdir": "/var/lib/mysql",
5649
}
5750

58-
// mysqlCommonConfigs is the map of the mysql common configs.
59-
var mysqlCommonConfigs = map[string]string{
60-
"character_set_server": "utf8mb4",
61-
"interactive_timeout": "3600",
62-
"default-time-zone": "+08:00",
63-
"expire_logs_days": "7",
64-
"key_buffer_size": "33554432",
65-
"log_bin_trust_function_creators": "1",
66-
"long_query_time": "3",
67-
"binlog_cache_size": "32768",
68-
"binlog_stmt_cache_size": "32768",
69-
"max_connections": "1024",
70-
"max_connect_errors": "655360",
71-
"query_cache_size": "0",
72-
"sync_master_info": "1000",
73-
"sync_relay_log": "1000",
74-
"sync_relay_log_info": "1000",
75-
"table_open_cache": "2000",
76-
"thread_cache_size": "128",
77-
"wait_timeout": "3600",
78-
"group_concat_max_len": "1024",
79-
"slave_rows_search_algorithms": "INDEX_SCAN,HASH_SCAN",
80-
"max_allowed_packet": "1073741824",
81-
"event_scheduler": "OFF",
82-
"innodb_print_all_deadlocks": "0",
83-
"autocommit": "1",
84-
"transaction-isolation": "READ-COMMITTED",
51+
var mysqlSpecialConfig = map[string]string{
52+
"plugin-load": "\"semisync_master.so;semisync_slave.so;audit_log.so;connection_control.so\"",
53+
"audit_log_file": "/var/log/mysql/mysql-audit.log",
54+
"audit_log_exclude_accounts": "\"root@localhost,root@127.0.0.1," + utils.ReplicationUser + "@%," + utils.MetricsUser + "@%\"",
55+
"audit_log_buffer_size": "16M",
56+
"rpl_semi_sync_master_enabled": "OFF",
57+
"rpl_semi_sync_slave_enabled": "ON",
58+
"rpl_semi_sync_master_wait_no_slave": "ON",
59+
"rpl_semi_sync_master_timeout": "1000000000000000000",
60+
"audit-log": "ON",
8561
"audit_log_policy": "NONE",
8662
"audit_log_rotate_on_size": "104857600",
8763
"audit_log_rotations": "6",
64+
"audit_log_format": "OLD",
8865
"connection_control_failed_connections_threshold": "3",
8966
"connection_control_min_connection_delay": "1000",
9067
"connection_control_max_connection_delay": "2147483647",
91-
"explicit_defaults_for_timestamp": "0",
92-
"innodb_adaptive_hash_index": "0",
68+
}
69+
70+
// mysqlCommonConfigs is the map of the mysql common configs.
71+
var mysqlCommonConfigs = map[string]string{
72+
"character_set_server": "utf8mb4",
73+
"interactive_timeout": "3600",
74+
"default-time-zone": "+08:00",
75+
"expire_logs_days": "7",
76+
"key_buffer_size": "33554432",
77+
"log_bin_trust_function_creators": "1",
78+
"long_query_time": "3",
79+
"binlog_cache_size": "32768",
80+
"binlog_stmt_cache_size": "32768",
81+
"max_connections": "1024",
82+
"max_connect_errors": "655360",
83+
"query_cache_size": "0",
84+
"sync_master_info": "1000",
85+
"sync_relay_log": "1000",
86+
"sync_relay_log_info": "1000",
87+
"table_open_cache": "2000",
88+
"thread_cache_size": "128",
89+
"wait_timeout": "3600",
90+
"group_concat_max_len": "1024",
91+
"slave_rows_search_algorithms": "INDEX_SCAN,HASH_SCAN",
92+
"max_allowed_packet": "1073741824",
93+
"event_scheduler": "OFF",
94+
"innodb_print_all_deadlocks": "0",
95+
"autocommit": "1",
96+
"transaction-isolation": "READ-COMMITTED",
97+
98+
"explicit_defaults_for_timestamp": "0",
99+
"innodb_adaptive_hash_index": "0",
93100
}
94101

95102
// mysqlStaticConfigs is the map of the mysql static configs.
96103
// The mysql need restart, if modify the config.
97104
var mysqlStaticConfigs = map[string]string{
98-
"audit_log_format": "OLD",
105+
99106
"default-storage-engine": "InnoDB",
100107
"back_log": "2048",
101108
"ft_min_word_len": "4",

config/samples/mysql_v1alpha1_cluster.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: sample
55
spec:
66
replicas: 3
7-
mysqlVersion: "5.7"
7+
mysqlVersion: "8.0"
88

99
mysqlOpts:
1010
rootPassword: ""
@@ -55,7 +55,7 @@ spec:
5555

5656
podSpec:
5757
imagePullPolicy: IfNotPresent
58-
sidecarImage: radondb/mysql-sidecar:0.1
58+
sidecarImage: acekingke/sidecar:0.1
5959
busyboxImage: busybox:1.32
6060

6161
slowLogTail: false

sidecar/config.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,39 @@ func (cfg *Config) buildInitSql() []byte {
240240
sql := fmt.Sprintf(`SET @@SESSION.SQL_LOG_BIN=0;
241241
CREATE DATABASE IF NOT EXISTS %s;
242242
DROP user IF EXISTS 'root'@'127.0.0.1';
243-
GRANT ALL ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '%s' with grant option;
243+
CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY '%s';
244+
GRANT ALL ON *.* TO 'root'@'127.0.0.1' with grant option;
244245
DROP user IF EXISTS '%s'@'%%';
245-
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '%s'@'%%' IDENTIFIED BY '%s';
246+
CREATE USER '%s'@'%%' IDENTIFIED BY '%s';
247+
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '%s'@'%%';
246248
DROP user IF EXISTS '%s'@'%%';
247-
GRANT SELECT, PROCESS, REPLICATION CLIENT ON *.* TO '%s'@'%%' IDENTIFIED BY '%s';
249+
CREATE USER '%s'@'%%' IDENTIFIED BY '%s';
250+
GRANT SELECT, PROCESS, REPLICATION CLIENT ON *.* TO '%s'@'%%';
248251
DROP user IF EXISTS '%s'@'%%';
249-
GRANT SUPER, PROCESS, RELOAD, CREATE, SELECT ON *.* TO '%s'@'%%' IDENTIFIED BY '%s';
252+
CREATE USER '%s'@'%%' IDENTIFIED BY '%s';
253+
GRANT SUPER, PROCESS, RELOAD, CREATE, SELECT ON *.* TO '%s'@'%%';
250254
DROP user IF EXISTS '%s'@'%%';
251-
GRANT ALL ON %s.* TO '%s'@'%%' IDENTIFIED BY '%s';
255+
CREATE USER '%s'@'%%' IDENTIFIED BY '%s';
256+
GRANT ALL ON %s.* TO '%s'@'%%' ;
252257
FLUSH PRIVILEGES;
253-
`, cfg.Database, cfg.RootPassword, cfg.ReplicationUser, cfg.ReplicationUser, cfg.ReplicationPassword,
254-
cfg.MetricsUser, cfg.MetricsUser, cfg.MetricsPassword, cfg.OperatorUser, cfg.OperatorUser,
255-
cfg.OperatorPassword, cfg.User, cfg.Database, cfg.User, cfg.Password)
258+
`, cfg.Database, //database
259+
260+
cfg.RootPassword,
261+
cfg.ReplicationUser, //drop user
262+
cfg.ReplicationUser, cfg.ReplicationPassword, //create user
263+
cfg.ReplicationUser, //grant REPLICATION
264+
265+
cfg.MetricsUser, //drop user MetricsUser
266+
cfg.MetricsUser, cfg.MetricsPassword, //create user
267+
cfg.MetricsUser, //grant
268+
269+
cfg.OperatorUser, //drop user
270+
cfg.OperatorUser, cfg.OperatorPassword, //create
271+
cfg.OperatorUser, //grant
272+
273+
cfg.User, //drop user
274+
cfg.User, cfg.Password, //create user
275+
cfg.Database, cfg.User) //grant
256276

257277
return utils.StringToBytes(sql)
258278
}

sidecar/init.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,26 @@ func NewInitCommand(cfg *Config) *cobra.Command {
4747
// runInitCommand do some initialization operations.
4848
func runInitCommand(cfg *Config) error {
4949
var err error
50+
// Get the mysql user.
51+
user, err := user.Lookup("mysql")
52+
if err != nil {
53+
return fmt.Errorf("failed to get mysql user: %s", err)
54+
}
55+
uid, err := strconv.Atoi(user.Uid)
56+
if err != nil {
57+
return fmt.Errorf("failed to get mysql user uid: %s", err)
58+
}
59+
gid, err := strconv.Atoi(user.Gid)
60+
if err != nil {
61+
return fmt.Errorf("failed to get mysql user gid: %s", err)
62+
}
5063

5164
if exists, _ := checkIfPathExists(dataPath); exists {
5265
// remove lost+found.
5366
if err := os.RemoveAll(dataPath + "/lost+found"); err != nil {
5467
return fmt.Errorf("removing lost+found: %s", err)
5568
}
5669

57-
// Get the mysql user.
58-
user, err := user.Lookup("mysql")
59-
if err != nil {
60-
return fmt.Errorf("failed to get mysql user: %s", err)
61-
}
62-
uid, err := strconv.Atoi(user.Uid)
63-
if err != nil {
64-
return fmt.Errorf("failed to get mysql user uid: %s", err)
65-
}
66-
gid, err := strconv.Atoi(user.Gid)
67-
if err != nil {
68-
return fmt.Errorf("failed to get mysql user gid: %s", err)
69-
}
7070
// chown -R mysql:mysql /var/lib/mysql.
7171
if err = os.Chown(dataPath, uid, gid); err != nil {
7272
return fmt.Errorf("failed to chown %s: %s", dataPath, err)
@@ -93,7 +93,24 @@ func runInitCommand(cfg *Config) error {
9393
return fmt.Errorf("error mkdir %s: %s", extraConfPath, err)
9494
}
9595
}
96+
// chown -R mysql:mysql /var/lib/mysql.
97+
if err = os.Chown(extraConfPath, uid, gid); err != nil {
98+
return fmt.Errorf("failed to chown %s: %s", dataPath, err)
99+
}
96100

101+
if err = copyFile(path.Join(configMapPath, "special.cnf"), path.Join(initFilePath, "special.cnf")); err != nil {
102+
return fmt.Errorf("failed to copy special.cnf: %s", err)
103+
}
104+
105+
if err = os.Chmod(path.Join(initFilePath, "special.cnf"), 0755); err != nil {
106+
return fmt.Errorf("failed to chmod special.cnf : %s", err)
107+
}
108+
// Create shell `cp path.Join(initFilePath, "special.cnf") extraConfPath; chmod extraConfPath/special.cnf`
109+
special_sh := "cp " + path.Join(initFilePath, "special.cnf ") + extraConfPath + "; "
110+
special_sh += "chmod 755 " + extraConfPath + "/special.cnf"
111+
if err = ioutil.WriteFile(initFilePath+"/special.sh", []byte(special_sh), 0755); err != nil {
112+
return fmt.Errorf("failed to write special: %s", err)
113+
}
97114
// Run reset master in init-mysql container.
98115
if err = ioutil.WriteFile(initFilePath+"/reset.sql", []byte("reset master;"), 0644); err != nil {
99116
return fmt.Errorf("failed to write reset.sql: %s", err)

utils/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ var (
2323
// MySQLTagsToSemVer maps simple version to semver versions
2424
MySQLTagsToSemVer = map[string]string{
2525
"5.7": "5.7.33",
26+
"8.0": "8.0.25",
2627
}
2728

2829
// MysqlImageVersions is a map of supported mysql version and their image
2930
MysqlImageVersions = map[string]string{
3031
"5.7.33": "percona/percona-server:5.7.33",
32+
"8.0.25": "percona/percona-server:8.0.25",
3133
}
3234
)
3335

0 commit comments

Comments
 (0)