I am unable to connect my Sequel Pro to my docker mysql container. I keep getting the following error message:
I have a docker-compose.yml ;
db:
image: mysql:5.7
ports:
- "33071:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=default
volumes:
- ./dump:/docker-entrypoint-initdb.d
- ./db/mysqlconf:/etc/mysql/conf.d
- ./mysqldata:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8677:80"
links:
- db
I have read multiple forum answers on this but none of them helps. The most relevant is this link but this seems to relate to the mysql on the mac as opposed to a docker container. He talks of editing the my.cnf file located in /etc/my.cnf. on Unix/Linux.
However my docker container does not have a my.cnf file.
So I am not sure what to do.
I used the following settings for Sequel Pro connection:
Host: 127.0.0.1
Username: root
Password: root
Database: (left it blank)
Port: 3306
You are mapping port 33071 of your host, to port 3306 of your docker container in your docker-compose file.
ports:
- "33071:3306"
But in your Sequel Pro settings mentioned above, you are using port 3306 of your host. That is incorrect.
Solution:
Use the host port that is mapped with docker container i.e 33071
(Ideally you should have kept the port as 3307 in port mapping).
Related
I am facing a problem trying to use docker and two port forwardings. Basically I have:
A docker container hosting a MySQL database running on port 3306 in the container
The host of the container, where port XXXX is linked to the 3306 of the container with the docker-compose command ports: - XXXX:3306; I can access my container within the host using PhpMyadmin. So, so far so good
I create a bridge with a proxy server on port 3336 created with a command: ssh -i key.pem -R 3336:localhost:XXXX ubuntu#IP
I then have a client (say Mysql Workbench) which is connected to the proxy using another tunnel : ssh -i key.pem -L 3306:3336 ubuntu#IP
I tried to summarize everything in the following picture with XXXX being 3306 (the green box).
When I try to connect to the database using this rather complex method, it succeeds when XXXX=3306. However, when XXXX=8701 for example, it does not work anymore. Do you have any idea why ? The error I get is a classic timeout: UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT
Thank you in advance for your help.
Best,
B
I found the issue, which was related to the docker-compose.yml file;
Previously I had:
version: "3.7"
services:
db:
build:
context: ./database
command: --default-authentication-plugin=mysql_native_password --sql_mode=""
restart: always
cap_add:
- SYS_NICE
volumes:
- db_data:/var/lib/mysql
ports:
- ${MYSQL_HOST_PORT}:${MYSQL_PORT}
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_TCP_PORT: ${MYSQL_HOST_PORT}
env_file: ./.env
The trick was to have the same port in and outside of the container ${MYSQL_HOST_PORT}:${MYSQL_HOST_PORT}
Hope it would help others in the same situation
I have the following docker-compose.yml file:
version: '3.2'
services:
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=verysecret
- MYSQL_DATABASE=yii2advanced
- MYSQL_USER=yii2advanced
- MYSQL_PASSWORD=secret
I run it via docker-compose up -d. But I can't connect from SQL client to my database (I use DataGrip from JetBrains). Here is my configuration:
The password of course is secret. I have already tried to check allowed hosts for yii2advanced user:
As you can see for my yii2advanced connection is allowed from any host. I have tried to change mysqld.cnf and set bind-address = 0.0.0.0. Tried to setbind-address to *. Tried to set not 127.0.0.1 but 172.17.0.1 in the settings of my SQL client. Tried to create manually new one user with full privileges. No effect. The problem still exists. I can't connect to mysql in the container from my localhost.
What is wrong?
By default, docker-compose create a bridge network that will isolate your application from outside including the localhost.
You need to expose 3306 port from isolated network to localhost.
Try this.
version: '3.2'
services:
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=verysecret
- MYSQL_DATABASE=yii2advanced
- MYSQL_USER=yii2advanced
- MYSQL_PASSWORD=secret
ports:
- 3306:3306 # <host port>:<container port>
Good to read docker network section. https://docs.docker.com/network/
The full error is Doctrine\DBAL\Exception\ConnectionException: An exception occurred in driver: SQLSTATE[HY000] [2002] No such file or directory in /app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php on line 113, but that's too long for the title.
I'm trying to set up a Symfony project locally, but I'm struggling to get the database connection to work. My parameters.yml looks as follows
parameters:
database_host: 127.0.0.1
database_port: 3306
database_name: database_name
database_user: username
database_password: password
I've been googling this issue a lot and most people seem to solve the issue by changing database_host from localhost to 127.0.0.1, but this doesn't work for me. The app itself runs via Docker, but I've set up the database connection once via Brew and once with a MySQL server for Mac. In both cases I can connect via the command line and with SequelPro/TablePlus, but whenever I try to access the website through the browser I get the "No such file or directory" error.
I've also tried multiple ways of setting up a Docker MySQL container, but can't get it to work. My docker-compose.yml looks like this;
nginx:
build: nginx
ports:
- "8080:80"
links:
- php
volumes:
- ../:/app
php:
build: php-fpm
volumes:
- ../:/app
working_dir: /app
extra_hosts:
- "site.dev: 172.17.0.1"
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'database_name'
MYSQL_USER: 'username'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password_root'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- my-db:/var/lib/mysql
But whenever I run docker-compose up -d I get the error Unsupported config option for services: 'db'.
Another attempt was adding
mysql:
image: mysql:latest
volumes:
- mysql_data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD='password'
- MYSQL_DATABASE='database_name'
- MYSQL_USER='username'
- MYSQL_PASSWORD='password'
To the docker-compose file, and while it does build the mysql image, I can't seem to connect to it with SequelPro/TablePlus. I ran docker-inspect on the container to get the IP (172.17.0.3), but can't seem to get access to it. I can exec into it, login using mysql -u root and create the required user and database, but then I'm still struggling to actually connect to it.
Running docker ps does show the sql container running btw;
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6de6030791d docker_nginx "nginx -g 'daemon of…" 19 minutes ago Up 14 minutes 0.0.0.0:8080->80/tcp docker_nginx_1
f26b832bb005 docker_php "docker-php-entrypoi…" 19 minutes ago Up 14 minutes 9000/tcp docker_php_1
6c2a9e657435 mysql:latest "docker-entrypoint.s…" 19 minutes ago Up 14 minutes 3306/tcp, 33060/tcp docker_mysql_1
I also thought it might be an issue with changes to the parameters.yml file not properly syncing with the container as I'm using Mac (at my old workplace we had to use docker-sync to make sync changes between our dev environment and the actual container), but when inspecting the container itself using exec I can see the changes in the parameters.yml file.
Could the issue be it trying to connect to a mysql server running outside the Docker container? I'm still very new to Docker so I wouldn't be surprised if that's the mistake. Any tips are appreciated 'cause I'm at a dead end.
Your docker-compose file looks wrong to me, try below docker-compose file.
I removed the links, network is much easier.
version: '3'
services:
nginx:
build: nginx
ports:
- "8080:80"
networks:
- backend
volumes:
- ../:/app
php:
build: php-fpm
volumes:
- ../:/app
working_dir: /app
networks:
- backend
extra_hosts:
- "site.dev: 172.17.0.1"
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'database_name'
MYSQL_USER: 'username'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password_root'
networks:
- backend
ports:
- '3306:3306'
volumes:
- ./my-db:/var/lib/mysql
networks:
backend:
driver: bridge
then use database_host: db in php file.
I would diagnose
Check docker logs in the mysql container => no errors
Login to the mysql container and login to mysql => no errors
Login to mysql from the host (mysql -u username -p since you are mapping to 3306 port of the host)
Make sure mysql.cnf doesn't block connect from outside(check
bind-address in the mysql configuration if it 127.0.0.1 the its only
allow to connect form locally, i would for now make it 0.0.0.0 or
commented that line if exists)
mysqld --verbose --help => you will see all options
mysqld --verbose --help | grep bind-address=> check the bind-address
Make sure the user i tried to login has enough privileges to
connect(SELECT user,host FROM mysql.user;) check your user can
connect from docker network => 172.* or anywhere=> %
I think your issue is with your parameters.yml:
parameters:
database_host: 127.0.0.1
When you run compose, MySQL and PHP will run in their own containers which will have their own IPs: 127.0.0.1 or localhost from the php won't be able to connect to the db container. It's like you deployed PHP on a virtual machine A and MySQL to another virtual machine B, but you try to access MySQL from machine A by using localhost where you should specify machine B IP or hostname.
With Docker Compose the internal DNS will resolve the service name to it's container, so you can use something like:
parameters:
# name of the service in compose should be resolved
database_host: db
The error SQLSTATE[HY000] [2002] No such file or directory may be caused when the client tries to read MySQL socket usually present at /var/lib/mysql/mysql.sock which is probably not present in your PHP container.
I'm trying to containerize my django file, and I keep running into the issue:(2006, ’Can\‘t connect to local MySQL server through socket \‘/var/run/mysqld/mysqld.sock\’ (2 “No such file or directory”)
I found out later mysql.sock is in this location:/tmp/mysql.sock instead of /var/run/mysqld/mysqld.sock, how do I change the location for docker to see /tmp/mysql.sock
Here is my docker-composr.yml:
version: '3'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: somepassword
adminer:
image: adminer
restart: always
ports:
- 8080:8080
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
I have followed the instructions on the mysql docker website to link mysql instance to a container
EDIT: I read another stack overflow similar to this, I changed my django code to 'HOST': '127.0.0.1' in DATABASES now I get : (2006, 'Can\'t connect to MySQL server on \'127.0.0.1\' (111 "Connection refused")')
Your host should be db. When using docker-compose, you address different servers by their service name.
So, in settings.py, you should have:
DATABASES = {
'default': {
'HOST': 'db',
...
}
}
If you want to connect to your containerized MySQL server both inside and outside of the container, you'll first need to make sure the port is mapped on the host machine:
services:
db:
image: mysql
ports:
- "3306:3306"
...
That will allow you to access MySQL using localhost or 127.0.0.1 directly on your host machine.
If you want to be able to run Django in both the web container and also on the host, you'll need to override the DATABASES setting depending upon the scenario. The web container will need to use a HOST value of db, whereas your local machine will need a value of localhost.
I'm using the official MySQL image from docker hub and expose ports 3333:3306 to connect from outside.
I know that I have to change the bind IP inside /etc/mysql/my.cnf to the IP of this container and grant permission for a user like: GRANT ALL PRIVILEGES ON * . * TO 'newuser'#'container_ip'; so I can connect to this container by:
mysql -h container_ip -u root -p
But I received this error
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Btw, I also try to connect from WordPress in another container but it cannot establish the connection. Here is docker-compose.yml
version: '2'
services:
mysqldb:
image: mysql:5.6
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- ./mysql-data:/var/lib/mysql
- ./mysql-import-data:/import-data
ports:
- "3333:3306"
blog:
image: webdevops/php-nginx:ubuntu-14.04
environment:
WEB_DOCUMENT_ROOT: /usr/share/nginx/html
volumes:
- ./blog:/usr/share/nginx/html
ports:
- "8080:80"
depends_on:
- mysqldb
What's the mistake I made with this mysql container? I cannot connect to it.
The IP of the container may change every time docker-compose up. How can I configure it?
Hmm I'm a little confused. From the point of view of the host os, the docker container is bound to one or more network interfaces. In your compose file you are exposing port 3333 to the host. That's what you have to connect to.
Plus you need to use an IP address, otherwise the mysql client will try to connect with a unix socket.
mysql -h 127.0.0.1 --port 3333 -u root -p
If you are trying to connect from inside your blog container then you can use mysqldb as your host with the 3306 port.