Can't connect to mysql container from localhost - mysql

I'm trying to set up a MySQL container for developing.
So I used docker-compose to set it up.
The container and the mysql looks OK. The thing is that I want to connect to it from a DBeaver client and I can't find how I do it.
Here is my docker-compose.yml:
version: '2'
services:
db:
image: 'mysql:5.7'
volumes:
- '~/dev/dbs-data/mysql:/var/lib/mysql'
restart: 'always'
expose:
- '3306'
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: 'pass'
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'pass'
When I try to connect it from DBeaver I get:
java.sql.SQLException: null, message from server:
"Host '172.18.0.1' is not allowed to connect to this MySQL server"
UPDATE
I'm not trying to connect using the IP 172.18.0.1. I tried to connect using localhost:3306, 127.0.0.1:3306 and with the sub IP docker gave it 0.0.0.0:3306
UPDATE
After having success connecting on my Mac, I tried again with DBeaver on my linux and again:
Tried to connect with other tool, mysql workbench:

As you can see in the official image documention :
MYSQL_ROOT_HOST : By default, MySQL creates the 'root'#'localhost' account. This account can only be connected to from inside the container, requiring the use of the docker exec command as noted under Connect to MySQL from the MySQL Command Line Client. To allow connections from other hosts, set this environment variable. As an example, the value "172.17.0.1", which is the default Docker gateway IP, will allow connections from the Docker host machine.
So you have to set the MYSQL_ROOT_HOST variable with the address 172.18.0.1 that you can see in the error message.

On Docker, run this command to create a MySql container and it will expose the ports to the outside world of docker.
docker run --name <mysql-container-name> -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<root-password> -e MYSQL_USER=root -e MYSQL_PASSWORD=<user-password> -d mysql:latest
Few points to note:
You may see below error when trying to connect with DBeaver:
Public Key Retrieval is not allowed
Solution: When creating a new connection on DBeaver, go to Driver Properties look for allowPublicKeyRetrievel and set it to TRUE. Also, if needed set useSSL to FALSE.
Test your connection from DBeaver or any other clients, and it should be working.

I am new to docker and was experiencing the same issue in Linux, it was an issue with the addresses allowed to accept connection; here is what worked out for me:
Find the MySql configuration file named mysqld.cnf
It would be: /etc/mysql/mysql.conf.d/mysqld.cnfOr if you have your own configuration file.
Edit the bind-address key in it. Set it as: bind-address = 0.0.0.0
This will allow to accept connections from any IP address Restart docker-compose by $ docker-compose down$ docker-compose up
Wait for MySQL to start, it should work fine now.

Related

How to connect to mysql docker container on a remote host from mysql workbench?

I've set up a docker container running a mysql instance on a remote computer I have. In the past this hasn't been an issue but for some reason I can't get it to work now. I am unsure what the issue might be. I am using docker compose and I can't seem to connect through mysql work bench on a different computer even those the container is running. Here are my details:
docker-compose.yaml
version: '3.7'
services:
api:
image: api
restart: unless-stopped
container_name: api
build: ./node/
ports:
- 3008:3008
mysql:
image: mysql
restart: unless-stopped
container_name: mysql
environment:
MYSQL_DATABASE: pitapaldb
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
build: ./database/
ports:
- 3306:3306
networks:
default:
external:
name: my-net
database/Dockerfile
FROM mysql
COPY init.sql /docker-entrypoint-initdb.d
database/init.sql
CREATE DATABASE mydb;
USE mydb;
SET SQL_SAFE_UPDATES = 0;
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'password';
flush privileges;
CREATE TABLE carts (
id int PRIMARY KEY AUTO_INCREMENT,
lat float,
lon float,
address varchar(255),
status boolean,
city_id int
);
container is definitely running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
784cf75183f4 mysql "docker-entrypoint.s…" 2 minutes ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
But when I try to connect via workbench I get 'unable to connect'. I've tried both username user and root with password password. The IP address I use definitely should work because I have other services operating from it with no issue:
#LoF10 Here is a quick list of things to check:
Can you connect within the docker network on the machine running the MySQL docker? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it --network my-net mysql:5.7 mysql -h mysql -uroot -ppassword
If not, there may be a problem with your MySQL config, MySQL data, or the initialization of the container. These are what #JorgeCampos is suggesting you verify. Since you are pulling directly from MySQL's Docker Hub entry, the config should be set properly to allow remote connections. If good, proceed. FYI, you will know you've connected successfully if you see mysql> on the terminal. To exit: \q.
Can you connect on exposed port on the localhost of the machine running the MySQL docker? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it --network host mysql:5.7 mysql -h 127.0.0.1 -uroot -ppassword
Make sure to use the IP used above and NOT localhost. This is b/c the MySQL client has special handling of the 'localhost' keyword by looking for mysqld locally. Using the 127.0.0.1 forces MySQL to connect via a proper socket connection. If you are not able to connect, then there is a problem with mapping the container's port to your host. If good, proceed.
Assuming both machines are on the same network and the machine that has MySQL Workbench also has docker, can you connect using the IP of the machine running MySQL container e.g. 10.0.0.4? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it mysql:5.7 mysql -h 10.0.0.4 -uroot -ppassword
If not, you may want to verify if you can:
Ping the 10.0.0.4 machine
If there are any firewall rules that prevent its proper exposure to the network. This happens commonly with Windows' default Firewall...
If on AWS, there are a number of reasons why you might not be able to reach if it has been properly assigned a Public Port e.g. Security Groups, Route Tables, Internet Gateway, etc.
Once you are able to proceed from 3 above, then you should be able to connect using MySQL Workbench as you've described.
Hope those help. Any more detailed recommendation will require you sharing more about your local networking setup (OS, Physical/Virtual, how you are determining IP's, etc).

Not able to connect MYSQL from docker

I have downloaded a MYSQL docker image and am trying to connect to MYSQL using the host given in the IPaddress section of Docker inspect (say 172.17.0.2), port 3306 , username :root , and database: MYSQL.
I am trying to run an R script with the following Database connection:
con <- dbConnect(RMySQL::MySQL(),
dbname = “mysql”,
host=“172.17.0.2”,
port=3306,
username = “root”,
password = )
But I am getting an error stating the host does not exist. I even tried using various options of host like “localhost”. But am still failing to connect, with the error message:
Error in .local(drv, …) :
Failed to connect to database: Error: Can’t connect to MySQL server on ‘172.17.0.2’ (0)
Calls: dbConnect -> dbConnect -> .local -> .Call
Please help on this.
Trying to connect using localhost, meaning the localhost of R container, not the DB.
You can connect with the container IP directly but in case of Docker the IP changing frequently if you restart the DB the container the IP will be changed.
Better to use Docker-compose, which will keep both containers in the same network and R container will be able to connect using the name of the container.
Also, verify the logs of MySQL container is it up or you able to connect from MySQL client?
You can try something like
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: my_db
MYSQL_USER: my_user
MYSQL_PASSWORD: my_pass
web:
depends_on:
- db
image: r-base:latest
ports:
- "8000:80"
restart: always
volumes:
db_data: {}
So, in this case, your MySQL connection string will be
dbname = “mydb”, host=“db”, port=3306, username = “root”, password =root_password
Or the other option is to pass host IP to the R container instead of using container IP.
docker run -it --add-host=db:192.168.x.x r_base_image
So now the host will be
dbname = “mydb”, host=“db”
You need to go to Mysql configuration file (my.cnf) and bind the IP address.
something like :
[mysqld]
bind-address = xx.xx.xx.xx
if you dont know the ip , use 0.0.0.0
it means your host mysql can be gloabaly use for all ip addresses.

Docker Compose - Creating and connecting to mysql container

Having a hard time trying to get to grips with mysql in docker. I have got the container running with docker-compose, but I am not able to connect to the database via any tools such as phpmyadmin, workbench or tableplus.
I have connected directly to the running container and run
mysql -uroot -p
and entered the root password which I have passed, but this fails with this error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Here is my docker-compose.yml file:
version: '3'
services:
db:
image: mysql
restart: always
environment:
MYSQL_DATABASE: quotes
MYSQL_USER: quotes
MYSQL_PASSWORD: P#KhzKZp)56sU8n+
MYSQL_ROOT_PASSWORD: +\VrLG*<t5sq[\\shR29u#n~A3^Jp*
ports:
- '3306:3306'
volumes:
- /private/mdbdata/quotes:/etc/mysql/conf.d
expose:
- '3306'
Been on this for days... hope someone can help!
I think your container is looking for a MySQL server on 'localhost', which WILL NOT WORK. 'localhost' to a container is the container itself - NOT the host machine it's running on.
You should be able to access the MySQL server using the host machine IP address.
Finally solved this one ... I had to remove all special characters from the password strings.
I tired adding single and double quotes are around the strings to see if that would allow the special characters, but that still failed. Passwords needed to be alphanumeric.
Have you tried the solution provided here? Basically, if you had a container running previously, you have to explicitly purge it since docker keeps track of root passwds (via volumes) from previously instantiated containers. This happens with a lot of tools, it's not unique to MySQL containers. A simple docker-compose rm -v should suffice, afterwards bring up your container. This basically deletes the old volume from the disk, removing all data from previous container instantiation.
You can also call a docker ps, find your container and execute docker rm -v <CONTAINER_NAME>. Bring up your container afterwards.

Runing multiple mysql containers with different port using host network

I have a Mysql server running on the host using default port 3306. I Want to run a MySQL docker container using network host but with a different port.
My configuration is defined in a docker-compose file. After building the image and tried running the container, it starts and shutdown with port conflict notice.
Is there a way to dynamically change the container port before starting up? I don't want to use the network bridge.
If using host networking is a hard requirement, then nothing in Docker space will be able to control or limit what ports the service does or doesn't use. You need to change a service-specific configuration file, environment variable, or command-line argument to make it listen somewhere else.
Particularly for servers that listen on a single TCP port (like most database and HTTP-based servers) the default Docker bridge/NAT setup should work fine; alternate setups like host networking and macvlan are unnecessary. If you're willing to use the standard setup, this is trivial:
version: '3'
services:
mysql:
image: mysql
ports: ['9999:3306'] # listen on host port 9999 instead
docker run --name 'dockername' -e MYSQL_ROOT_PASSWORD='password' -p 1000:3306 -d mysql
docker exec -it 'dockername' mysql -uroot -p
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'password'
flush privileges;
here 1000 is the port at which you want to run your mysql docker container.

Docker nodejs with mysql communication

I'm using 2 docker images one with my nodeJS backend server the other with my MySQL database. On the docker-compose file I'm defining the passwords, ports and hostnames correctly:
sql:
image: mysql:5.7.22
hostname: sql
ports:
- 3306:3306
secrets:
- db_root_pass
- db_user_pass
environment:
MYSQL_USER: user
MYSQL_PASSWORD_FILE: /run/secrets/db_user_pass
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_pass
provider:
image: monokilho/app:dev
hostname: provider
ports:
- 3000:3001
- 9221:9229
secrets:
- db_user_pass
command: node --inspect=0.0.0.0:9229 appModule.js
And on my DB_config.js file for NodeJS I have the connection setup like so:
db_config.host = 'sql';
db_config.port = '3306';
db_config.user = 'user';
db_config.password = fs.readFileSync('/run/secrets/db_user_pass', 'utf8');
db_config.database = 'app';
db_config.multipleStatements = true;
Problem is that although, using this exact configurations, docker connects Node to MySQL just fine on my local windows machine, when I upload the images to my remote linux server I continue to get:
Access denied for user 'user'#'8b2e56e566b2.network_default'
I've already remade the secrets, tried manually adding the passwords to the config on NodeJS and nothing... what makes it even weirder is that if I go on the MySQL container to connect directly or if I make another MySQL container and remotely connect it works, so I know the password input on MySQL config is correct and it is accepting remote connections.
Any suggestion what might be the difference between windows and linux for this behavior to happen? Thanks in advance.
PS: If needed windows is windows 10 and linux distro is ububtu 16.04.
EDIT: The access denied error appears on the mysql logs so the nodejs docker can reach the mysql docker and the network should be fine.
Apparently the mysql config was ignoring a sneaky \n on the password file allowing it to work normally with a command line connection, while on the nodejs it was bugging the connection.