Wordpress not connecting to mysql

Mysql starts properly, i am trying to use consul connect then connect to mysql db from wordpress

job "wordpress-mysql-sample" {
  datacenters = ["dc1"]
  
  group "mysql" {
    network {
      mode = "bridge"
      port "db" {
        to = 3306
      }
    }
    
    service {
      name = "mysql"
      port = "db"
      
      connect {
        sidecar_service {}
      }
    }
    
    task "mysql" {
      driver = "docker"
      config {
        image = "mysql:latest"
      }
      env {
        MYSQL_ROOT_PASSWORD = "rootpassword"
        MYSQL_DATABASE      = "mydatabase"
        MYSQL_USER          = "myuser"
        MYSQL_PASSWORD      = "mypassword"
      }
      
      resources {
        cpu    = 2300
        memory = 4096
      }
    }
  }
  
  group "wordpress" {
    count = 1
    network {
      mode = "bridge"
      port "http" {
        to = 80
      }
    }
    
    service {
      name = "wordpress"
      port = "http"
      
      connect {
        sidecar_service {
          proxy {
            upstreams {
              destination_name = "mysql"
              local_bind_port  = 3306
            }
          }
        }
      }
      
      check {
        type     = "http"
        path     = "/"
        interval = "10s"
        timeout  = "2s"
      }
    }
    
    task "wordpress" {
      driver = "docker"
      config {
        image = "wordpress:latest"
      }
      env {
        WORDPRESS_DB_HOST     = "mysql.service.consul:3306"
        WORDPRESS_DB_USER     = "myuser"
        WORDPRESS_DB_PASSWORD = "mypassword"
        WORDPRESS_DB_NAME     = "mydatabase"
      }
      
      resources {
        cpu    = 500
        memory = 256
      }
    }
  }
}

Hi,

IIRC, Consul Connect does not work properly with ports defined in the network stanza.

Please try to remove the port from the network stanza and use the literal “3306” in the service->port definition.

Works for me at last :slight_smile:

Hi @pipethedev,

In addition to @matthias’s suggestion, you must switch WORDPRESS_DB_HOST from mysql.service.consul to 127.0.0.1:3306.

Also, note that it should be 127.0.0.1 and not localhost due to the way MySQL driver works (ref: mysql - Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /private/tmp/wordpress/wp-includes/wp-db.php on line 1452 - Stack Overflow).

I have tested your nomad job spec with the above two changes and it works for me. (you should also have Service-Intentions if ACLs are enabled in Consul with default-policy set to deny).