Docker MYSQL '[2006] MySQL server has gone away' - mysql

i have a little problem with mysql server.
i created my docker-compose.yml, but when i want to access to phpMyAdmin (localhost:8080), an error message appeared sais that:
"phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. Please check the values ​​of host, username and password in the configuration and make sure they match the information provided by the MySQL server administrator".
Here is my docker-compose file and thanks for helping me
version: '2'
services:
apache:
image: rafaelcgstz/magento2
# build: .
ports:
- 80:80
- 9001:9000
# - "35729:35729" # live reload
volumes:
- ./src:/var/www/html
- ~/.composer:/var/www/.composer
- ~/.npm:/var/www/.npm
# - ~/.nvm:/var/www/.nvm
environment:
XDEBUG_CONFIG: "remote_host=localhost"
PHP_IDE_CONFIG: "serverName=Docker"
depends_on:
- db
links:
- db
networks:
- magento-network
db:
image: mariadb
ports:
- 3300:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=magento
- MYSQL_USER=magento
- MYSQL_PASSWORD=magento
volumes:
- dbdata:/var/lib/mysql
networks:
- magento-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- PMA_HOST=db
- PMA_USER=root
- PMA_PASSWORD=root
- MYSQL_ROOT_PASSWORD=root
ports:
- 8080:80
networks:
- magento-network
redis:
image: redis
ports:
- 6379
networks:
- magento-network
redis-session:
image: redis
ports:
- 6379
networks:
- magento-network
mailhog:
image: mailhog/mailhog
ports:
- 1025:1025
- 8025:8025
networks:
- magento-network
networks:
magento-network:
driver: bridge
volumes:
dbdata:
driver: local

Seems like you have a typo in mariadb service definition:
ports:
- 3300:3306
You configured port mapping so that container is reachable at 3300 but you did not pass this information to PHPMyadmin. As a result connection attempt just times out.
Side note: you do not need to expose port for database at all - other containers will communicate with it using Docker's virtual network and for local access you can use docker container -it exec <container-id> mysql... or docker-compose exec db mysql...

while it appears there is a typo for the port with this post I want to point out that it's important to make sure that your user has access 'TO' and 'FROM' the appropriate IP.
This is an (wide open -adjust as needed) example for updating the access domain when running adminer / phpadmin via docker:
GRANT ALL ON . TO 'admin'#'172.18.0.0/255.255.255.0' IDENTIFIED BY 'password' WITH GRANT OPTION;
p.s. I'm adding this answer here because I also landed on this page with the same error.

I solved my problem with this command:
docker-compose destroy
sudo rm -rf db/
docker-compose up -d
docker-compose web
composer install
symfony d:m:m --no-interaction
symfony d:f:l --no-interaction

Related

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!

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 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

can I Use mysql,oracle database as well as sql server on Same system

I have installed SQL-Server 2008 r2 on my system,but now i need to install MYSQL too. Is it possible to install MYSQL and SQL-Server side-by-side. Does installing both SQL-Server and MYSQL on same system affect each other?
Yes this is possible. However you have to make sure the ports they listen to are different.By default mysql uses port 3306 and SQL-server uses port 1433.They are both applications like any other application. With different processes, so they should run on the same machine without any conflicts. On setup just make sure you configure the ports well so that they do not use the same port, of which the system too should detect that the port is being used by another application.
The simple and clean answer to your question is Docker, i have created tutorial where MySQL, MSSQL, Oracle, PostgreSQL and MongoDB are setup and running simultaneously in single CentOS system with 3GB of RAM without affecting each other:
https://adhoctuts.com/run-mulitple-databases-in-single-machine-using-docker-vagrant/
https://www.youtube.com/watch?v=LeqkCoX28qg
below is the content of docker-compose.yml file from the tutorial, but you need the other files as well (all files are in following git repository: https://github.com/mamedshahmaliyev/adhoctuts/tree/master/docker/multiple_databases). If you need MySQL and MSSQL only just delete other services from docker-compose.yml and run docker-compose up:
# link to tutorial: https://adhoctuts.com/run-mulitple-databases-in-single-machine-using-docker-vagrant/
version: "3.1"
networks:
docker-network:
services:
# https://hub.docker.com/_/mysql
mysql_persistance: # service name
image: mysql:8
container_name: mysql_p # container_name
command: --default-authentication-plugin=mysql_native_password
volumes:
- /docker/mysql/data:/var/lib/mysql # for data persistance
- /docker/mysql/conf:/etc/mysql/conf.d # put all the custom configuration files from host to container
environment:
- MYSQL_ROOT_PASSWORD=AdHocTuts2019#
ports:
- "3309:3306" # map host port to container port
networks:
- docker-network
#restart: on-failure
mysql_no_persistance:
image: mysql:5.7
container_name: mysql_np
environment:
- MYSQL_ROOT_PASSWORD=AdHocTuts2019#
ports:
- "3308:3306"
networks:
- docker-network
# https://hub.docker.com/_/microsoft-mssql-server
mssql:
image: mcr.microsoft.com/mssql/server:2017-CU8-ubuntu
container_name: mssql
volumes:
- /docker/mssql/data:/var/opt/mssql
environment:
- SA_PASSWORD=AdHocTuts2019#
- ACCEPT_EULA=Y
- TZ=Asia/Baku
- MSSQL_PID=Express
ports:
- "1433:1433"
networks:
- docker-network
# https://hub.docker.com/_/oracle-database-enterprise-edition
# Accept Terms of Service for Oracle Database Enterprise Edition (Proceed to Checkout).
# Then in command line: docker login
# sqlplus sys/Oradoc_db1#ORCLDB as sysdba
oracle:
image: store/oracle/database-enterprise:12.2.0.1-slim
container_name: oracle
volumes:
- /docker/oracle/data:/ORCL # host path must have 777 permission or writable by docker oracle user
environment:
- DB_SID=ORCLDB
- DB_MEMORY=1GB
ports:
- "1521:1521"
networks:
- docker-network
# https://hub.docker.com/_/postgres
postgres:
image: postgres:12
container_name: postgres
volumes:
- /docker/postgre/data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=AdHocTuts2019#
- POSTGRES_USER=postgres
- POSTGRES_DB=docker_db
ports:
- "5432:5432"
networks:
- docker-network
# https://hub.docker.com/_/mongo
mongo:
image: mongo:3.4.21-xenial
container_name: mongo
volumes:
- /docker/mongo/data:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=AdHocTuts2019#
ports:
- "27017:27017"
networks:
- docker-network