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

Fix auth switch failure on MySQL 8 #979

Merged
merged 1 commit into from
Jan 14, 2021

Conversation

sile
Copy link
Contributor

@sile sile commented Jan 13, 2021

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:password@127.0.0.1/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:password@127.0.0.1/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:password@127.0.0.1/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
Copy link
Member

mehcode commented Jan 14, 2021

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

@mehcode mehcode merged commit 043bbd5 into launchbadge:master Jan 14, 2021
@mehcode
Copy link
Member

mehcode commented Jan 14, 2021

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
Copy link
Contributor Author

sile commented Jan 14, 2021

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