-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(mysql): ensure MySQL socket symlink is created and wait for MySQL to start #2597
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Fixes MySQL startup/healthcheck failures caused by a Unix socket path mismatch by attempting to ensure /var/run/mysqld/mysqld.sock exists (via symlink) and adding a short wait for the socket before running readiness checks.
Changes:
- Add logic to create
/var/run/mysqldand (when detected) symlink/var/run/mysqld/mysqld.sock→/var/lib/mysql/mysql.sock. - Add a brief pre-check loop to wait for the MySQL socket to appear before proceeding with existing
mysqladmin pingreadiness waiting. - Apply the same behavior to both normal and debug docker-compose configurations.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| docker/docker-compose.yml | Adds socket wait + symlink creation before MySQL readiness checks to keep socket-based healthchecks working. |
| docker/docker-compose-debug.yml | Mirrors the socket wait + symlink creation logic for debug compose runs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| mkdir -p /var/run/mysqld 2>/dev/null || true |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There’s trailing whitespace on the blank line after MYSQL_PID=$$! (line 29) and an extra blank line later (line 43), which makes the YAML harder to review/maintain. Please remove the trailing spaces and collapse redundant blank lines.
| for i in $(seq 1 30); do | ||
| if [ -S /var/lib/mysql/mysql.sock ]; then | ||
| break | ||
| fi | ||
| sleep 1 | ||
| done | ||
|
|
||
| if [ -S /var/lib/mysql/mysql.sock ] && [ ! -e /var/run/mysqld/mysqld.sock ]; then |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as in docker-compose.yml: the 30s wait loop can skip symlink creation if /var/lib/mysql/mysql.sock shows up after the loop, causing the later mysqladmin ping -h localhost ... wait (and healthcheck) to fail indefinitely because /var/run/mysqld/mysqld.sock never gets created. Prefer creating the symlink immediately (dangling is OK) and/or retrying symlink creation while waiting for MySQL, only skipping if /var/run/mysqld/mysqld.sock already exists as a real socket.
| for i in $(seq 1 30); do | |
| if [ -S /var/lib/mysql/mysql.sock ]; then | |
| break | |
| fi | |
| sleep 1 | |
| done | |
| if [ -S /var/lib/mysql/mysql.sock ] && [ ! -e /var/run/mysqld/mysqld.sock ]; then | |
| # Ensure the socket symlink exists early; dangling is acceptable. | |
| if [ ! -S /var/run/mysqld/mysqld.sock ]; then | |
| ln -sf /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock 2>/dev/null || true | |
| fi | |
| for i in $(seq 1 30); do | |
| # If the MySQL socket appears during the wait, re-ensure the symlink. | |
| if [ -S /var/lib/mysql/mysql.sock ] && [ ! -S /var/run/mysqld/mysqld.sock ]; then | |
| ln -sf /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock 2>/dev/null || true | |
| echo 'Socket symlink ensured' | |
| break | |
| fi | |
| if [ -S /var/lib/mysql/mysql.sock ]; then | |
| break | |
| fi | |
| sleep 1 | |
| done | |
| # Final safeguard to ensure the symlink exists once MySQL is up. | |
| if [ -S /var/lib/mysql/mysql.sock ] && [ ! -S /var/run/mysqld/mysqld.sock ]; then |
| ln -sf /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock 2>/dev/null || true | ||
| echo 'Socket symlink ensured' |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ln failures are ignored (2>/dev/null || true) but the script still prints Socket symlink ensured, which can report success even if the symlink wasn’t actually created. Consider letting ln fail loudly or gating the success message on ln’s exit code.
| ln -sf /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock 2>/dev/null || true | |
| echo 'Socket symlink ensured' | |
| if ln -sf /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock 2>/dev/null; then | |
| echo 'Socket symlink ensured' | |
| else | |
| echo 'Failed to ensure socket symlink' >&2 | |
| fi |
| for i in $(seq 1 30); do | ||
| if [ -S /var/lib/mysql/mysql.sock ]; then | ||
| break | ||
| fi | ||
| sleep 1 | ||
| done |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 30s wait loop can leave the symlink uncreated if /var/lib/mysql/mysql.sock appears after the loop finishes. In that case the subsequent mysqladmin ping -h localhost ... loop (and the container healthcheck) can keep failing indefinitely because /var/run/mysqld/mysqld.sock never gets created. Consider creating the /var/run/mysqld/mysqld.sock symlink up-front (a dangling symlink is fine) and/or retrying symlink creation while waiting for MySQL, only skipping if /var/run/mysqld/mysqld.sock already exists as a real socket.
| ln -sf /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock 2>/dev/null || true | ||
| echo 'Socket symlink ensured' |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ln errors are suppressed (2>/dev/null || true) but the script still prints Socket symlink ensured unconditionally, which can be misleading if the symlink creation fails. Consider either not swallowing the ln failure or checking ln’s exit status before emitting the success message.
| ln -sf /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock 2>/dev/null || true | |
| echo 'Socket symlink ensured' | |
| ln -sf /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock 2>/dev/null | |
| if [ $$? -eq 0 ]; then | |
| echo 'Socket symlink ensured' | |
| else | |
| echo 'Failed to ensure socket symlink' >&2 | |
| fi |
| mkdir -p /var/run/mysqld 2>/dev/null || true | ||
| for i in $(seq 1 30); do | ||
| if [ -S /var/lib/mysql/mysql.sock ]; then |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR description’s “Files changed” list mentions only docker-compose.yml, but this PR also modifies docker-compose-debug.yml. Please update the description to reflect all changed files so reviewers/users aren’t surprised by debug compose behavior changes.
What type of PR is this?
Check the PR title.
(Optional) Translate the PR title into Chinese.
修复(mysql):确保 MySQL 健康检查使用期望的 unix socket 路径
(Optional) More detailed description for this PR (en / zh)
en:
This PR fixes an issue where
mysqladmin(and Docker healthchecks relying on the default socket path) fail to connect using the unix socket because MySQL creates its socket at/var/lib/mysql/mysql.sockwhile some checks expect/var/run/mysqld/mysqld.sock. Symptom observed when running inside the container:Root cause: mismatch between MySQL's socket location and the path expected by healthcheck tools. Fix applied: create a stable symlink from
/var/run/mysqld/mysqld.sockto/var/lib/mysql/mysql.sockin the MySQL entrypoint wrapper before the first healthcheck, ensuring existing healthchecks continue to work without changing their invocation. This is a low-risk, backwards-compatible change.zh:
该 PR 修复了一个问题:
mysqladmin(以及依赖默认 socket 路径的 Docker 健康检查)无法通过 unix socket 连接,因为 MySQL 在容器中将 socket 创建在/var/lib/mysql/mysql.sock,而某些检查期望的路径为/var/run/mysqld/mysqld.sock。在容器内触发的故障示例如上。根本原因:MySQL 的 socket 路径与健康检查工具期望的路径不一致。解决方法是在 MySQL 启动后的 entrypoint 脚本中(首次健康检查之前)创建从
/var/run/mysqld/mysqld.sock到/var/lib/mysql/mysql.sock的符号链接,保证现有健康检查和mysqladmin能够正常工作,风险低且向后兼容。(Optional) Which issue(s) this PR fixes:
Files changed
entrypointwrapper prior to the first healthcheck loop.Why this change
/var/run/mysqld/mysqld.sock.Alternatives considered
mysqladmin --protocol=TCP -h 127.0.0.1 ...) — more explicit but requires changing checks and may expose different networking semantics.socketMySQL config to/var/run/mysqld/mysqld.sock— more invasive and could affect runtime config.Testing / Verification
mysqladminreturnsmysqld is alive(after entering the password).Status: "healthy"after initial start and migration steps complete.coze-mysqlservice was unhealthy.Risks and Rollback
Additional notes
mysqladmin --protocol=TCP -h 127.0.0.1 ...and increasestart_period/retries.