Skip to content

Fix auth switch failure on MySQL 8 - #979

Merged
mehcode merged 1 commit into
transact-rs:masterfrom
sile:fix-mysql-auth-switch-failure
Jan 14, 2021
Merged

mehcode merged 1 commit into
transact-rs:masterfrom
sile:fix-mysql-auth-switch-failure

Conversation

@sile

@sile sile commented Jan 13, 2021

Copy link
Copy Markdown
Contributor

The current implementation fails to connect a MySQL 8 server if the authentication plugin of the user has been changed from the default caching_sha2_password (related issue: #779).

This PR addresses the problem.

Note that I'm not familiar with MySQL and not sure this patch is the correct solution to address this issue, so any advice is very welcomed.

Commands to reproduce the problem

Starts a MySQL server:

$ docker run -e MYSQL_DATABASE=db -e MYSQL_USER=foo \
    -e MYSQL_PASSWORD=password -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 --rm mysql:8.0.22

Connects to the server with foo user (this will be succeeded):

use sqlx::Connection;

#[async_std::main]
async fn main() -> anyhow::Result<()> {
    let url = "mysql://foo:[email protected]/db";
    sqlx::mysql::MySqlConnection::connect(url).await?;
    Ok(())
}

Changes the auth plugin from the default to mysql_native_password:

use sqlx::Connection;

#[async_std::main]
async fn main() -> anyhow::Result<()> {
    let url = "mysql://root:[email protected]/db";
    let mut connection = sqlx::mysql::MySqlConnection::connect(url).await?;
    sqlx::query("ALTER USER 'foo' IDENTIFIED WITH mysql_native_password BY 'password'")
        .execute(&mut connection)
        .await?;
    Ok(())
}

Again, connects to the server with foo user (this will be failed):

use sqlx::Connection;

#[async_std::main]
async fn main() -> anyhow::Result<()> {
    let url = "mysql://foo:[email protected]/db";
    sqlx::mysql::MySqlConnection::connect(url).await?;
    Ok(())
}
// => Error: error returned from database: 1045 (28000): Access denied for user 'foo'@'172.17.0.1' (using password: YES)

Once the patch of this PR is applied, the above script can be executed without errors.

@mehcode

mehcode commented Jan 14, 2021

Copy link
Copy Markdown
Contributor

I appreciate the fix. Thank you ❤️ . This is a very undocumented part of MySQL. Haha.

@mehcode
mehcode merged commit 043bbd5 into transact-rs:master Jan 14, 2021
@mehcode

mehcode commented Jan 14, 2021

Copy link
Copy Markdown
Contributor

Note that I'm not familiar with MySQL and not sure this patch is the correct solution to address this issue, so any advice is very welcomed.

I've done some extensive testing on next (slated to become the core of 0.6) and dropping the NUL works for all forms of auth that I can tell.

@sile
sile deleted the fix-mysql-auth-switch-failure branch January 14, 2021 07:33
@sile

sile commented Jan 14, 2021

Copy link
Copy Markdown
Contributor Author

Thank you for your review!

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

Successfully merging this pull request may close these issues.

2 participants