Skip to content

Commit

Permalink
WALG-1665. User proper DSNs in MySQL documentation & validate DSN for…
Browse files Browse the repository at this point in the history
…mat it earlier (#1692)
  • Loading branch information
ostinru committed May 4, 2024
1 parent 5675ab0 commit a0749bc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
8 changes: 4 additions & 4 deletions docs/MySQL.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Configuration

* `WALG_MYSQL_DATASOURCE_NAME`

To configure the connection string for MySQL. Required. Format ```user:password@host/dbname```
To configure the connection string for MySQL. Required. [DSN format](https://github.com/go-sql-driver/mysql#dsn-data-source-name) ```user:password@tcp(host)/dbname```

* `WALG_MYSQL_SSL_CA`

Expand Down Expand Up @@ -63,7 +63,7 @@ To configure the server id of the binlog server. Should be unique for each repli

* `WALG_MYSQL_BINLOG_SERVER_REPLICA_SOURCE`

To configure the connection string that will be used by `binlog-server` to connect to your MySQL. [DSN format](https://github.com/go-sql-driver/mysql#dsn-data-source-name): ```user:password@host/dbname```
To configure the connection string that will be used by `binlog-server` to connect to your MySQL. [DSN format](https://github.com/go-sql-driver/mysql#dsn-data-source-name): ```user:password@tcp(host)/dbname```

> **Operations with binlogs**: If you'd like to do binlog operations with wal-g don't forget to [activate the binary log](https://mariadb.com/kb/en/activating-the-binary-log/) by starting mysql/mariadb with [--log-bin](https://mariadb.com/kb/en/replication-and-binary-log-server-system-variables/#log_bin) and [--log-basename](https://mariadb.com/kb/en/mysqld-options/#-log-basename)=\[name\].
Expand Down Expand Up @@ -256,7 +256,7 @@ In that case MySQL backup is a plain SQL script.
Here's typical wal-g configuration for that case:

```bash
WALG_MYSQL_DATASOURCE_NAME=user:pass@localhost/mysql
WALG_MYSQL_DATASOURCE_NAME=user:pass@tcp(localhost)/mysql
WALG_STREAM_CREATE_COMMAND="mysqldump --all-databases --single-transaction --set-gtid-purged=ON"
WALG_STREAM_RESTORE_COMMAND="mysql"
WALG_MYSQL_BINLOG_REPLAY_COMMAND='mysqlbinlog --stop-datetime="$WALG_MYSQL_BINLOG_END_TS" "$WALG_MYSQL_CURRENT_BINLOG" | mysql'
Expand All @@ -277,7 +277,7 @@ wal-g can work as replication source to do fast PiTR. In this case it will serve
WALG_MYSQL_BINLOG_SERVER_PASSWORD="walgpwd"
WALG_MYSQL_BINLOG_SERVER_ID=99

WALG_MYSQL_BINLOG_SERVER_REPLICA_SOURCE="user:password@127.0.0.1:3306/db"
WALG_MYSQL_BINLOG_SERVER_REPLICA_SOURCE="user:password@tcp(127.0.0.1:3306)/db"
```

Restore procedure is straightforward:
Expand Down
8 changes: 8 additions & 0 deletions internal/databases/mysql/binlog_server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/replication"
"github.com/go-mysql-org/go-mysql/server"
mysqldriver "github.com/go-sql-driver/mysql"
"github.com/google/uuid"
"github.com/wal-g/tracelog"

"github.com/wal-g/wal-g/internal"
conf "github.com/wal-g/wal-g/internal/config"
"github.com/wal-g/wal-g/pkg/storages/storage"
Expand Down Expand Up @@ -264,6 +266,12 @@ func HandleBinlogServer(since string, until string) {
startTS, untilTS, _, err = getTimestamps(st.RootFolder(), since, until, "")
tracelog.ErrorLogger.FatalOnError(err)

// validate WALG_MYSQL_BINLOG_SERVER_REPLICA_SOURCE
replicaSource, err := conf.GetRequiredSetting(conf.MysqlBinlogServerReplicaSource)
tracelog.ErrorLogger.FatalOnError(err)
_, err = mysqldriver.ParseDSN(replicaSource)
tracelog.ErrorLogger.FatalOnError(err)

tracelog.InfoLogger.Printf("Starting binlog server")

serverAddress, err := conf.GetRequiredSetting(conf.MysqlBinlogServerHost)
Expand Down
9 changes: 7 additions & 2 deletions internal/databases/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
"github.com/wal-g/wal-g/internal/compression"

gomysql "github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-sql-driver/mysql"
mysqldriver "github.com/go-sql-driver/mysql"
"github.com/wal-g/tracelog"

"github.com/wal-g/wal-g/internal"
conf "github.com/wal-g/wal-g/internal/config"
"github.com/wal-g/wal-g/pkg/storages/storage"
Expand Down Expand Up @@ -172,7 +173,7 @@ func getMySQLConnectionFromDatasource(datasourceName string) (*sql.DB, error) {
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
return nil, fmt.Errorf("failed to load certificate from %s", caFile)
}
err = mysql.RegisterTLSConfig("custom", &tls.Config{
err = mysqldriver.RegisterTLSConfig("custom", &tls.Config{
RootCAs: rootCertPool,
})
if err != nil {
Expand All @@ -189,6 +190,10 @@ func getMySQLConnectionFromDatasource(datasourceName string) (*sql.DB, error) {
datasourceName += "?tls=custom"
}
}
_, err := mysqldriver.ParseDSN(datasourceName)
if err != nil {
return nil, err
}
db, err := sql.Open("mysql", datasourceName)
return db, err
}
Expand Down

0 comments on commit a0749bc

Please sign in to comment.