Search code examples
mysqldatabase-connectionddev

Why is connection refused to database in ddev?


For:

$conn = new mysqli('127.0.0.1', 'db', 'db', 'db');

I get: mysqli_sql_exception: Connection refused

And for:

$conn = new mysqli('localhost', 'db', 'db', 'db');

I get: mysqli_sql_exception: No such file or directory

The database does exist, as the following command confirms:

ddev launch -p

gives:

phpmyadmin screenshot of database

and here is ddev describe output:

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Project: debra-local ~/clients/defra/rpa/dev/sites/debra-local https://debra-local.ddev.site                            │
│ Docker provider: docker 24.0.2                                                                                          │
│ Router: traditional                                                                                                     │
├────────────┬──────┬────────────────────────────────────────────────────────────────────────────────┬────────────────────┤
│ SERVICE    │ STAT │ URL/PORT                                                                       │ INFO               │
├────────────┼──────┼────────────────────────────────────────────────────────────────────────────────┼────────────────────┤
│ web        │ OK   │ https://debra-local.ddev.site                                                  │ php PHP8.2         │
│            │      │ InDocker: web:443,80,8025                                                      │ nginx-fpm          │
│            │      │ Host: 127.0.0.1:32937,32938                                                    │ docroot:'html'     │
│            │      │                                                                                │ NodeJS:16          │
├────────────┼──────┼────────────────────────────────────────────────────────────────────────────────┼────────────────────┤
│ db         │ OK   │ InDocker: db:3306                                                              │ mysql:5.7          │
│            │      │ Host: 127.0.0.1:32939                                                          │ User/Pass: 'db/db' │
│            │      │                                                                                │ or 'root/root'     │
├────────────┼──────┼────────────────────────────────────────────────────────────────────────────────┼────────────────────┤
│ PHPMyAdmin │ OK   │ https://debra-local.ddev.site:8037                                             │                    │
│            │      │ InDocker: dba:80,80                                                            │                    │
│            │      │ `ddev launch -p`                                                               │                    │
├────────────┼──────┼────────────────────────────────────────────────────────────────────────────────┼────────────────────┤
│ Mailhog    │      │ MailHog: https://debra-local.ddev.site:8026                                    │                    │
│            │      │ `ddev launch -m`                                                               │                    │
├────────────┼──────┼────────────────────────────────────────────────────────────────────────────────┼────────────────────┤
│ All URLs   │      │ https://debra-local.ddev.site, https://127.0.0.1:32937,                        │                    │
│            │      │ http://debra-local.ddev.site, http://127.0.0.1:32938                           │                    │
└────────────┴──────┴────────────────────────────────────────────────────────────────────────────────┴────────────────────┘

and here is .ddev/config.yaml contents:

name: debra-local
type: php
docroot: html
php_version: "8.2"
webserver_type: nginx-fpm
router_http_port: "80"
router_https_port: "443"
xdebug_enabled: false
additional_hostnames: []
additional_fqdns: []
database:
    type: mysql
    version: "5.7"
nfs_mount_enabled: false
mutagen_enabled: false
use_dns_when_possible: true
composer_version: "2"
web_environment: []
nodejs_version: "16"

Solution

  • You are trying to connect to 127.0.0.1. That's not where the database server is in DDEV. It's at db. so you need

    $conn = new mysqli('db', 'db', 'db', 'db');