Skip to content

Commit 4bfd057

Browse files
committed
fix: use mysql_native_password for tests instead of caching_sha2_password
1 parent 3946853 commit 4bfd057

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/pytest_databases/docker/mysql.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ def check(_service: ServiceContainer) -> bool:
8080
else:
8181
db_name += suffix
8282

83+
# For MySQL 8, we need to handle authentication plugin compatibility
84+
exec_commands = (
85+
f'mysql --user=root --password={root_password} -e "CREATE DATABASE {db_name};'
86+
f"GRANT ALL PRIVILEGES ON *.* TO '{user}'@'%'; "
87+
)
88+
89+
# For MySQL 8, change the authentication plugin to mysql_native_password
90+
# to avoid issues with caching_sha2_password and cryptography library
91+
if "mysql:8" in image or "mysql:9" in image:
92+
exec_commands += (
93+
f"ALTER USER '{user}'@'%' IDENTIFIED WITH mysql_native_password BY '{password}'; "
94+
)
95+
96+
exec_commands += 'FLUSH PRIVILEGES;"'
97+
8398
with docker_service.run(
8499
image=image,
85100
check=check,
@@ -95,11 +110,7 @@ def check(_service: ServiceContainer) -> bool:
95110
},
96111
timeout=60,
97112
pause=0.5,
98-
exec_after_start=(
99-
f'mysql --user=root --password={root_password} -e "CREATE DATABASE {db_name};'
100-
f"GRANT ALL PRIVILEGES ON *.* TO '{user}'@'%'; "
101-
'FLUSH PRIVILEGES;"'
102-
),
113+
exec_after_start=exec_commands,
103114
transient=isolation_level == "server",
104115
platform=platform,
105116
) as service:

0 commit comments

Comments
 (0)