phpmyadmin / mysqli on docker do not connect - mysql

I want to run two docker container, one for phpmyadmin and one for mysql server, so i wrote this docker-compose file to run it on RaspberryPi:
version: '3.1'
services:
db:
image: mysql:latest
container_name: db
volumes:
- dbdata:/var/lib/mysql
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: test
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_HOST: db
PMA_ARBITRARY: 1
volumes:
dbdata:
The volume mount, the containers build up, the phpmyadmin page load but when i try to login with server: machine-ip:3306, user: root, pass: test; got this error:
mysqli::real_connect(): Error while reading greeting packet. PID=34
mysqli::real_connect(): (HY000/2006): MySQL server has gone away
Tried everything and worked only one time, but when i restarted it not worked again

Related

Docker Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock

I have been trying to setup docker-compose for my application written in flask and connect it to msql database. Both containers are seems to be working fine and aplication starts properrly, but whenever there is any request to database, I am getting following error from my flask-app comtainer:
sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)")
For me it looks like my containers don't see each other. I am new to Docker, but I thought that my current docker-compose should work
version: '3.9'
networks:
backend:
volumes:
mysqldb:
services:
app:
restart: "no"
container_name: flask-app
build:
context: ./AEH_PAP
dockerfile: ./Dockerfile
ports:
- "9000:9000"
networks:
- backend
environment:
DATABASE_URI: mysql://user:password#db:9000/library3
links:
- 'db'
depends_on:
- db
volumes:
- /var/lib/mysql/mysql.sock:/mysql.sock
db:
container_name: mysql-db
image: "mysql:8.0.31"
ports:
- '3307:3306'
restart: "no"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: library3
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- "mysqldb:/var/lib/mysql"
- "./init.sql:/docker-entrypoint-initdb.d/init.sql"
networks:
- backend
mysql-db logs
flask-app logs
Also I have been trying to connect to mysql-db container from flask-app container with command mysql --host localhost --port 3306 -u user -p but still getting the same error.
My Docker knowledge is very limited but should I have msql server installed also on flask-app?

Docker Compose - Connection to Phpmyadmin and MysQL not working

I need a simple way with Docker-compose to create an environment with PHP, NGINX, MySQL and phpmyadmin.
I have already successfully created the PHP environment with NGINX.
Now I want to add a database with MySQL and phpmyadmin. These two components do not seem to work. For example, I cannot reach phpmyadmin through the specified port "8081". I reach my local servers with my local IP addresses and the ports at the end of the address.
As soon as I want to call phpmyadmin the browser window tells me "Can't connect to server".
Here is the docker-compose.yml file:
version: "3.9"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./default.conf:/etc/nginx/conf.d/default.conf
links:
- php-fpm
php-fpm:
image: php:8-fpm
volumes:
- ./src:/var/www/html
mysql:
image: mysql
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: '<mypassword>'
MYSQL_DATABASE: baton
MYSQL_USER: baton
MYSQL_PASSWORD: '<mypassword>'
ports:
- "3306:3306"
volumes:
- ./database/mysql:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: pma
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
Hope anyone can help!
Now I found the mistake I made to connect to the database via phpmyadmin. I got a second database, which already running on port 3306. I now switched to the existing database and now the connection works!

Can't Connect to MySQL Database using Adminer and docker-compose

I'm trying to connect to my mysql database using official adminer and mysql images from docker hub.
Here is my docker-compose.yml file configuration:
version: '3'
services:
mysql:
image: mysql
restart: always
volumes:
- mysql:/var/lib/mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD= 1
- MYSQL_DATABASE= db
ports:
- 3306:3306
- 33060:33060
adminer:
image: adminer
restart: always
ports:
- 8080:8080
depends_on:
- mysql
volumes:
mysql:
Whenever I want to login to the MySQL using Adminer I face with the following problem:
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
SQLSTATE[HY000] [2002] No such file or directory
Here is the inputs I've used trying to connect to MySQL from Adminer interface:
#first try
System: MySQL
Server: localhost
Username: root
Database: db
#second try
System: MySQL
Server: mysql #container-name
Username: root
Database: db
You have to add the default authentication plugin in compose file
command: --default-authentication-plugin=mysql_native_password
Here is the complete docker-compose.yml
services:
mysql:
image: mysql
restart: always
command: --default-authentication-plugin=mysql_native_password
volumes:
- mysql:/var/lib/mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD= 1
- MYSQL_DATABASE= db
ports:
- 3306:3306
- 33060:33060
adminer:
image: adminer
restart: always
ports:
- 8080:8080
depends_on:
- mysql
volumes:
mysql:
In adminer
System: MySQL <DB System to connect>
Server: mysql <should match the starting tag before image tag > (in your case mysql but you can change it to any name )
Username: root <username>
Password: <password>
Database: db <database name>

How to fix 'The server requested authentication method unknown to the client [caching_sha2_password]' on docker compose?

I'm setting up a wamp. The docker-compose was working very well until I experience some problems with the phpmyadmin and mysql container. I couldn't connect nor from php or phpmyadmin and usually had this error message : mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password]
I was able to solve the problem by just connecting entering the shell of the db container docker exec -it db mysql -uroot -p and running this command : ALTER USER 'root' IDENTIFIED WITH mysql_native_password by '123456'; But this kind of boring because I have a partner working on the same project and we have to change working posts a lot so it means rerun docker each time in the development phase so I was wondering what's wrong on my docker-compose...
Here it is :
version: "3.1"
services:
www:
build: .
container_name: app
ports:
- "8001:80"
volumes:
- ~/Desktop/WORK_in_progress/camagru/www/:/var/www/html/
links:
- db
networks:
- default
db:
image: mysql:8.0
container_name: db
restart: always
tty: true
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
command: --innodb-use-native-aio=0
environment:
MYSQL_DATABASE: CAMAGRU
MYSQL_USER: user
MYSQL_PASSWORD: 123456
MYSQL_ROOT_PASSWORD: 123456
volumes:
- ~/Desktop/WORK_in_progress/camagru/dump:/docker-entrypoint-initdb.d
- ~/Desktop/WORK_in_progress/camagru/conf:/etc/mysql/conf.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
container_name: phpmyadmin
restart: always
tty: true
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: 123456
MYSQL_ROOT_PASSWORD: 123456
volumes:
persistent:
note : I don't use any configurations file, I modified a bit a compose that I found online.
Your command is right, the issue appears because you have defined the command key two times, and the second one is overriding the first one. Instead you should define it only once and like this:
command: --default-authentication-plugin=mysql_native_password --innodb-use-native-aio=0

Docker Compose phpmyadmin not connecting to MySQL

I have a docker compose file which is basically trying to build a WAMP/LAMP style environment.
Basically, PHPMyAdmin can't seem to connect to MySQL. Looking through answers here, it appeared that it was an issue with legacy auth using MySQL Image 8 so I added:
command: --default-authentication-plugin=mysql_native_password
but that didn't work, so I dropped down to mysql image5.7 and the issue is still present. For some reason, I can't connect to MySQL and I get the error:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Try again
and
mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Try again
Here is a copy of my Docker Compose which I don't think is doing anything weird.
Hoping someone can clarify that for me.
version: "3.1"
services:
site:
build: .
ports :
- "80:80"
volumes:
- ./www:/var/www/html/
links:
- database
networks:
- php-network
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- database:mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- "8080:80"
environment:
- MYSQL_USERNAME=admin
- MYSQL_PASSWORD=root
networks:
- php-network
#######################################
# MySQL server
#######################################
database:
image: mysql:5.7.25
ports:
- "3306:3306"
container_name: db-mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test_db
- MYSQL_USER=admin
- MYSQL_PASSWORD=root
networks:
- php-network
networks:
php-network:
driver: bridge
I also saw mention of a "depends on" flag which I tried too but that also failed to allow me to connect. I have had the same error throughout.
It's about the container name.
database:
image: mysql:5.7.25
ports:
- "3306:3306"
- **container_name: db-mysql**
and in your phpcontainer:
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- database:mysql
environment:
**PMA_HOST: mysql**
PMA_PORT: 3306
you define host as mysql, which in docker network will be unrecognizable.
try switching PMA_HOST to db-mysql.
Sorry for bad formatting.
Also use docker ps to see docker container names and to figure out which hosts do you need to connect.
I've got this working now with the following:
version: "3.1"
services:
www:
build: .
ports:
- "8081:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
#######################################
# MySQL server
#######################################
db:
image: mysql:5.7.25
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: test_db
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
According to your phpmyadmin service configuration, you are trying to reach mysql container by mysql address:
environment:
PMA_HOST: mysql
PMA_PORT: 3306
But container with mysql server is accessible by database or db-mysql addresses.
So, you need to change phpmyadmin service configuration to:
environment:
PMA_HOST: database
PMA_PORT: 3306