can not connect to mysql in docker's bridge network - mysql

I created a MySQL container but can only connect to the database from the host.
I start the MySQL container in this file:
docker-composer.yml:
version: '3.3'
services:
db:
image: mysql:5.7
ports:
- "3307:3306"
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
when run docker-compser up , I got this error:
MySQL Connection Error: (2002) No route to host
But I can succeed to connect to the container from the host, like this:
mysql -u root -p -h 127.0.0.1 -P 3307

You can try to connect without a port specifying:
WORDPRESS_DB_HOST: db

It turns out to be a firewall problem. I am using fedora 32 system. And the docker interface using the default zone which will block 80 port and 3306 port.
So, after I bind my docker interface to trusted zone and added some port everything worked.

Related

Docker connect to mysql container via remote software

I have created my docker environment, but is having trouble connecting to mysql via remote software e.g. Workbench or Sequel Pro. Below is my yml file for mysql specific.
mysql-dev:
image: mysql:8.0.17
container_name: bcdb
volumes:
- mysql-dev:/var/lib/mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_ROOT_PASSWORD: "rootpwd"
MYSQL_USER: 'testuser'
MYSQL_PASSWORD: 'testpassword'
MYSQL_DATABASE: 'testdb'
MYSQL_ROOT_HOST: '%'
ports:
- "3301:3306"
Note: The mysql-dev is connected to volumes mysql-dev:
No matter if I type in as hostname:
localhost, 127.0.0.1, (ip address of the mysql server) etc. there is no connection available. Error message is: "MySQL said: Can't connect to MySQL server on '127.0.0.1' (61)"
Just a thought: Do I need to expose the ports in the Dockerfile, or am I missing something basic?
I'm starting the container by typing: "docker-compose up -d" - nothing else atm.
Try bridge network, here is a working yaml config,
version: "3"
services:
mysql-dev:
image: mysql:5
container_name: bcdb
# volumes:
# - mysql-dev:/var/lib/mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_ROOT_PASSWORD: "rootpwd"
MYSQL_USER: 'testuser'
MYSQL_PASSWORD: 'testpassword'
MYSQL_DATABASE: 'testdb'
MYSQL_ROOT_HOST: '%'
ports:
- "3301:3306"
networks:
- mysql-main
networks:
mysql-main:
driver: bridge
Test connection:
➜ ~ nc 127.0.0.1 3301
J
5.7.34OEaDdC���;2
Bpq?7O{mysql_native_password
^C
➜ ~
For the details, you can refer this excellent blog post.

DOCKER and WordPress: Error establishing a database connection

I am new to docker and i have this docker .yml file (below) and i have a local set up of my WordPress site, i have imported a sql dump into a database in the 'container_web'. The problem i am having is that when i try to connect WordPress to the database inside the container i get this error "Error establishing a database connection." For the host i've used localhost, 127.0.0.1, mysql and none of them seems to work.
Do you know or could direct me in the right direction on what i should be checking in order to connect my WordPress install to the database?
services:
web:
build: .
container_name: 'container_web'
dns: '8.8.8.8'
volumes:
- ./:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
depends_on:
- db
ports:
- 80:80
restart: always
environment:
WORDPRESS_ENV: 'development'
db:
container_name: 'container_db'
image: mysql:5.7
ports:
- 3306:3306
volumes:
- ./:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydatabase
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
mysql-data: {}

docker mysql wordpress port doesn't connect

I downloaded the mysql and wordpress images. Mysql ports are
3306 localhost:32781
33060 localhost:32780
Wordpress configuration is
WORDPRESS_DB_HOST 192.168.99.100:32774
MYSQL_ROOT_PASSWORD and WORDPRESS_DB_PASSWORD are the same
I try to connect to wordpress with
http://192.168.99.100:32774/
I get the message
This site can’t be reached
How do I have to configure the ports of mysql and wordpress?
CONFIGURATION MYSQL
WORDPRESS
Error trace
From what you can find on the docker configuration page, you should take this example and modify it to your needs.
There is the following docker-compose file that will launch a wordpress in a minute:
https://docs.docker.com/compose/wordpress/
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
From that file you have various options like, using docker compose tool (https://docs.docker.com/compose), or if you have a swarm running you could use docker stack command(https://vsupalov.com/difference-docker-compose-and-docker-stack/) or you can divide the configuration of both elements and create separate Dockerfile's(the configuration of a Docker file differs from what you can see on docker-compose so take the information an create your own) and launch them separated, you should launch mysql first as wordpress depends on a bbdd running first.
The easiest way to implement this is using docker-compose. Here is an example:
version: '3.2'
services:
database:
image: mysql:5.7
volumes:
- my_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wp_user
MYSQL_PASSWORD: password
wordpress:
depends_on:
- database
image: wordpress:php7.3-apache
ports:
- '8000:80'
restart: always
environment:
WORDPRESS_DB_HOST: database:3306
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: password
working_dir: /var/www/html
volumes:
my_data: {}
A few notes: the database doesn't mount any port on host because it doesn't need to. If you don't want to use docker-compose you can run docker run commands for this but then you have to create your own network for the containers and attach them to it.
Wordpress will be available on http://localhost:8000.
WORDPRESS_DB_HOST is the connection to the database and you won't be able to access that through http anyway.
Hope this helps you.

Docker can't connect to MySQL running docker-compose up

I am using this Docker config: https://github.com/romaricp/kit-starter-symfony-4-docker
I start the environment by using:
docker-compose build
followed by:
docker-compose up -d
Everything is running fine but there is a problem with MySQL service. I get an Symfony error:
"An exception occurred in driver: could not find driver"
as well as PMA error when I try to login to DB:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed:
Name does not resolve
Any idea how could I make it work?
docker-compose.yml:
version: '3'
services:
apache:
build: .docker/apache
container_name: sf4_apache
ports:
- 80:80
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/wwwroot/sf4
depends_on:
- php
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
php:
build: .docker/php
container_name: sf4_php
volumes:
- .:/home/wwwroot/sf4
environment:
- maildev_host=sf4_maildev
depends_on:
- maildev
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
ports:
- 8080:80
links:
- mysql
maildev:
image: djfarrelly/maildev
container_name: sf4_maildev
ports:
- 8001:80
Also I opened the mysql logs and I see this:
2019-04-07T12:00:30.943414Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.15' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
Maybe the port is wrong and that's why I can't connect?
For phpmyadmin service, i think you should set the PMA_HOST and PMA_PORT environment variables like this:
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- 8080:80
links:
- mysql
Your mysql container should have the command instruction at the start to set the authentication plugin (there is an issue with the connectors with mysql 8), more details here
mysql:
image: mysql
container_name: sf4_mysql
command: "--default-authentication-plugin=mysql_native_password"
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
in your Symfony app, the connection string is located in .env file, and it should have the following format :
DATABASE_URL=mysql://mysql_user:mysql_user_password#mysql_host:mysql_port/db_name
mysql_user: your mysql user (ex: root)
mysql_user_password: the user's password (ex: root)
mysql_host: is should contain your mysql container service name
located in your docker-compose.yml file (mysql in this case)
mysql_port: mysql container internal port (3306, in this case)
db_name: the database you want to connect to.
the DATABASE_URL can look like this :
DATABASE_URL=mysql://root:root#mysql:3306/sf4
stop your containers after these changes and start them up again.
Hope this will help.
You have to expose the mysql port 3306:
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
networks:
- default
I think you're having trouble linking the containers. I suggest that you use a custom network instead of linking. That all containers can see each other

docker-compose mysql container denies access to wordpress container

I have a problem with mysql 5.7 container denying access to wordpress container. I'm using docker-compose and I'm running docker on Mac OSX. Docker should be on latest version available.
Here's my docker-compose.yml
version: '2'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
container_name: wordpress
ports:
- "8000:80"
- "443:443"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: blog
WORDPRESS_DB_USER: blog_admin
WORDPRESS_DB_PASSWORD: userpasswd
networks:
- wordpress_net
db:
image: mysql:5.7
container_name: db
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpasswd
MYSQL_DATABASE: blog
MYSQL_USER: blog_admin
MYSQL_PASSWORD: userpasswd
networks:
- wordpress_net
networks:
wordpress_net:
volumes:
db_data:
Logs from db container are:
2017-05-12T23:28:06.138429Z 321 [Note] Access denied for user 'blog_admin'#'172.19.0.3' (using password: YES)
Logs from wordpress container are:
MySQL Connection Error: (1045) Access denied for user 'blog_admin'#'172.19.0.3' (using password: YES)
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'blog_admin'#'172.19.0.3' (using password: YES) in - on line 22
docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b02f0146fe7 wordpress:latest "docker-entrypoint..." 25 minutes ago Up 26 seconds 0.0.0.0:443->443/tcp, 0.0.0.0:8000->80/tcp wordpress
5d932ed6c269 mysql:5.7 "docker-entrypoint..." 25 minutes ago Up 25 minutes 0.0.0.0:3306->3306/tcp db
What have I tried:
Restarting docker host.
docker-compose rm -v and then docker-compose up -d again.
Logging in with those user credentials and root credentials outside of wordpress container.
Removing docker images and pulling them again from scratch.
Using root credentials in WORDPRESS_DB_HOST, WORDPRESS_DB_USER
I can see all the env vars for db when I connect to db container. Wordpress container keeps restarting it self. I saw one answer on stack overflow which recommended flushing privileges and setting new user account but I want to know if I'm doing something wrong that could cause this problem to appear again on other machine.
Change:
WORDPRESS_DB_USER: blog_admin
WORDPRESS_DB_PASSWORD: userpasswd
To:
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: rootpasswd
And then:
docker-compose up -d --build
Your username Blog_admin doesn't have access to create database.
What have I done:
docker-compose rm -v hasn't worked for me as I've always used docker-compose down to shutdown containers. And I think this is the root of the problem.
I deleted the folder with my docker-compose.yml and created a new one.
Then I created a compose file with just the config for mysql container, launched it and tried to connect to the mysql server as root.
It worked. Then I had to stop the container with docker stop containerID.
Then I ran docker-compose rm -v(For some reason rm -v works only when you stop the container. Not when you use docker-compose down this caused the db's state to persist, as I used a volume for the db container) and completed the yml file with the wordpress container config.
I've ended up with something like this:
version: '2'
services:
wordpress:
image: wordpress:latest
container_name: wordpress-blog
depends_on:
- mysql
ports:
- "8000:80"
- "443:443"
restart: always
environment:
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpress
mysql:
image: mysql:5.7
container_name: mysql-db
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: admin
MYSQL_PASSWORD: password
NOTE: I had a problem previously not only connecting to the database from the wordpress container, but also from the db container itself. The method I described above helped me to solve this issue.
Simple solution
change
volumes:
- db_data:/var/lib/mysql
to something like
volumes:
- db_data:/var/lib/mysqlx