I have 3 running containers, all is fine (containers are running, database is setted up) except that the PDO connection does not work. There is the error report
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Connection
refused in /var/www/html/lib/OCFram/PDOFactory.php:9 Stack trace: #0 /var/www
/html/lib/OCFram/PDOFactory.php(9): PDO->__construct('mysql:host=mysq...',
'root', 'root') #1 /var/www/html/lib/OCFram/BackController.php(17):
OCFram\PDOFactory::getMysqlConnexion() #2 /var/www/html/lib/OCFram
/Application.php(69): OCFram\BackController->__construct(Object(App\Frontend
\FrontendApplication), 'Welcome', 'index') #3 /var/www/html/App/Frontend
/FrontendApplication.php(17): OCFram\Application->getController() #4 /var/www
/html/bootstrap.php(30): App\Frontend\FrontendApplication->run() #5 {main}
thrown in /var/www/html/lib/OCFram/PDOFactory.php on line 9
the docker-compose.yml
version: "3.2"
services:
php:
build: './php/'
volumes:
- ./MediterPourGrandir/:/var/www/html/
apache:
build: './apache/'
depends_on:
- php
- mysql
ports:
- "8080:80"
volumes:
- ./MediterPourGrandir/:/var/www/html/
mysql:
image: mysql:5.6.40
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=monsupersite
- MYSQL_USER=root
- MYSQL_PASSWORD=root
ports:
- "3306:3306"
the php docker file
FROM php:7.2.7-fpm-alpine3.7
# RUN apk update; \
# apk upgrade;
# RUN docker-php-ext-install pdo pdo_mysql
# RUN docker-php-ext-install mysqli
RUN apk update --no-cache \
&& apk add --no-cache $PHPIZE_DEPS \
&& apk add --no-cache mysql-dev \
&& docker-php-ext-install pdo pdo_mysql
the pdo connection class
<?php
namespace OCFram;
class PDOFactory
{
public static function getMysqlConnexion()
{
$db = new \PDO('mysql:host=mysql;port=3306;dbname=monsupersite', 'root', 'root');
// $db = new \PDO('mysql:host=mysql;port=3306;charset=utf8', 'root', 'rootpassword');
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
echo "Connected succesfully";
return $db;
}
}
I have done so many unsuccessful try. I'am missing something, but can not find out..... If someone got an idea, it would be great.
Thank you.
I finally can find out.
I did not notice it but after few seconds the mysql container were shutting down. The reason is: by default a mysql container name the MYSQL_USER as 'root', so who wants to use 'root' as MYSQL_USER must not declare it. See the github solved issue https://github.com/docker-library/mysql/issues/129
With these settings it works.
mysql:
image: mysql:5.6.40
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=monsupersite
- MYSQL_PASSWORD=root
ports:
- "3306:3306"
Yes. Or you can check php_info()
information does php have enable extension or not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.