Skip to content

Commit 9074756

Browse files
committed
fix(other): resolve MySQL access denied in container networking with init script
1 parent be4a5b2 commit 9074756

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

docker/docker-compose.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ services:
77
- "3306:3306"
88
volumes:
99
- ./data/mysql:/var/lib/mysql
10+
# Mount the initialization script to create user with Docker network permissions
11+
- ./init-db.sh:/docker-entrypoint-initdb.d/01-init-user.sh:ro
1012
environment:
1113
- MYSQL_ROOT_PASSWORD=root_password
1214
- MYSQL_DATABASE=cypht
@@ -19,7 +21,7 @@ services:
1921
retries: 5
2022

2123
cypht:
22-
image: cypht/cypht:2.5.1
24+
image: iralfred/cypht:2.5.2-alpha
2325
depends_on:
2426
db:
2527
condition: service_healthy

docker/init-db.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
# MariaDB/MySQL initialization script for Cypht
3+
# This script runs in /docker-entrypoint-initdb.d/ and ensures the database user
4+
# has proper permissions for Docker networking.
5+
#
6+
# It is intended to be used **only** with MariaDB/MySQL.
7+
# - It creates the application user with '@%' host for Docker network connections
8+
# - It also ensures a '@localhost' user exists for local access / healthchecks
9+
#
10+
# Note: SQLite and PostgreSQL do not use this script:
11+
# - SQLite is file-based, has no users, and is accessed directly by the app
12+
# - PostgreSQL should be configured using its own mechanisms; this script does not touch it
13+
14+
set -e
15+
16+
# MySQL/MariaDB initialization
17+
MYSQL_USER="${MYSQL_USER:-cypht}"
18+
MYSQL_PASSWORD="${MYSQL_PASSWORD:-cypht_password}"
19+
MYSQL_DATABASE="${MYSQL_DATABASE:-cypht}"
20+
21+
# Get root password - MariaDB init scripts can access this via environment or file
22+
if [ -n "$MYSQL_ROOT_PASSWORD_FILE" ] && [ -f "$MYSQL_ROOT_PASSWORD_FILE" ]; then
23+
MYSQL_ROOT_PASSWORD=$(cat "$MYSQL_ROOT_PASSWORD_FILE")
24+
elif [ -n "$MYSQL_ROOT_PASSWORD" ]; then
25+
MYSQL_ROOT_PASSWORD="$MYSQL_ROOT_PASSWORD"
26+
else
27+
echo "Error: MYSQL_ROOT_PASSWORD not set" >&2
28+
exit 1
29+
fi
30+
31+
# Create user with wildcard host (%) to allow connections from any Docker container
32+
# This is necessary because Docker containers connect via service names, not localhost.
33+
# Note: MYSQL_USER environment variable creates 'user'@'localhost', but we also need 'user'@'%'.
34+
mysql -u root -p"${MYSQL_ROOT_PASSWORD}" <<EOF_SQL
35+
-- Create user with wildcard host for Docker network connections
36+
-- This allows connections from any container in the Docker network
37+
CREATE USER IF NOT EXISTS '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';
38+
GRANT ALL PRIVILEGES ON ${MYSQL_DATABASE}.* TO '${MYSQL_USER}'@'%';
39+
40+
-- Ensure localhost user exists (may already exist from MYSQL_USER env var)
41+
CREATE USER IF NOT EXISTS '${MYSQL_USER}'@'localhost' IDENTIFIED BY '${MYSQL_PASSWORD}';
42+
GRANT ALL PRIVILEGES ON ${MYSQL_DATABASE}.* TO '${MYSQL_USER}'@'localhost';
43+
44+
-- Flush privileges to ensure changes take effect
45+
FLUSH PRIVILEGES;
46+
EOF_SQL
47+
48+
echo "✓ Created MySQL/MariaDB user '${MYSQL_USER}' with Docker network permissions (@'%' and @'localhost')"
49+

docker/init-db.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- MariaDB initialization script for Cypht
2+
-- This script creates the database user with proper host permissions for Docker networking
3+
-- It will only run if the database is being initialized (first run)
4+
--
5+
-- NOTE: This is a fallback SQL file. The preferred method is to use init-db.sh
6+
-- which dynamically reads environment variables. This SQL file uses default values.
7+
-- For production use, mount init-db.sh instead or customize this file.
8+
9+
-- Create user with wildcard host (%) to allow connections from any Docker container
10+
-- This is necessary because Docker containers connect via service names, not localhost
11+
CREATE USER IF NOT EXISTS 'cypht'@'%' IDENTIFIED BY 'cypht_password';
12+
GRANT ALL PRIVILEGES ON cypht.* TO 'cypht'@'%';
13+
14+
-- Also create user for localhost connections (for healthchecks and local access)
15+
CREATE USER IF NOT EXISTS 'cypht'@'localhost' IDENTIFIED BY 'cypht_password';
16+
GRANT ALL PRIVILEGES ON cypht.* TO 'cypht'@'localhost';
17+
18+
-- Flush privileges to ensure changes take effect
19+
FLUSH PRIVILEGES;
20+

0 commit comments

Comments
 (0)