3.5 KiB
Database Connection Setup for DDEV
Problem: Connection Refused Error
If you're seeing SQLSTATE[HY000] [2002] Connection refused, it means the Laravel application running inside DDEV cannot reach the Magento databases.
Solution
The databases magento1 and magento2 are likely running on your host machine, but the Laravel app is running inside a DDEV container. Containers need a special IP address to reach the host machine.
Step 1: Find Your Host IP from DDEV
Run this command to find the correct IP address:
ddev exec "ip route show default | awk '/default/ {print \$3}'"
This will output something like 172.22.0.1 (your gateway IP).
Step 2: Update Your .env File
Add these lines to your .env file with the correct host IP:
# Magento 1 Database
# Use the gateway IP from Step 1 (e.g., 172.22.0.1)
# OR use 127.0.0.1 if databases are in another DDEV project
MAGENTO1_DB_HOST=172.22.0.1
MAGENTO1_DB_PORT=3306
MAGENTO1_DB_DATABASE=magento1
MAGENTO1_DB_USERNAME=root
MAGENTO1_DB_PASSWORD=your_password
MAGENTO1_DB_PREFIX=
# Magento 2 Database
MAGENTO2_DB_HOST=172.22.0.1
MAGENTO2_DB_PORT=3306
MAGENTO2_DB_DATABASE=magento2
MAGENTO2_DB_USERNAME=root
MAGENTO2_DB_PASSWORD=your_password
MAGENTO2_DB_PREFIX=
Step 3: Verify MySQL is Accessible from Host
Make sure your MySQL server on the host is configured to accept connections. Check:
-
MySQL is running:
sudo systemctl status mysql # or sudo systemctl status mariadb -
MySQL binds to the correct interface: Check
/etc/mysql/my.cnfor/etc/mysql/mariadb.conf.d/50-server.cnf:bind-address = 0.0.0.0 # Allows connections from any IP # OR bind-address = 127.0.0.1 # Only localhost (won't work from DDEV) -
Firewall allows connections:
sudo ufw status # If needed, allow MySQL: sudo ufw allow 3306/tcp
Step 4: Test Connection from DDEV Container
Test if you can connect from inside the DDEV container:
ddev exec "mysql -h 172.22.0.1 -u root -pyour_password -e 'SHOW DATABASES;'"
Replace 172.22.0.1 with your gateway IP from Step 1, and your_password with your actual MySQL root password.
Alternative: If Databases are in Separate DDEV Projects
If magento1 and magento2 are in separate DDEV projects, you can:
-
Use the DDEV database service name:
- If magento1 is in a DDEV project called
magento1, use:MAGENTO1_DB_HOST=magento1-db - This only works if both projects are in the same Docker network
- If magento1 is in a DDEV project called
-
Use the host port mapping:
- Find the mapped port:
ddev describe(in the magento1 project) - Use
127.0.0.1as host and the mapped port
- Find the mapped port:
Quick Fix Script
Run this to automatically detect and test the connection:
# Get the gateway IP
GATEWAY_IP=$(ddev exec "ip route show default | awk '/default/ {print \$3}'" | tr -d '\n')
echo "Gateway IP: $GATEWAY_IP"
# Test connection
ddev exec "mysql -h $GATEWAY_IP -u root -proot -e 'SHOW DATABASES LIKE \"magento%\"'"
Still Having Issues?
-
Check if databases exist:
mysql -u root -p -e "SHOW DATABASES LIKE 'magento%';" -
Check MySQL user permissions:
mysql -u root -p -e "SELECT User, Host FROM mysql.user WHERE User='root';"Make sure root can connect from
%(any host) or from the gateway IP. -
Check MySQL error log:
sudo tail -f /var/log/mysql/error.log -
Test from web interface: Visit
/migrationand click "Test Connections" to see detailed error messages.