Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"unexpected EOF" when connecting to MySQL DB via cloud-sql-proxy in github actions #1040

Open
pidgezero-one opened this issue Feb 21, 2024 · 0 comments

Comments

@pidgezero-one
Copy link

Describe the Bug
I'm receiving the following error message when connecting to a Cloud SQL instance with a MySQL driver in a Github Action using cloud-sql-proxy:

[mysql] 2024/02/21 02:53:14 packets.go:37: unexpected EOF
[mysql] 2024/02/21 02:53:14 packets.go:37: unexpected EOF
[mysql] 2024/02/21 02:53:14 packets.go:37: unexpected EOF
panic: driver: bad connection

Steps to Reproduce
These are the relevant steps of my GHA YML that run whenever my main branch receives a push:

    steps:
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'

      - name: Checkout
        uses: actions/checkout@v3

      # Setup gcloud CLI
      - uses: google-github-actions/setup-gcloud@v0
        with:
          service_account_key: ${{ secrets.AUTH_KEY }}
          project_id: ${{ env.PROJECT_ID }}
          export_default_credentials: true

      - name: Run database migrations
        run: |
          make proxy_db
          make migration

These are the definitions of make proxy_db and make migration in my makefile:

proxy_db:
	@curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.7.0/cloud-sql-proxy.linux.amd64
	@chmod +x cloud-sql-proxy
	@nohup ./cloud-sql-proxy --credentials-file $(GOOGLE_APPLICATION_CREDENTIALS) --address 0.0.0.0 --port 3306 $(CLOUD_SQL_INSTANCE_NAME) > cloud-sql-proxy.out 2> cloud-sql-proxy.err < /dev/null &
	@echo $!
	@echo CLOUD_SQL_PROXY_PID=$(echo $!) >> $GITHUB_ENV
	@./scripts/wait/wait-for-it.sh 127.0.0.1:3306 -s -t 10 -- echo "Cloud SQL Proxy is running"
migration:
	@go run cmd/migration/main.go

The contents of main.go are as follows:

package main

import (
	"database/sql"
	"fmt"
	"time"

	_ "github.com/GoogleCloudPlatform/berglas/pkg/auto"
	"github.com/golang-migrate/migrate/v4"
	"github.com/golang-migrate/migrate/v4/database/mysql"
	_ "github.com/golang-migrate/migrate/v4/database/mysql"
	_ "github.com/golang-migrate/migrate/v4/source/file"
	"github.com/kelseyhightower/envconfig"
)

type variables struct {
	MySQLProxyPort int    `required:"false" envconfig:"mysql_proxy_port"`
	MySQLDB        string `required:"true" envconfig:"mysql_db"`
	MySQLUser      string `required:"true" envconfig:"mysql_user"`
	MySQLPass      string `required:"true" envconfig:"mysql_pass"`
	Env            string `envconfig:"build_env"`
}

const migrationsPath = "scripts/db/migrations"

func main() {
	var v variables
	envconfig.MustProcess("db migration", &v)

	db, err := sql.Open("mysql", getDbSource(v))
	db.SetConnMaxLifetime(time.Minute * 5)
	source := fmt.Sprintf("file://%s", migrationsPath)
	driver, err := mysql.WithInstance(db, &mysql.Config{DatabaseName: v.MySQLDB, StatementTimeout: 300 * time.Second})
	if err != nil {
		panic(err) // <-- this is where it's failing
	}
	m, err := migrate.NewWithDatabaseInstance(source, v.MySQLDB, driver)
	if err != nil {
		panic(err)
	}

	fmt.Println("Running migrations...")
	if err := m.Up(); err != nil {
		if err == migrate.ErrNoChange {
			fmt.Println("No migrations to run.")
			return
		}

		panic(err)
	}
	fmt.Println("Migrations ran successfully.")
}

func getDbSource(v variables) string {
	return fmt.Sprintf(
		"%s:%s@tcp(localhost:%v)/%s?multiStatements=true",
		v.MySQLUser,
		v.MySQLPass,
		v.MySQLProxyPort,
		v.MySQLDB,
	)
}

func getDbUrl(v variables) string {
	return fmt.Sprintf(
		"mysql://%s", getDbSource(v),
	)
}

All of the environment variables and secrets used above are being parsed correctly, and the wait-for-it script indicates that the DB proxy is ready before the action runner tries to use migration.go.

This happens whether or not I include tls=true.

This happens every time I push a new commit to my main branch, kicking off the action.

Expected Behavior
I would expect my migrations to succeed without an EOF error.

Migrate Version
4.17.0

Loaded Source Drivers
I'm not sure how to find this, because I'm using migrate as an import, not as a command line tool.

Loaded Database Drivers
mysql (github.com/go-sql-driver/mysql v1.7.1)

Go Version
1.21

Stacktrace

[mysql] 2024/02/21 02:53:14 packets.go:37: unexpected EOF
[mysql] 2024/02/21 02:53:14 packets.go:37: unexpected EOF
[mysql] 2024/02/21 02:53:14 packets.go:37: unexpected EOF
panic: driver: bad connection
goroutine 1 [running]:
main.main()
	/home/runner/work/.../cmd/migration/main.go:35 +0x4e5
exit status 2
make: *** [makefile:14: migration] Error 1
Error: Process completed with exit code 2.

Additional context
Add any other context about the problem here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant