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

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>

Related

phpmyadmin / mysqli on docker do not connect

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

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 mysql root or user won't login, just access denied. Using docker compose

Using docker compose i have a mysql 5.7 database container, i set the root password and a user password but they don't work, the docker-compose:
version: '3.8'
services:
viprs-proxy:
platform: linux/amd64
image: nginx:alpine
container_name: viprs-proxy
depends_on:
- viprs-website
volumes:
- ./nginx/proxy.conf:/etc/nginx/nginx.conf
ports:
- 80:80
networks:
- viprs-net
viprs-website:
platform: linux/amd64
image: nginx
container_name: viprs-website
depends_on:
- php
- viprs-website-database
volumes:
- ./website/nginx/site.conf:/etc/nginx/conf.d/default.conf
- ./website:/usr/share/nginx/html
- ./website/logs:/var/log/nginx
- viprs-uploads:/usr/share/nginx/html/wp-content/uploads
ports:
- 80
links:
- php
networks:
- viprs-net
php:
platform: linux/amd64
#image: php:7-fpm
image: viprs-php
container_name: php
volumes:
- ./website:/usr/share/nginx/html
ports:
- 9000
networks:
- viprs-net
viprs-website-database:
platform: linux/amd64
image: mysql:5.7
container_name: viprs-db
command: --init-file /usr/share/nginx/website.sql
volumes:
- ./website.sql:/usr/share/nginx/website.sql
- viprs-db:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: testpassword
MYSLQ_DATABASE: viprs
MYSQL_USER: viprs
MYSQL_PASSWORD: testpassword
networks:
- viprs-net
networks:
viprs-net:
volumes:
viprs-uploads:
viprs-db:
Now if i log into bash:
docker exec -it viprs-db bash
and try to log into mysql:
mysql -u root -p
It just says access is denied, it doesn't matter if i am using the root user or the viprs user.
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
If i output $MYSQL_ROOT_PASSWORD it gives:
echo $MYSQL_ROOT_PASSWORD
testpassword
I have seen loads of people with this issue but can't find any solution that works. The environment is set as per the image documentation on docker hub so i'm confused.
In fact, i can log in as root without a password, so something isn't right.
The environment variables are only used if there is no database present when then container starts and MySQL has to create one.
If there already is a database, the users defined in that database are used and no new users are created. Since you have a volume mapping on /var/lib/mysql chances are that you already have a database.
To verify if that's the issue, you can try removing the /var/lib/mysql mapping. That will cause the container to create a new database when it starts using the environment variable values.

How to connect to mariadb from host with docker-compose

I have this docker-compose file:
version: "3"
services:
mariadb:
image: mariadb:latest
container_name: mariadb
command: --default-authentication-plugin=mysql_native_password
restart: unless-stopped
env_file: .env
volumes:
- db-data:/var/lib/mysql
networks:
- internal
drupal:
image: drupal:9.3.9-fpm-alpine
container_name: drupal
depends_on:
- mariadb
restart: unless-stopped
networks:
- internal
- external
volumes:
- /var/www/html/:/var/www/html/
webserver:
image: nginx:latest
container_name: webserver
depends_on:
- drupal
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- /var/www/html/:/var/www/html/
- ./nginx-conf:/etc/nginx/conf.d
networks:
- external
networks:
external:
driver: bridge
internal:
driver: bridge
volumes:
db-data:
I run docker-compose up -d and everything works fine, but I can not access to the mariadb server from host, I tried these
# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
# mysql -uroot -p -h mariadb
Enter password:
ERROR 2005 (HY000): Unknown MySQL server host 'mariadb' (-2)
# mysql -uroot -p -h 127.0.0.1
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
Edit:
.env file:
MYSQL_ROOT_PASSWORD=abc1234
MYSQL_DATABASE=my_db
MYSQL_USER=drupal
MYSQL_PASSWORD=cdf1234
From MariaDB Docker Library documentation;
One of MARIADB_ROOT_PASSWORD, MARIADB_ALLOW_EMPTY_ROOT_PASSWORD, or
MARIADB_RANDOM_ROOT_PASSWORD (or equivalents, including *_FILE), is
required. The other environment variables are optional.
This is for the initialization of the db-data volume. Once initialized, these environment variables are optional.

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