Docker connect app to mysql host - mysql

I have an Ubuntu 16.04 installation in a vm and I'm trying to run an app inside a docker container and connect it to a MySQL database on the host
I have something like this.. on the host I have the MySQL:
And when I try to connect my app to the MySQL I got this:
java.sql.exception: cannot create poolableConnectionFactory ( Could not create connection to database server. Attempted reconnect 3 times
I execute my app using a docker-compose:
version: '2'
services:
exo:
image: exoplatform/exo-community:5.0
environment:
EXO_DB_TYPE: mysql
EXO_DB_NAME: user
EXO_DB_USER: root
EXO_DB_PASSWORD: "my-pass"
EXO_DB_HOST: "127.0.0.1"
EXO_ADDONS_LIST: exo-jdbc-driver-mysql
expose:
- "8080"
- "3306"
ports:
- "8080:8080"
volumes:
- exo_data:/srv/exo
- exo_logs:/var/log/exo
volumes:
exo_data:
external:
name: exo_data
exo_logs:
external:
name: exo_logs
networks:
default:
driver: bridge
As you can see I try to connect to the MySQL default port... And I just saw that 3306 port is listening on the host.
I also tried to add the following line ( saw on another post)
In my.cnf I have the bind-address 0.0.0.0.
And MySQL is running:
root#xxx/# mysqladmin -u root -p status
Uptime: 4784 Threads: 4 Questions: 17 Slow queries: 0 Opens: 114 Flush tables: 1 Open tables: 33 Queries per second avg: 0.003
I'm using docker on bridge so I guess I just need to add the port on docker composer in order to connect docker and host. But nothing seems to works. Can someone point me to right direction?

The problem is that you are trying to connect to the host from the container using localhost. This won't work as the container has its own ip and localhost will hit the container and not the host.
Connecting from container to host is a very frequent question on stackoverflow and you can find many answers in From inside of a Docker container, how do I connect to the localhost of the machine?
The easiest way (though not recommended) is to use network_mode: host inside docker-compose file. In this case, the container will share the networking with the host and localhost will hit the host machine as well.

I think you need specify IP address:
mysql -u root -p -h 44.55.66.77
and give privileges for your:
DB GRANT ALL ON yourDatabase.* TO root#’1.2.3.4’ IDENTIFIED BY ‘my-pass’;
to get IP address inside container you can run this:
awk 'END{print $1}' /etc/hosts

Related

Cannot connect to mysql server (root) inside docker container executed with docker-compose: Access denied

I am running a mysql server inside a docker container using docker-compose, here is my yaml file:
version: '3'
services:
mysqltest:
image: mysql
network_mode: host
ports:
- "3306:3306"
volumes:
- "/home/myuser/bds/mysql/:/var/lib/mysql"
user: "1000:1000"
environment:
- "MYSQL_ROOT_PASSWORD=mysecret"
The container loads file with the docker-compose up command, but when i try to connect from the host machine to the mysql server with user root, it fails:
mysql -h 127.0.0.1 -u root -pmysecret
ERROR 1045 (28000): Access denied for user 'root'#'127.0.0.1' (using password: YES)
However, i can connect to the server inside the container using the same command, if i start the container using docker command line:
docker run -it --rm --name mysqltest --user 1000:1000 -v /home/myuser/bds/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysecret -p 3306:3306 -d mysql
I tried setting network_mode to host, none and also without specifying it, but the result is the same using docker-compose.
What could be wrong with my YAML file so that i cannot connect as when i use docker command ?
Thanks in advance
I assume you didn't change your default Docker configuration. By default Docker will run in the network mode: 'bridge' and not host. See the difference here.
You can check using docker inspect container.
When you just start the container using the command you will start your container in bridge mode and the container port 3306 will be mapped on 3306. This will not happen when you try host. Again see the link for the difference.
So update your docker-compose.yaml and define bridge as network mode:
version: '3'
services:
mysqltest:
image: mysql
network_mode: bridge
ports:
- "3306:3306"
volumes:
- "/home/myuser/bds/mysql/:/var/lib/mysql"
user: "1000:1000"
environment:
- "MYSQL_ROOT_PASSWORD=mysecret"
SOLVED !! The issue was originated from setting different mysql root passwords with the different docker commands:
The first time i was testing the container i user std docker command and the environment MYSQL_ROOT_PASSWORD=othersecret. Everything worked fine. i connected from host using mysql command and restored a database
Once i finished testing, i wrote yaml file, but with the root password i wanted to use for producction:
environment:
MYSQL_ROOT_PASSWORD: "mysecret"
But then, when i started the container with docker-compose and the yaml file, i could not connect from host using mysql command. It seems the original root password ("othersecret") was written to the user table inside the mysql schema and that is why i could not connect using the password set in the yaml file. I cleaned up the mysql directory and started the container using docker-compose (without specifying a network mode) and finally i was able to connect from host.

I can't connect to mysql in docker from my localhost with DataGrip database client

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/

Connecting to MySql server running in a Docker Container

I have a little problem, I can't seem to connect to mysql server community edition running inside a docker container.
I can easily connect to mysql server using the cli by using:
docker exec -it mysqlserver mysql -uroot -p
But if I try to connect with any other database connector, like DataGrip or MySql Workbench, I get an access denied.
But I changed nothing in the configuration files. I set ip as localhost, using the default 3306 port that the container exposed. username I keep as root and the password is exactly the same but it still keeps failing.
Am I missing something, not understanding anything properly?
Some help would be greatly appreciated!
Extra info: I am using MacOS, running the container with Docker for Mac and I am using as of this moment the latest MySql database version.
your container doesn't contain other connectors, try to publish port when you run your container with docker run --name mysql -p 127.0.0.1:3306:3306 .... to link port. And you can connect you to the container with your local cli. Try this doc
Below is the docker-compose.yml that I used to start up a MariaDB instance for testing some queries. MariaDB is API-compatible with MySQL, so no difference.
version: "2"
services:
db:
image: bitnami/mariadb:latest
volumes:
- ./mariadb/data:/bitnami
ports:
- "9001:3306"
environment:
MARIADB_ROOT_PASSWORD: ChangeMeIfYouWant
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- "9010:80"
links:
- db:db
The image is downloaded and the container created and started via docker-compose up. Afterwards, I can easily connect to it using JetBrains DataGrip.
host: localhost
port: 9001
user: root
password: ChangeMeIfYouWant

How to connect to a MySQL Docker Container via SSH?

When I try to tunnel via SSH to the Host Mashine (vServer) and then try to connect via the internal docker Container-IP then I can't connect to MySQL.
This is my docker-compose file.
version: '2'
services:
mysql:
build: ./mysql
environment:
MYSQL_ROOT_PASSWORD: test
volumes:
- ./db:/var/lib/mysql
The only solution I found was to forward the MySQL-Port of the mysql container to the Host-Mashine.
version: '2'
services:
mysql:
build: ./mysql
environment:
MYSQL_ROOT_PASSWORD: test
volumes:
- ./db:/var/lib/mysql
ports:
- 3306:3306
Then I am able to connect via the Host IP to MySQL but this is without SSH its direct via TCP and the port.
This is a No-Go for me to bring the MySQL Service into the internet.
Reasons can be found here https://security.stackexchange.com/questions/63881/is-it-not-safe-to-open-mysqls-port-to-the-internet why it is not a good practice to bring your mysql port into the internet.
So what is a good practice to connect to my docker mysql container with SSH but keep the mysql ports closed?
One simple way is to bind the MySQL port only to the localhost address. That assumes the host has a mysql client available outside of Docker.
ports:
- 127.0.0.1:3306:3306
You could also omit the ports section completely (no port binding at all), and use the mysql client that's already inside the container.
docker-compose exec mysql bash
Then run the mysql command inside the container to do whatever queries you want to do.
An easy way to achieve this is to forward the ssh port of the docker conatiner to some port on your host, i.e.
ports:
- 22:<some free host port>
and then access the container via ssh to the host port you used. Note, that it is a bad idea to use port 22, since that will cause a conflict with the ssh server running on your host.

Connect to mysql inside docker

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.