Search code examples
swiftvaporvapor-fluentfluent-mysql

MySQL & Vapor 3: unrecognized basic packet, full auth not supported


I want to connect a MySQL database on my machine with a Vapor 3 app.
My current configure.swift file looks as follows:

try services.register(FluentMySQLProvider())

...

let mysqlConfig = MySQLDatabaseConfig(
    username: "dev",
    password: "",
    database: "test"
)
let mysql = MySQLDatabase(config: mysqlConfig)

var databases = DatabasesConfig()
databases.add(database: mysql, as: .mysql)
services.register(databases)

This works just fine. However, since I need to add my Model to the migration configuration, I also need to add:

var migrations = MigrationConfig()
migrations.add(model: Posts.self, database: .mysql)
services.register(migrations)

When running the app this time, I'm seeing an error saying:

Full authentication not supported over insecure connections.

After some research, it seems that this error can be overcome by changing the password logic from caching_sha2_password to mysql_native_password.
However, that leaves me with the error saying:

Unrecognized basic packet.

How do I fix this?


Solution

  • From MySQL 8 if you want to use it on localhost (unsecured connection) then you need to disable the MySQL transport layer security. Use unverifiedTLS for transport in MySQLDatabaseConfig initializer.

    Your MySQLDatabaseConfig initializer should look something like this:

    let config = MySQLDatabaseConfig(
        hostname: "127.0.0.1",
        port: 3306,
        username: "dev",
        password: "",
        database: "test",
        transport: MySQLTransportConfig.unverifiedTLS
    )
    

    It should work fine with this configuration.