Replace the RUN apt-get update && \
for RUN apt-get update && apt-get install -y \
Add right after && docker-php-ext-install pdo pdo_mysql
Dockerfile
FROM php:apache
RUN apt-get update && apt-get install -y \
&& docker-php-ext-install pdo pdo_mysql
COPY ./app /var/www/html/
At this link(https://mysqlserverteam.com/mysql-8-0-4-new-default-authentication-plugin-caching_sha2_password/) you will be able to read about what is happening with your refused connection.
If you don’t need to use version 8, downgrade to version 5. Then follow the following.
Delete your local mysql volume:
$ docker-compose down
$ sudo rm -r mysql/
Use a developer username and password(dev, dev123), forget the root.
However, you can use root, or if you need to get the root password that will be generated randomly. Go to the terminal when Mysql is being lifted and search for the generated password. The log line will look like this: dbmysql_1 | GENERATED ROOT PASSWORD: odut5doo2ahngie5shohV8Eingah7us4
Do so in your Docker-Compose.yml, changing the Mysql version(image: mysql:5), setting the database, root password and user creation dev.
version: "3"
services:
php-apache:
build: .
ports:
- "8081:80"
links:
- dbmysql
volumes:
- ./app:/var/www/html
dbmysql:
image: mysql:5
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 1
MYSQL_DATABASE: database
MYSQL_USER: dev
MYSQL_PASSWORD: dev123
ports:
- "3306:3306"
Now rebuild in the environment. If your environment is accurate, run the commands as admin, use: sudo
$ docker-compose up --build
In his config.php, use dev user, or use root:
<?php
$dsn = "mysql:host=dbmysql;dbname=database";
$dbuser = "dev";
$dbpass = "dev123";
try{
$pdo = new PDO($dsn,$dbuser, $dbpass);
echo "Conectado!";
}catch (PDOException $e){
echo $e->getMessage();
}
Dude. First, put the code here and not the link. I looked there... Replace the
RUN apt-get update && \
forRUN apt-get update && apt-get install -y \
Test it. Don’t forget to indulge the rest ...&& docker-php-ext-install pdo pdo_mysql
– Fabiano Monteiro
Next time I publish the codes, thank you for tidying up the post, I’ll try as you suggested.
– Aurino Geraldo