Docker Compose - Connection to Phpmyadmin and MysQL not working - mysql

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!

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?

connecting to a mysql container: what's the equivalent of localhost inside a docker compose project?

I have this docker-compose.yml file, which has one service (log_app) trying to write to a mysql database (mydb).
version: '3.4'
services:
mydb:
container_name: mysql_data
restart: always
image: mysql:8.0.22
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PW}
MYSQL_DATABASE: ib
MYSQL_PASSWORD: ${MYSQL_PW}
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- ./mysql-data:/var/lib/mysql
tws:
build: .
container_name: ib_logger_app
volumes:
- ./ib/IBController.ini:/root/IBController/IBController.ini
- ./ib/jts.ini:/root/Jts/jts.ini
environment:
TRADING_MODE: ${TWS_TRADING_MODE}
TWSUSERID: ${TWS_USER_ID}
TWSPASSWORD: ${TWS_PASSWORD}
FIXUSERID:
FIXPASSWORD:
XVFB_ARGS: -ac -screen 0 1024x768x16 +extension RANDR
restart: always
ports:
- 5901:5900
depends_on:
- mydb
log_app:
build: log_app/ib_client
environment:
- IB_GATEWAY_URLNAME=tws
- IB_GATEWAY_URLPORT=4004
- MKT_DATA_TYPE=4
restart: on-failure
depends_on:
- tws
- mydb
volumes:
mysql-data:
When I run this on a bare metal server, everything works fine, but in that situation, I connect to localhost on port 3306. This doesn't seem to work here, though. I get
Can't connect to MySQL server on 'localhost' (99)
I've been reading that 3306 is still the right port, but localhost and 127.0.0.1 are wrong now. Neither works for me.
I've also seen some answers on this site that recommend changing localhost to host.docker.internal, but that gives me this:
Can't connect to MySQL server on 'host.docker.internal' (111)
The host name will be the container name as defined in your docker-compose file.
In your example, you should use mydb:3306 instead of localhost:3306
EDIT:
one way to ensure that is to exec into the container that you want to connect from and run:
telnet mydb 3306
and see if it works (but t might require additional installation of telnet).

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = ms_api_shop

I am using Lumen with Docker to create simple API for authentication. After installing LumenPassport, I cannot migrate the database. I can easily connect to the MySQL db with Dbeaver.
I have already created one Lumen Docker project for the same purpose, it is the second. The first one worked without a problem. Moreover, I have checked the MySQL databases, ms_api_shop was there
Errors:
Here is my docker-compose
services:
nginx:
build:
context: .
dockerfile: docker/Nginx.Dockerfile
image: nginx
ports:
- 8092:80
depends_on:
- fpm
volumes:
- ./:/var/www/lumen-docker
links:
- mysql
fpm:
build:
context: .
dockerfile: docker/fpm.Dockerfile
volumes:
- ./:/var/www/lumen-docker
depends_on:
- mysql
links:
- mysql
mysql:
image: mysql:5.7
ports:
- 33006:3306
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_DATABASE=ms_api_shop
- MYSQL_ROOT_USER=
volumes:
- mysql-data:/var/lib/mysql
volumes:
mysql-data:
And env:
DB_HOST=mysql
DB_PORT=33006
DB_DATABASE=ms_api_shop
DB_USERNAME=
DB_PASSWORD=
In your docker-file, you are binding the 33006 container port to the 3306 of the host port. In case you want to access the MySQL, you should use 3306 not 33006 as you did in your .env
I have a Laravel app running in Docker and a part of my docker config looks like:
But I personally feel that they should be within a docker network, look at the last two lines of the code in my docker-compose.yml below:
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./mysql:/var/lib/mysql
networks:
- laravel
According to my knowledge, docker containers can communicate with each other when they are on the same network. You seem to have not connected these two docker in docker-compose yet. To get network information from a container, you can use the below command.
$ docker inspect --format='{{json .NetworkSettings.Networks}}' <docker_container_name>
In case you need to connect these two containers, follow these steps:
First, you create a network
$ docker network create my-net
Second, you connect container to the network.
$ docker network connect my-net <docker_container_name>
Don't forget to connect these two containers to the network.

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

docker-compose run two instance of mysql

I want to run two instances of mysql using docker-compose.
I'm running MySQL in a Docker container and I have another Docker container running a python script to access the MySQL database.
One works fine on port 3306. In order to get two working, I thought I would just run the other one on a different port. But when I change it to a different port (e.g. 6603), but when I do, I get the below error:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'mysql:6603' (111 Connection refused)
I have read every question on s.o. I can find that seems relevant but none of the solutions work. I feel certain the fix will involve changing a line or two of configuration but I've spent many hours on this so far so any help would be greatly appreciated.
The docker-compose script is below (works fine if 6603 is replaced with 3306).
server:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8080:8080"
links:
- mysql:mysql
volumes:
- ../py:/app
tty: true
mysql:
image: mysql
expose:
- "6603"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
And it is being accessed like this:
cnx = mysql.connector.connect(user='root', password='password',
host='mysql',
port="6603",
database='project')
Try to specify another port for MySQL by modifying its my.cnf file.
Have eventually found a couple of ways that work. The neatest one is for each app to create a network and connect the containers to it.
If each app uses a different network then mysql can run on 3306 on that Docker network and can be accessed on mysql://3306 from app1 and mysql2://3306 from app2. (Assuming you name you give the mysql service for app 2 is mysql2).
The Docker file with the new lines is below:
server:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8080:8080"
volumes:
- ../py:/app
tty: true
networks:
-net
mysql2:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
networks:
-net
networks:
net:
driver: bridge
The Docker file for the second app is identical except the names are different (I put a 2 after each for simplicity).
server2:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8081:8080"
volumes:
- ../py:/app
tty: true
networks:
-net2
mysql2:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
networks:
-net2
networks:
net2:
driver: bridge