Connect to mysql inside docker - mysql

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.

Related

Access locally mysql on docker from remote through SSH tunnel

I'm trying to access my mysql database from workbench through SSH tunnel, mysql is in a container (working with docker-compose).
Here is my mysql container config in docker-compose :
mysql:
image: mysql:5.7
container_name: mysql
expose:
- 3306
ports:
- '3306:3306'
volumes:
- ./mysql:/var/lib/mysql
environment:
- "MYSQL_ROOT_PASSWORD=#####"
- "MYSQL_DATABASE=#####"
- "MYSQL_USER=#####"
- "MYSQL_PASSWORD=#####"
restart: always
But I'm getting a "Failed to Connect to MySQL" error, just like if my ports were not well reforwarding from SSH to MySQL container (and so, not hitting the good door).
Please note I did not allowed remote access from mysql, as I want to only keep SSH/localhost access possible.
Do you have any idea? Thanks
You are already exposing port 3306, so you should be able to access it from your system with below command -
mysql -u <mysql_user> -p -h localhost -p 3306
If not accessible, try if you are able to access it inside the docker session by logging in to mysql container in interactive mode.

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/

Sequel Pro gives 'Connection failed!' error using Docker Mysql container

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

Docker connect app to mysql host

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

How to connect to mysql created by docker-compose

I can not connect to Mysql created with Docker in local environment (Mac OS X).
I have created the following configuration file.
version: '2'
services:
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: mysqldatabase
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
MYSQL_ROOT_PASSWORD: password
ports:
- "33333:3306"
container_name: mysql-db
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
driver: local
Then, I started the docker container of mysql with the following command.
$ docker-compose up -d
$ docker start mysql-db
Up to this point there was no problem, but an error occurred when trying to connect to mysql.
$ mysql -p 33333 -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Please tell me how to deal with it.
Try mysql -h 0.0.0.0 -P 33333 -u root -p.
Note port here has upper case P and password has lower case p.
Hope this helps!
I think if you connect from the host to the container. mysql will see the connection from non-local host, so disconnect it. You can change mysql config to allow other hosts, or connect from inside that container using docker exec -it mysql ...