2

I am trying make one Docker image with Apache and PHP linked to another image with MySQL. Apache and PHP works great on first look and MySQL container is successfully connected to third container with phpmyadmin and it works. But when I am trying to create new connection with PDO it returns SQLSTATE[HY000] [2002] No such file or directory.

try {
    $msql = new \PDO('mysql:host=localhost;dbname=test', 'root', 'password');
    echo 'Connected';
} catch (\PDOException $e) {
    echo 'Error: ' . $e->getmessage();
}

phpinfo() returns pdo_mysql (mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $). PHP version is 7.0.22.

This is Dockerfile:

FROM ubuntu:16.04

RUN apt-get update

# Apache
RUN apt-get -y install apache2

# PHP
RUN apt-get -y install php libapache2-mod-php php-mcrypt php7.0-mysql
RUN phpenmod pdo_mysql

# phpmyadmin
#RUN apt-get -y install phpmyadmin

# Start Apache
CMD /etc/init.d/mysql start -D FOREGROUND
CMD /usr/sbin/apache2ctl -D FOREGROUND

EXPOSE 80

I used mysql:5.7.13 image for mysql container with exposed 3306 port and both containers are in the same network. Any ideas where is the problem?

micobg
  • 1,272
  • 6
  • 21
  • 35
  • You forgot the "q" in your connection string: `new \PDO('mysl:host` – rickdenhaan Jan 07 '18 at 21:04
  • Yes, thank you! :) But it still returns an error (not the same): SQLSTATE[HY000] [2002] No such file or directory – micobg Jan 07 '18 at 21:11
  • 5
    That's probably because you're trying to connect to localhost and you named your MySQL docker container something else when you linked it. See [this question](https://stackoverflow.com/q/40075065/1941241) for a possible duplicate. – rickdenhaan Jan 07 '18 at 21:16
  • Yes, host=mysql was the solution. Thank you! – micobg Jan 12 '18 at 19:15

0 Answers0