From 8bd42450de8af801c90ae8f4a544e775607aab05 Mon Sep 17 00:00:00 2001 From: ASAKURA Kazuki <32762324+Arthur1@users.noreply.github.com> Date: Mon, 7 Oct 2024 20:23:11 +0900 Subject: [PATCH 1/8] switch command to fetch replica status depending on mysql version --- lib/mysql.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/mysql.go b/lib/mysql.go index f88db9a..faf9b23 100644 --- a/lib/mysql.go +++ b/lib/mysql.go @@ -296,8 +296,14 @@ func fetchShowVariablesBackwardCompatibile(stat map[string]float64) error { } // This code does not work with multi-source replication. -func (m *MySQLPlugin) fetchShowSlaveStatus(db *sql.DB, stat map[string]float64) error { - rows, err := db.Query("show slave status") +func (m *MySQLPlugin) fetchShowReplicaStatus(db *sql.DB, stat map[string]float64, version [3]int) error { + command := "show replica status" + if version[0] < 8 || (version[0] == 8 && version[1] == 0 && version[2] < 22) { + // From MySQL 8.0.22, SHOW RELICA STATUS command is created and SHOW SLAVE STATUS command is deprecated. + // cf.) https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-22.html + command = "show slave status" + } + rows, err := db.Query(command) if err != nil { return fmt.Errorf("FetchMetrics (Slave Status): %w", err) } @@ -441,7 +447,7 @@ func (m *MySQLPlugin) FetchMetrics() (map[string]float64, error) { } } - err = m.fetchShowSlaveStatus(db, stat) + err = m.fetchShowReplicaStatus(db, stat, v) if err != nil { return nil, err } From 2523d0bccfe72e95826caf371c9807cd1f6de830 Mon Sep 17 00:00:00 2001 From: ASAKURA Kazuki <32762324+Arthur1@users.noreply.github.com> Date: Mon, 7 Oct 2024 20:29:37 +0900 Subject: [PATCH 2/8] replace deprecated grant replication slave command --- tests/read-replica-mysql8/sourcedb/init.d/init.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/read-replica-mysql8/sourcedb/init.d/init.sql b/tests/read-replica-mysql8/sourcedb/init.d/init.sql index 519c2ef..9d046ee 100644 --- a/tests/read-replica-mysql8/sourcedb/init.d/init.sql +++ b/tests/read-replica-mysql8/sourcedb/init.d/init.sql @@ -1,2 +1,2 @@ CREATE USER 'replica'@'%' IDENTIFIED BY 'replica'; -GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%'; +GRANT REPLICATION ON *.* TO 'replica'@'%'; From 3f35b7801a82abea11538a6c0a84df7209192cb5 Mon Sep 17 00:00:00 2001 From: ASAKURA Kazuki <32762324+Arthur1@users.noreply.github.com> Date: Mon, 7 Oct 2024 20:31:59 +0900 Subject: [PATCH 3/8] update README.md because current version does not work with mysql 8.4 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eaade09..eebb0ed 100644 --- a/README.md +++ b/README.md @@ -18,5 +18,5 @@ command = "/path/to/mackerel-plugin-mysql" ## Supported MySQL version -- `v1.1.0 >=` mysql 5.7, 8.0 and above +- `v1.1.0 >=` mysql 5.7, 8.0 - `v1.0.0` mysql 5.0, 5.1, 5.5, 5.6, 5.7, 8.0 From a529809773b7d5a86501bc8b8de14ff790aba49c Mon Sep 17 00:00:00 2001 From: ASAKURA Kazuki <32762324+Arthur1@users.noreply.github.com> Date: Mon, 7 Oct 2024 20:34:22 +0900 Subject: [PATCH 4/8] fix error messages containing slave word --- lib/mysql.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mysql.go b/lib/mysql.go index faf9b23..27ccea9 100644 --- a/lib/mysql.go +++ b/lib/mysql.go @@ -305,13 +305,13 @@ func (m *MySQLPlugin) fetchShowReplicaStatus(db *sql.DB, stat map[string]float64 } rows, err := db.Query(command) if err != nil { - return fmt.Errorf("FetchMetrics (Slave Status): %w", err) + return fmt.Errorf("FetchMetrics (Replica Status): %w", err) } defer rows.Close() for rows.Next() { columns, err := rows.ColumnTypes() if err != nil { - return fmt.Errorf("FetchMetrics (Slave Status): %w", err) + return fmt.Errorf("FetchMetrics (Replica Status): %w", err) } valuePtrs := make([]interface{}, len(columns)) @@ -321,7 +321,7 @@ func (m *MySQLPlugin) fetchShowReplicaStatus(db *sql.DB, stat map[string]float64 valuePtrs[i] = &values[i] } if err = rows.Scan(valuePtrs...); err != nil { - return fmt.Errorf("FetchMetrics (Slave Status): %w", err) + return fmt.Errorf("FetchMetrics (Replica Status): %w", err) } for i, column := range columns { variableName := column.Name() From d2b8ecb0fb747d674f0f3edcd6277a6a9cdaf552 Mon Sep 17 00:00:00 2001 From: ASAKURA Kazuki <32762324+Arthur1@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:17:01 +0900 Subject: [PATCH 5/8] Revert "replace deprecated grant replication slave command" This reverts commit 2523d0bccfe72e95826caf371c9807cd1f6de830. --- tests/read-replica-mysql8/sourcedb/init.d/init.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/read-replica-mysql8/sourcedb/init.d/init.sql b/tests/read-replica-mysql8/sourcedb/init.d/init.sql index 9d046ee..519c2ef 100644 --- a/tests/read-replica-mysql8/sourcedb/init.d/init.sql +++ b/tests/read-replica-mysql8/sourcedb/init.d/init.sql @@ -1,2 +1,2 @@ CREATE USER 'replica'@'%' IDENTIFIED BY 'replica'; -GRANT REPLICATION ON *.* TO 'replica'@'%'; +GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%'; From 94222ae1ab441f0df022b3c117346c8ada99b608 Mon Sep 17 00:00:00 2001 From: ASAKURA Kazuki <32762324+Arthur1@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:18:01 +0900 Subject: [PATCH 6/8] change column name for Seconds_Behind metric --- lib/mysql.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/mysql.go b/lib/mysql.go index 27ccea9..3565298 100644 --- a/lib/mysql.go +++ b/lib/mysql.go @@ -298,10 +298,12 @@ func fetchShowVariablesBackwardCompatibile(stat map[string]float64) error { // This code does not work with multi-source replication. func (m *MySQLPlugin) fetchShowReplicaStatus(db *sql.DB, stat map[string]float64, version [3]int) error { command := "show replica status" + secondsBehindSourceColumn := "Seconds_Behind_Source" if version[0] < 8 || (version[0] == 8 && version[1] == 0 && version[2] < 22) { // From MySQL 8.0.22, SHOW RELICA STATUS command is created and SHOW SLAVE STATUS command is deprecated. // cf.) https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-22.html command = "show slave status" + secondsBehindSourceColumn = "Seconds_Behind_Master" } rows, err := db.Query(command) if err != nil { @@ -326,7 +328,7 @@ func (m *MySQLPlugin) fetchShowReplicaStatus(db *sql.DB, stat map[string]float64 for i, column := range columns { variableName := column.Name() value := values[i] - if variableName == "Seconds_Behind_Master" { + if variableName == secondsBehindSourceColumn { if value != nil { f, err := atof(string(value)) if err != nil { From 3fc4abd8bdc8ee6537b99ade205b308e89dc47e4 Mon Sep 17 00:00:00 2001 From: ASAKURA Kazuki <32762324+Arthur1@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:36:11 +0900 Subject: [PATCH 7/8] --default-authentication-plugin=mysql_native_password option is deleted --- tests/extend-mysql8/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/extend-mysql8/test.sh b/tests/extend-mysql8/test.sh index 019c39f..045b601 100755 --- a/tests/extend-mysql8/test.sh +++ b/tests/extend-mysql8/test.sh @@ -25,7 +25,7 @@ docker run -d \ --name "test-$plugin" \ -p $port:3306 \ -e MYSQL_ROOT_PASSWORD=$password \ - "$image" --default-authentication-plugin=mysql_native_password + "$image" trap 'docker stop test-$plugin; docker rm test-$plugin; exit' 1 2 3 15 EXIT sleep 10 From 19150f57dd1aa9a13cc533f25349582aa652bec3 Mon Sep 17 00:00:00 2001 From: ASAKURA Kazuki <32762324+Arthur1@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:42:21 +0900 Subject: [PATCH 8/8] update README.md to support mysql 8.4 for next release --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index eebb0ed..57df4bd 100644 --- a/README.md +++ b/README.md @@ -18,5 +18,6 @@ command = "/path/to/mackerel-plugin-mysql" ## Supported MySQL version +- `v1.3.0 >=` mysql 5.7, 8.0, 8.4 and above - `v1.1.0 >=` mysql 5.7, 8.0 - `v1.0.0` mysql 5.0, 5.1, 5.5, 5.6, 5.7, 8.0