1

So I have a set of Docker services (running on Ubuntu) that are almost working at the moment:

  • php/apache webserver
  • MySQL server
  • PHPMyAdmin webserver

I have them set up in the docker-compose defined below, and the PHPMyAdmin is able to interface with the SQL server, but using the PHP connection script below, and calling db_connect(), gives the following error page:

Warning: mysqli::__construct():(HY000/2002): No such file or directory in /var/www/html/api/sql_db.php on line 12
MySQLi Connection failed: No such file or directory
SESSION: Array ( [sql_server] => db [username] => root[password] => test [dbname] => bravo 

File containing db_connect():

<?php
session_start();
$servername = "db";
$username = "root";
$password = "test";
$dbname = "bravo";
$_SESSION['sql_server'] = $servername;
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$_SESSION['dbname'] = $dbname;
function db_connect(){
  $conn = new mysqli($servername, $username, $password, $dbname);
  if ($conn->connect_errno) {
    printf("MySQLi Connection failed: %s\n", $conn->connect_error."<br>SESSION: ");
    print_r($_SESSION);
    session_destroy();
    unset($_SESSION);
    exit();
  }
}

docker-compose.yml:

version: '3'

services:
  db:
    image: mysql:5.7.23
    container_name: db
    command: --default-authentication-plugin=mysql_native_password
    ports: 
      - 3306:3306
    environment:
      MYSQL_DATABASE: bravo
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test
    restart: always
    volumes:
      - ./dump:/docker-entrypoint-initdb.d
      - ./db/data/mysql:/var/lib/mysql
    networks: 
      - default

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links: 
      - db
    ports:
      - 8000:80
    environment:
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test
    depends_on:
      - db

  websrv:
    build: .
    ports: 
      - 8080:80
    volumes:
      - ./www:/var/www/html
    links:
      - db
    networks:
      - default
    depends_on:
      - db

and finally, the Dockerfile referenced by the docker-compose for the websrv service:

FROM php:7.1.2-apache

RUN docker-php-ext-install mysqli 

I've gone through a number of forum posts on why mysqli wouldn't connect, but I haven't found an answer that solves my problem yet.

I've tried getting the internal ip of the docker container as well, but I haven't had any luck.

My choice of the docker images was a result of me trying to simply get php & mysqli, phpMyAdmin, and MySQL all working together. Any functioning combination of the three would be great for me, but surprisingly I haven't really found any public repo that's done it yet.

Thanks for any pointers.

3
  • If you start with docker-compose up. Then you should be able to access db by db not localhost. Also it takes sometime to boot up the database for more information regarding start up sequence please visit. docs.docker.com/compose/startup-order Commented Aug 12, 2018 at 4:53
  • @nicholasnet, I do start with docker-compose. As you can see in my PHP script, I am looking for a node named db Commented Aug 12, 2018 at 14:56
  • In phpmyadmin section you are not defining network in .yml file Commented Aug 12, 2018 at 15:33

3 Answers 3

1

I found my problem: I did a PHP goof. The way that I had my file written, the variables were defined outside of the function, and when the scope changed to inside the function, those values were all either undefined or null.

The fix for this admittedly stupid problem is to put those value definitions inside the function, like so.

<?php
function db_connect(){
  $servername = "db";
  $username = "root";
  $password = "test";
  $dbname = "bravo";

  $conn = new mysqli($servername, $username, $password, $dbname);
  if ($conn->connect_errno) {
    printf("MySQLi Connection failed: %s\n", $conn->connect_error."<br>SESSION: ");
    print_r($_SESSION);
    session_destroy();
    unset($_SESSION);
    exit();
  }
  return $conn;
}

Then, the connection can be made by calling $conn = db_connect(); after the file this exists in has been imported.

Thanks all for your efforts.

Sign up to request clarification or add additional context in comments.

Comments

0

In the docker compose, the link alias is "service: alias"(https://docs.docker.com/compose/compose-file/compose-file-v2/#links). I think in your example, in the webserver, the hostname of database is 'mysql'.

2 Comments

so, I saw that, but if you actually run the services, and from the webserver run ping db, it gets the same ip as when you run ping mysql I tried it both ways, and got the same response from the PHP server. I also tried links: - db, the same as you see in the phpmyadmin service.
I went back and tried fixing that,and as I mentioned above, I'm seeing the same error. I've modified the post to show that. Also, I'm using version 3 of the compose file. not version 2.
0

I dont know if it could be use full but my connectDB file is:

<?php
$conn = mysqli_connect("localhost","root","password","nameDb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.