migrate/DATABASE_SETUP.md

127 lines
3.5 KiB
Markdown

# 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:
```bash
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:
```env
# 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:
1. **MySQL is running:**
```bash
sudo systemctl status mysql
# or
sudo systemctl status mariadb
```
2. **MySQL binds to the correct interface:**
Check `/etc/mysql/my.cnf` or `/etc/mysql/mariadb.conf.d/50-server.cnf`:
```ini
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)
```
3. **Firewall allows connections:**
```bash
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:
```bash
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:
1. **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
2. **Use the host port mapping:**
- Find the mapped port: `ddev describe` (in the magento1 project)
- Use `127.0.0.1` as host and the mapped port
### Quick Fix Script
Run this to automatically detect and test the connection:
```bash
# 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?
1. **Check if databases exist:**
```bash
mysql -u root -p -e "SHOW DATABASES LIKE 'magento%';"
```
2. **Check MySQL user permissions:**
```bash
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.
3. **Check MySQL error log:**
```bash
sudo tail -f /var/log/mysql/error.log
```
4. **Test from web interface:**
Visit `/migration` and click "Test Connections" to see detailed error messages.