...
1# MySQL
2
3`mysql://user:password@tcp(host:port)/dbname?query`
4
5| URL Query | WithInstance Config | Description |
6|------------|---------------------|-------------|
7| `x-migrations-table` | `MigrationsTable` | Name of the migrations table |
8| `x-no-lock` | `NoLock` | Set to `true` to skip `GET_LOCK`/`RELEASE_LOCK` statements. Useful for [multi-master MySQL flavors](https://www.percona.com/doc/percona-xtradb-cluster/LATEST/features/pxc-strict-mode.html#explicit-table-locking). Only run migrations from one host when this is enabled. |
9| `dbname` | `DatabaseName` | The name of the database to connect to |
10| `user` | | The user to sign in as |
11| `password` | | The user's password |
12| `host` | | The host to connect to. |
13| `port` | | The port to bind to. |
14| `tls` | | TLS / SSL encrypted connection parameter; see [go-sql-driver](https://github.com/go-sql-driver/mysql#tls). Use any name (e.g. `migrate`) if you want to use a custom TLS config (`x-tls-` queries). |
15| `x-tls-ca` | | The location of the CA (certificate authority) file. |
16| `x-tls-cert` | | The location of the client certicicate file. Must be used with `x-tls-key`. |
17| `x-tls-key` | | The location of the private key file. Must be used with `x-tls-cert`. |
18| `x-tls-insecure-skip-verify` | | Whether or not to use SSL (true\|false) |
19
20## Use with existing client
21
22If you use the MySQL driver with existing database client, you must create the client with parameter `multiStatements=true`:
23
24```go
25package main
26
27import (
28 "database/sql"
29
30 _ "github.com/go-sql-driver/mysql"
31 "github.com/golang-migrate/migrate"
32 "github.com/golang-migrate/migrate/database/mysql"
33 _ "github.com/golang-migrate/migrate/source/file"
34)
35
36func main() {
37 db, _ := sql.Open("mysql", "user:password@tcp(host:port)/dbname?multiStatements=true")
38 driver, _ := mysql.WithInstance(db, &mysql.Config{})
39 m, _ := migrate.NewWithDatabaseInstance(
40 "file:///migrations",
41 "mysql",
42 driver,
43 )
44
45 m.Steps(2)
46}
47```
48
49## Upgrading from v1
50
511. Write down the current migration version from schema_migrations
521. `DROP TABLE schema_migrations`
532. Wrap your existing migrations in transactions ([BEGIN/COMMIT](https://dev.mysql.com/doc/refman/5.7/en/commit.html)) if you use multiple statements within one migration.
543. Download and install the latest migrate version.
554. Force the current migration version with `migrate force <current_version>`.
View as plain text