Unable to connect Mysql 8 running on docker with mysql workbench - mysql

I have a docker setup that is running Mysql 8.
I am able to access Mysql inside docker container, however, I am unable to connect using Mysql workbench. I have another mysql container but this one is Mysql version 5.7 and I have no issues connecting with that one.
I have tried to allow root user full host access with % in the mysql.user
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
I have tried to connect to container using hostnames: localhost, 0.0.0.0, 127.0.0.1
and port 3306 but no luck
I also created a separate user and gave full privilege and still no luck
Below is my Docker compose file
version: '3.5'
services:
database:
build:
context: ./images/mysql
restart: always
command: ['--default-authentication-plugin=mysql_native_password']
environment:
- MYSQL_DATABASE=inim
- MYSQL_USER=inimuser
- MYSQL_PASSWORD=inimpass
- MYSQL_ROOT_PASSWORD=docker
ports:
- "3306:3306"
volumes:
- ./database/data:/var/lib
volumes:
my-datavolume:
my Dockerfile:
FROM mysql:8.0.20
CMD ["mysqld"]
EXPOSE 3306
Below is my Docker PS
b8832d9711b4 docker_inim_db_database "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp docker_inim_db_database_1
I have a similar setup with Mysql 5.7 and it connects fine. Not sure what I am doing wrong here.

You can debug your process by going to the container.
Please run:
$ docker exec docker_inim_db_database_1 /bin/bash
Now you should be in bash of the container.
Then login to mysql bash using:
$ mysql -uroot -p
and type your password or just press Enter if none.
There's two steps in that process:
a) Grant privileges. As root user execute with this substituting 'password' with your current root password :
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password';
b) bind to all addresses:
The easiest way is to comment out the line in your my.cnf file:
#bind-address = 127.0.0.1
and restart mysql
service mysql restart
By default it binds only to localhost, but if you comment the line it binds to all interfaces it finds. Commenting out the line is equivalent to bind-address=*.
To check where mysql service has binded execute as root:
netstat -tupan | grep mysql
Update For Ubuntu 16:
Config file is (now)
/etc/mysql/mysql.conf.d/mysqld.cnf
Most of steps above are from answer: https://stackoverflow.com/a/11225588/659077

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

Unable to connect to MySQL database from vps using localhost

I have two environments: my local machine Mac and linux on VPS. The problem is that I can not connect using localhost to my db from VPS.
On my local machine everything works fine.
Local:
mysql -u root mydb -h 127.0.0.1 --password=password --port=2345 //OK
mysql -u root mydb -h 0.0.0.0 --password=password --port=2345 //OK
mysql -u root mydb -h localhost --password=password --port=2345 //OK
Even using public ip of VPS I'm able to connect to remote db from local machine.
VPS:
mysql -u root mydb -h 127.0.0.1 --password=password --port=2345 //OK
mysql -u root mydb -h 0.0.0.0 --password=password --port=2345 //OK
mysql -u root mydb -h localhost --password=password --port=2345 //KO <-
Error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I used the following docker file to create my image.
DockerFile:
FROM mysql:8.0
COPY *.sql /docker-entrypoint-initdb.d/
docker-compose.yaml:
version: '3.7'
services:
ls:
image: ***/db-container:v6 # <-my image
container_name: db-container
restart: always
environment:
MYSQL_DATABASE: 'mydb'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '2345:3306'
expose:
# Opens port 3306 on the container
- '3306'
volumes:
- db-container:/var/lib/mysql
volumes:
db-container:
Query inside container:
mysql> SELECT host, user FROM mysql.user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| % | user |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
mysql> SELECT * FROM performance_schema.host_cache;
Empty set (0.02 sec)
"localhost" says to use a socket. But VPS probably allows only TCP/IP connections, not "socket".
I guess on local machine, you are not using docker. However, on vps, you are.
On you local, mysql is installed on the OS itself, hence, it has exposed its socket file at /var/run/mysqld/mysqld.sock. However, on VPS, the socket is inside docker.
The highlight here is the difference between localhost and 127.0.0.1 in reference to mysql. On unix based systems(including mac), localhost connects via socket, while 127.0.0.1 connects via TCP/IP.
Try adding following under docker-compose volumes section. See if it works then.
volumes:
- /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
There are many different root causes for a connection denied.
Since it is possible to execute queries inside the container, the best course of action is to inspect the performance_schema.host_cache table:
SELECT * FROM performance_schema.host_cache;
It should point more precisely at the problem.
Doc:
https://dev.mysql.com/doc/mysql-perfschema-excerpt/8.0/en/host-cache-table.html

ER_HOST_NOT_PRIVILEGED - docker container fails to connect to mariadb

I'm trying to get a docker container to work with mariadb and node.js images. The container will use an existing database in /home/mysql. However, when the container is launched, I'm getting this "failed to connect" error in node.js:
Error: ER_HOST_NOT_PRIVILEGED:
Host '172.18.0.5' is not allowed to connect to this MariaDB server
Here's my docker-compose.yml:
version: '3'
services:
mariadb:
image: mariadb
restart: always
volumes:
- /home/mysql:/var/lib/mysql
user: "mysql"
ports:
- "3306:3306"
watch:
build: .
restart: always
links:
- mariadb:mysql
environment:
- DOCKER_IP=172.18.0.2
depends_on: ['mariadb']
ports:
- "3000:3000"
After reading this thread, I found that mysql is actually running, but it fails to let other services connect:
These are some of the steps I have checked. As you can see, I can log in to the mysql instance:
$ docker exec -it 552aae9ea09c bash
mysql#552aae9ea09c:/$ mysql -u root -p
Enter password: *******
MariaDB [(none)]> SELECT host, user FROM mysql.user;
+-------------+------------------+
| host | user |
+-------------+------------------+
| 127.0.0.1 | root |
| ::1 | root |
| someusername| |
| someusername| root |
| localhost | |
| localhost | dbusername |
| localhost | databasename |
| localhost | root |
+-------------+------------------+
8 rows in set (0.00 sec)
mysql#552aae9ea09c:/$ mysqld --verbose --help | grep bind-address
2017-11-13 17:35:40 139825857279872 [Note] Plugin 'FEEDBACK' is disabled.
--bind-address=name IP address to bind to.
bind-address (No default value)
One thing to note is that even though I've explicitly set the user to mysql in the yml file, these three files in /home/mysql: ib_logfile0,ib_logfile1, ib_buffer_pool are still under the group of systemd-journal-remote, which I suspect has something to do with the connection failure.(reference)
The error you are receiving is caused by the fact that MariaDB thinks you are not authorized to connect to the server. This means that you haven't created a database user for the Node.js app or the grants for that user are incorrect.
A fool-proof way to solve this is to create a separate user for the Node.js application. You can automate this by writing the following SQL into a file and mounting the volume into /docker-entrypoint-initdb.d.
CREATE USER 'my-app-user'#'%' IDENTIFIED BY 'my-app-password';
GRANT ALL ON *.* TO 'my-app-user'#'%';
Change the username and password accordingly and reduce the given privileges from the ALL privilege. You can also change the wildcard hostname % to a specific IP address or hostname.
Simply run this sql query:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password'
(assuming that you are connecting as the root user)

Can't connect to mysql container from localhost

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.

connecting to a docker-compose mysql container denies access but docker running same image does not

I am having some issues connecting to the mysql docker container that I have launched with docker-compose. This is a long post (sorry!).
Here is my docker-compose.yml file:
db:
image: mysql:5.7
ports:
- "3306:3306" # I have tried both ports and expose "3306". Still doesn't work
environment:
- MYSQL_ROOT_PASSWORD="secret"
- MYSQL_USER="django"
- MYSQL_PASSWORD="secret"
- MYSQL_DATABASE="myAppDB"
Then:
$> docker-compose build
db uses an image, skipping #expected!
$> docker-compose up
<<LOTS OF OUTPUT>>
OK, so now I have an up and running docker container runner mysql:5.7. Great! Or is it?
When testing in my django app, I get Operational errors saying that the user isn't allowed to connect the database. Ok, so maybe it's my django then?
$> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7216f99ca0f mysql:5.7 "docker-entrypoint.sh" 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp sharpfin_db_1
$> docker-machine ip dev
192.168.99.100
$> mysql -h 192.168.99.100 -P 3306 -u django -p
Enter password:
ERROR 1045 (28000): Access denied for user 'django'#'192.168.99.1' (using password: YES)
ok so maybe It's something to do with connecting to the docker-compose container?
What if I try connecting from inside the docker container?
$> docker exec -it c7216f99ca0f /bin/bash
root#c7216f99ca0f:/#
root#c7216f99ca0f:/# mysql -u django -p
Enter password:
ERROR 1045 (28000): Access denied for user 'django'#'localhost' (using password: YES)
ok, so docker mysql won't let me connect, don't know why. Let's see what happens when I try do this without docker-compose:
$> docker run --name run-mysql -e MYSQL_ROOT_PASSWORD="secret" -e MYSQL_USER="django" -e MYSQL_PASSWORD="secret" -e MYSQL_DATABASE="myAppDB" -p "3306:3306" mysql:5.7
<<LOTS OF OUTPUT SAME AS BEFORE>>
Ok, so now we have a container running the same image as before with the same settings. (I think this assertion is probably not true - docker-compose is doing something different to docker run).
$> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
73071b929e82 mysql:5.7 "docker-entrypoint.sh" 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp run-mysql
There's my container (called run-mysql). Let's connect!
$> mysql -h 192.168.99.100 -P 3306 -u django -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.12 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| myAppDB |
+--------------------+
2 rows in set (0.01 sec)
mysql>
Alright. Can log in. That's weird... What about from inside the container?
$> docker exec -it 73071b929e82 /bin/bash
root#73071b929e82:/# mysql -u django -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.12 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| myAppDB |
+--------------------+
2 rows in set (0.00 sec)
mysql>
Ok, I can log in from outside and inside the container when I launch with docker run, but not with docker-compose. What's going on? There must be something either docker-compose is doing behind the scenes that changes how the database is initialized.
All the above is the exact same if I try with the root user as well. So it's not a permissions issue with the django user.
Any ideas how to resolve this?
Environment variables in docker-compose.yml file should not have quotes when using array definition:
db:
image: mysql:5.7
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_USER=django
- MYSQL_PASSWORD=secret
- MYSQL_DATABASE=myAppDB
If you use them in your docker-compose.yml file:
db:
image: mysql:5.7
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD="secret"
- MYSQL_USER="django"
- MYSQL_PASSWORD="secret"
- MYSQL_DATABASE="myAppDB"
and run:
$ docker-compose up -d
and enter running container:
$ docker-compose exec db /bin/bash
you will see the output:
root#979813643b0c:/# echo $MYSQL_ROOT_PASSWORD
"secret"
I had a similar issue, and this helped me:
https://github.com/docker-library/mysql/issues/51#issuecomment-76989402
Have you changed the passwords since you first tried running the containers? docker-compose does extra work to preserve volumes between runs (thus preserving the database); you may want to try docker-compose rm -v to delete everything and try starting it up again.
I am using the official mysql image with docker-compose and not having a problem. The only difference in my compose file is that I am using a dictionary instead of an array:
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_USER: django
MYSQL_PASSWORD: secret
MYSQL_DATABASE: myAppDB
I have noticed that the compose file documentation is still stuck in V1 in some places, so you could try this, if you're using V2. Otherwise, for debugging you can use docker-compose exec to interact with the container created by compose directly.
docker-compose exec db /bin/bash will get you a shell on the container that is giving you trouble and you can check things like SHOW GRANTS FOR django#'%' or whether the ports are being forwarded correctly. I hope this helps.
I had the same error "Access denied for user 'admin'#'172.20.0.1' (using password: YES)". And for the long time could not figure out how to resolve it. In my situation docker-compose takes configuration from .env file.
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USERNAME}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
Finally I have found issue! The problem was in double quots in parameters.
DB_PASSWORD="dbpassword"
doesn't work
DB_PASSWORD=dbpassword
work
I have same issue with mysql 5.7 in docker-compose:
image: mysql/mysql-server:5.7
dns: 8.8.8.8
volumes:
- mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: mydb
MYSQL_USER: user
MYSQL_PASSWORD: 123456
ports:
- 3306:3306
From inside the container, only using localhost worked (and not 127.0.0.1):
mysql -h localhost -u root -p
To change it to allow connections from everywhere, once you are inside the mysql application, do:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
EXIT;
It looks like your problem is solved. I thought I'd discuss my problems similar to this.
I am running tomcat (web) and mysql (db) using docker-compose on a Synology NAS (DSM 6.0.2). It worked fine on an Ubuntu box I have but not on the NAS.
The problem was the firewall on the NAS - I had modified my firewall rules to allow certain ports open but then DENY ALL at end. When I added :3306 to the allowed ports it worked!
This is not a good solution and I don't know why DSM would require this since the docker-compose is running on a BRIDGE network. I've put in a support ticket about this.
This answer may help others with this blocked container issue.
In my case I had mariadb installed and was trying to start a mysql container. Somehow starting the container didn't fail and running docker ps showed the container as listening on 3306, but in reality, the mariad mysqld was running and I was getting access denied. to that db rather than the one in the container. I'm using a Mac and was able to stop mariadb with: launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
editd docker-compose.yaml is not work when you already start that container,
so I support this way:
1: chack you db containers that already exist
docker ps
CONTAINER ID NAMES PORTS STATUS
1cbfe602466a mysql5.7 Exited (0) About an hour ago
2: if the container has been stared,then stop it and rm
docker stop 1cbfe602466a //(mysql5.7)
docker rm 1cbfe602466a
3: now you can Restart mysql container,
docker-compose.yaml //(Pay attention to formatting your code)
mysql5.7:
container_name: mysql5.7
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- 3306:3306
4: run commend:
docker-compose up -d mysql5.7
5: you are success! you can check it into container .
docker exec -it mysql5.7 /bin/bash
root#1719480b6716:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>