Closed sougiovn closed 6 years ago
Same here. The problem suddenly appears today on a new container 5.7.17. all previous containers with the same image are fine (sounds impossible, I know).
Still investigating how it changed from working to not working from one day to the next.
I resolved removing the volumes
configuration.
Can you check the user,host columns in your mysql.user table to make sure the user isn't limited in some way. And also run from the container (or with exec) mysqld --verbose --help | grep bind-address To check that the server will allow external connections
Thanks for the tipp @ltangvald the user was completely missing and so was the database. The problem was in my docker-compose calls that launched two containers simultanously which shared a data volume.
Hey @ltangvald, i've the same problem but dont really understand how to do this can you help me and telling me where i find my mysql stuff?
@s1dekick223
In MySQL, each database user is defined with IP address in it, so you can have for example a root user allowed to connect from localhost (127.0.0.1) but not from other IP addresses. With a container, you never access to the database from 127.0.0.1, it could explain the problem.
To check it, you can do the following:
docker exec -it your_container_name_or_id bash
mysql -u your_user -p
It will ask you your password, you have to write it and press enter.
SELECT host, user FROM mysql.user;
It will display a table, for example like this:
+------------+------------------+
| host | user |
+------------+------------------+
| % | root |
| 127.0.0.1 | root |
| ::1 | root |
| localhost | mysql.sys |
| localhost | root |
| localhost | sonar |
+------------+------------------+
It has to contain a line with your database user and '%' to works (% means "every IP addresses are allowed"). Example:
+------------+------------------+
| host | user |
+------------+------------------+
| % | root |
+------------+------------------+
My root user can connect itself from any IP addresses.
After that, like @ltangvald said, it could be a problem of allowing external connections to the container.
To check it, you can do the following:
docker exec -it your_container_name_or_id bash
mysqld --verbose --help | grep bind-address
It will display address to bind to, for example like this:
--bind-address=name IP address to bind to.
bind-address 0.0.0.0
The bind address have to be 0.0.0.0
(which means "every IP addresses") to work.
Using docker-compose
, if you link a volume, the parameters
environment:
MYSQL_ROOT_PASSWORD: 'pass'
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'pass'
in your docker-compose.yml
will not be used, so default user will not be created: you have to create it manually or remove the volume declaration.
Using
docker-compose
, if you link a volume, the parametersenvironment: MYSQL_ROOT_PASSWORD: 'pass' MYSQL_DATABASE: 'db' MYSQL_USER: 'user' MYSQL_PASSWORD: 'pass'
in your
docker-compose.yml
will not be used, so default user will not be created: you have to create it manually or remove the volume declaration.
This is not quite accurate. They will be used if the database files do not exist in the folder. If it is a folder from a previous run of mysql, either from another container or an install from the host machine (ensure neither is running), then the initialization step will be skipped and these environment variables have no effect.
If there is no important data in the files, you can always docker-compose rm -fv; sudo rm -rf ~/dev/dbs-data/mysql/*
and then docker-compose up -d
.
I have today this problem, but it was my mistake.
I copying docker-compose.yml and related volumes directories (where logs and database settings stored) from one old project to another new project. And run docker-compose up -d
So i have, in result, mysql's service image builted from cache, with database settings from original project. No environments were applied, @yosifkit make exact definition of situation.
Simply delete from volume's directory all old mysql data, rebuild image - and all works fine.
please @lucile-sticky my bind-address is '*' how do i edit it to '0.0.0.0'??? Thanks
@LordRahl90 to change the bind-address option, you have to:
[mysqld]
, add bind-address=0.0.0.0
Example:
In my case, I create my own mysqld.cnf
file:
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
# Where the database files are stored inside the container
datadir = /var/lib/mysql
# My application special configuration
max_allowed_packet = 32M
sql-mode = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'
# Accept connections from any IP address
bind-address = 0.0.0.0
Then in my Dockerfile, I tell to Docker to copy it inside the container:
FROM mysql:5.6
MAINTAINER lucile-sticky "aaa@bbb.ccc"
# Setup the custom configuration
ADD mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
So when MySQL starts, it uses my special configuration instead of the default one.
@lucile-sticky I'm having the same issue as which @LordRahl90 has, but I want to ask, why mysql does not set bind-address to 0.0.0.0 when it start? Do I have to set it manually?
@littlee I don't know what is the default value of bind-address
for MySQL, that's why I specify it, to be sure to have the wanted one.
For me, I hit the same issue once I switched the volume
to a path local on my computer, then went through a docker-compose down
and docker-compose up -d
cycle. After clearing out the files in that directory, the next docker-compose up -d
created things fresh, and it worked fine.
I didn't have this problem when using a data volume (volumes: ['db_data:/var/lib/mysql']
).
You can also copy the my.cnf file
docker cp mysql_mysql_1:/etc/my.cnf conf/
And then include a volume mapping exclusively to your file.
volumes:
- './conf/my.cnf:/etc/my.cnf'
So you can add bind-address=0.0.0.0 to your my.cnf file.
Anyway, I've done this, but still cannot connect from outside.
I fixed executing this command on docker container:
# mysql_secure_installation
@Danisan me too. Did you find the solution?
I think I know the issue. When using docker-compose, a new network is created with a different subnet, leading to a new gateway and ip address. By default, mysql will use 172.17.0.1 as a gateway for communication - which is normally the value on the 'bridge' network (you can view this using docker inspect bridge
and docker inspect <your instance name or ID>
). To view available networks, you can use docker network ls
.
A new root host needs to be specified to allow specified IPs to connect if you're going to use docker-compose the way you have it, but you will also need to know what your new subnet will be. In docker-compose version 3, you lose the ability to specify your own gateway for a new network - I'm guessing the gateway will always be x.x.x.1. However, you do have the ability to specify the subnet for the new network (this video does a good enough job to helping you understand subnetting assuming you already know a bit of binary math).
Documentation doesn't seem to be updated or at least I was unable to use "bridge", so what I did was create a new network.
[edit] After reading the error, it seems like you can only re-use user defined networks.
Here's my docker-compose.yml file
version: '3'
services:
mysql:
image: mysql/mysql-server:5.7
env_file: #Use environment variables from file.
- mysql.env
volumes:
- ./data:/var/lib/mysql
ports:
- 3306:3306
environment:
## Use % to allow remote access to all addresses.
## Generally not recommended by some people to allow for root.
#- MYSQL_ROOT_HOST=%
# This allows ip ranges from 192.168.0.49 to 192.168.0.54 to connect to root
- MYSQL_ROOT_HOST=192.168.0.48/255.255.255.248
networks:
network:
ipv4_address: 172.20.0.2
networks:
network: #will be prefixed with 'parent_dir_name_' so for me, it will be 'mysql_network'
ipam:
config:
- subnet: 172.20.0.0/16
Currently looking into other ways and learning more about networking and docker.
Using the compose file above, to connect to the database, you'll need to use the IP of the machine that the container is being hosted on, rather than the IP of the container; so localhost won't work unless a user with host 172.20.0.1 is created - and localhost won't work on a different machine because 127.0.0.1 is loopback. For example, if the machine that's running the mysql container is using 192.168.1.25 on my network, that will be the IP entered to access my database.
@lucile-sticky I am having this in my mysql.user table. | host | user | +-----------+---------------+ | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+---------------+ and I am getting the same error ""Host '172.22.0.1' is not allowed to connect to this mysql server". Suggest some way to resolve the issue.
Hi @meenakshik-optimus,
Create new user with host '%' rather than localhost. Below is works for me. It may useful to you. mysql> CREATE USER 'usernameall'@'%' IDENTIFIED BY 'ThePassword'; mysql> grant all on *.* to 'usernameall'@'%';
I am experiencing the same issue with 5.7.19
Did a quick test: I ran 2 containers; the first with 5.6.34 and the second with 5.7.19 & listed the users on the mysql.user table on both of them (jfr: ran the first, stopped it and then ran the second). While on 5.6.34 there is an entry for 'root' on '%', it is not the case with 5.6.34. It looks like the "culprit" might be either mysql 5.7 (as a new default for user root maybe?) or the bootstraping process on the docker image mysql:5.7
5.6.34 mysql.user table:
> select Host, User from user \G
*************************** 1. row ***************************
Host: %
User: root
5.7.19 mysql.user table:
*************************** 1. row ***************************
Host: localhost
User: root
*************************** 2. row ***************************
Host: 127.0.0.1
User: root
Here is the docker-compose definition I used for the 5.6.34 service:
api-db:
image: mysql:5.6.34
volumes:
- ./devel-api-db:/var/lib/mysql:rw
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=app1
- MYSQL_USER=super-secret-pwd
- MYSQL_PASSWORD=devel
- MYSQL_ROOT_HOST=127.0.0.1
ports:
- "3307:3306"
Here is the docker-compose definition for the 5.7.19 service:
api-db:
image: mysql:5.7.19
volumes:
- ./devel-api-db:/var/lib/mysql:rw
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=app1
- MYSQL_USER=super-secret-pwd
- MYSQL_PASSWORD=devel
- MYSQL_ROOT_HOST=127.0.0.1
ports:
- "3307:3306"
@ Dmthakkar.. I want it all to be done using docker file, so that I just need to run an instance of the image and everything will be set. How can I achieve this using docker file? Any suggestion?
I ran into this symptom for a different reason. For posterity:
My project is set up to run containers with docker-compose
and our custom services have user: ${UID}
. At some point while debugging, I ran the db service (which just uses the mysql:5.7
image) as root -- without running export UID
in my shell beforehand. That caused its volume to have the wrong permissions. So the fix for me was to remove all volumes (docker volume rm $(docker volume ls -q)
) and then either (a) run my default project scripts, which take care of publishing UID
, or (b) export UID
in my shell and then run one-offs like docker-compose run db
or docker-compose run db-migration
.
My `docker-compose.yaml' contained:
services:
db:
image: mysql:5.7
env_file:
- db/vars.env
volumes:
- db:/var/lib/mysql
db-migration:
depends_on:
- db
user: ${UID}
build:
...
In my case, bind-address
was *
instead of 0.0.0.0
, and that was fine; and I didn't see any difference between mysql:5.7
(which uses 5.7.20
currently) and the previous 5.7.19
. And none of the issues seemed to be with images/containers, just with the volumes' permissions; I can deterministically recreate the issue by changing whether I export UID
and deleting/recreating volumes, without changing images. The volume permission issue led to db setup failing (so the root user wasn't configured correctly even though mysqld
started).
I think this is probably as solved as it's going to be (and is turning into a dumping ground for other folks' connection issues). I'm going to close since there's nothing to be changed in the image (as far as I can tell -- would love to see a PR if that's not correct :heart:).
In the future, these sorts of questions/requests would be more appropriately posted to the Docker Community Forums, the Docker Community Slack, or Stack Overflow. Thanks! :+1:
You need .env file
DATABASE_NAME=DBNAME DATABASE_HOST=127.0.0.1 DATABASE_PASSWORD=DBPASS DATABASE_USERNAME=ROOTNAME
docker run -p 3306:3306 --name mysql2 -e MYSQL_ROOT_PASSWORD=b3RmELKOvCUrAdxIg0GEmugc3SY -e MYSQL_ROOT_HOST=% -d mysql/mysql-server:latest
Hostname: 0.0.0.0
Port: 3306
Username: root
Password: b3RmELKOvCUrAdxIg0GEmugc3SY [mysql random secret]
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '0.0.0.0',
port : 3306,
user : 'root',
password : 'b3RmELKOvCUrAdxIg0GEmugc3SY',
database : 'mysql'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
Stupid issue for me ...it was not working because I've explicitly set MYSQL_RANDOM_ROOT_PASSWORD=no
Complely removing the key/value now works (it uses the MYSQL_ROOT_PASSWORD
value)
Any ideas why this would be happening?: I have an install script which installs both Laradock and my Larvel application. I use this install script successfully within a Virtualbox VM - Ubuntu 16.04 without any issues at all. Every thing comes up and MySQL migrates no problem. I then install the exact same Ubuntu 16.04 OS on my same machine. This time, not as a VM but as the native OS. I run the exact same script, under the same Ubuntu OS with no modifications at all and I get the dreaded MySQL: "SQLSTATE[HY000] [1130] Host '172.20.0.3' is not allowed to connect to this MySQL server" during migration. Why would 2 identical systems react this way? Everything is exactly the same and I have repeated this problem 3 times so far.
I've encountered this same problem as originally described (though with a Python web app): I've used mysql:5.7
before and, in a new project, I suddenly could not connect to my mysql container, managed with docker-compose.
TL;DR of how I solved it:
docker-compose down
on other projects that used the mysql:5.7
image.MYSQL_ROOT_PASSWORD
env var (I am not completely sure if this is relevant).docker-compose up --build --force-recreate -d
.Details:
I am debugging a problem with docker-compose in an existing project and decided to create a short self-contained example that will reliably reproduce the problem I am having. I started with the base framework files, copied the Dockerfile
and docker-compose.yml
and edited the project-specific values. My docker-compose.yml
looked like:
version: '2'
services:
db_test:
image: "mysql:5.7"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_DATABASE=app_test
ports:
- "3309:3306"
test:
build: .
volumes:
- .:/main
depends_on:
- db_test
In the course of debugging my original problem, I ended up adding the MYSQL_ROOT_PASSWORD
env var alongside those already present. That's when I started encountering the problem described in this issue.
Thanks to lucile's excellent rundown above, I determined that the container created does not have the entry (%, root)
in mysql.user
. Note that this entry is present in the container created for my other (original) project.
I ended up cleaning my test project and, by chance, fell into the sequence of steps as described above and solved my problem.
If I set a volume
arg, the container raise some errors like cannot connect to x.x.x.x
。
And I found that mysqld start faild. It's seems that mysqld can't create tmp files in /tmp dir.
I tried a lot of ways to solve this problem. Here is a solution for my problem:
# in container
chmod -R 777 /tmp
service mysql restart
mysqld started successfully, and everything goes well. I do not know why, but it just works. You can have a try.
i have some problems. My docker-compose.yml:
version: '2'
services:
nginx:
image: nginx:latest
ports:
and when i'm trying login adminer with a such parameters:
System MySQL Server mysql Username root Password root
i have this error :"Host '172.18.0.3' is not allowed to connect to this MySQL server" Can someone help me please?or any ideas about the problems?
@Montego rm the old containers from this build and try to run start it with
docker-compose up -d
@s1dekick223 sorry for maybe stupid question, but how can i rm the old containers from this build correctly?
@Montego There are two ways, you can use Kitematic for that or you're using the terminal.
For the terminal use
docker ps -a
to search for your container(s)
then use
docker rm *containerID*
to rm it
@s1dekick223 thanks a lot. But...if i use docker-compose down before - i haven't containers if docker-compose is running, i have 3 containers: dockermaster_nginx_1, dockermaster_php_1, dockermaster_mysql_1 and i can't to rm this, because "Error response from daemon: You cannot remove a running container 49fcc2eda685202c4deb0bf27829221ff61ee4a13753ff543a5410591c2f9f46. Stop the container before attempting removal or force remove"
@Montego go into the root where your docker-compose.yml is start terminal and use
docker-compose stop
to shut them down
@s1dekick223 montego@montego-Inspiron-5767:~/Work/Projects/docker-master$ docker-compose stop Stopping dockermaster_nginx_1 ... done Stopping dockermaster_php_1 ... done Stopping dockermaster_mysql_1 ... done montego@montego-Inspiron-5767:~/Work/Projects/docker-master$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7344beca70a6 nginx:latest "nginx -g 'daemon ..." 15 minutes ago Exited (0) 11 seconds ago dockermaster_nginx_1 eaa46ef7a7d8 dockermaster_php "docker-php-entryp..." 15 minutes ago Exited (0) 10 seconds ago dockermaster_php_1 83cdaaa4c195 mysql:latest "docker-entrypoint..." 15 minutes ago Exited (0) 7 seconds ago dockermaster_mysql_1 montego@montego-Inspiron-5767:~/Work/Projects/docker-master$ docker rm 7344beca70a6 7344beca70a6 montego@montego-Inspiron-5767:~/Work/Projects/docker-master$ docker rm eaa46ef7a7d8 eaa46ef7a7d8 montego@montego-Inspiron-5767:~/Work/Projects/docker-master$ docker rm 83cdaaa4c195 83cdaaa4c195 montego@montego-Inspiron-5767:~/Work/Projects/docker-master$ docker-compose up -d Creating dockermaster_mysql_1 Creating dockermaster_php_1 Creating dockermaster_nginx_1 `
unfortunately, i still have my error "Host '172.18.0.3' is not allowed to connect to this MySQL server " in adminer :(
@Montego how long do you let him build? the sql image is having a weird brake you've to wait about 1-3 minutes while he's building the sql container ... try to run it and get yourself a drink or something like this ^^
@s1dekick223 about 5-10 seconds :) I can't resolve this problem about 3 days and this is killing me slowly >_<
Again: What is the problem? Somehow all answers look weird to me. Something really bad must have been changed in the project?
I can no more start a mysql server and successfully connect a wordpress to it. It used to work some weeks ago, and it still works with old deployments, so it seems that initializing a new empty mysql from scratch is broken.
I'm, using docker swarm.
What I see:
This works no more:
version: '3.3'
services:
mysql:
image: mysql
volumes:
- type: bind
source: /srv/volumes/test/mysql
target: /var/lib/mysql
environment:
- 'MYSQL_DATABASE=wordpress'
- 'MYSQL_USER=wordpress'
- 'MYSQL_PASSWORD=iy1ieWaiphoofeixogai0quai0eevu2Vah9Aecei'
- 'MYSQL_ROOT_PASSWORD=theeNgiHohzeph4LooW5iepohXei6Lieje8zo1he'
command: --log-bin=/var/lib/mysql/mysql-bin.log --binlog-format=ROW --server-id=1
wordpress:
image: wordpress
ports:
- 8080:80
volumes:
- type: bind
source: /srv/volumes/test/wordpress
target: /var/www/html
- type: bind
source: /srv/volumes/config/volumes/uploads.ini
target: /usr/local/etc/php/conf.d/uploads.ini
environment:
- 'WORDPRESS_DB_HOST=mysql'
- 'WORDPRESS_DB_NAME=wordpress'
- 'WORDPRESS_DB_USER=wordpress'
- 'WORDPRESS_DB_PASSWORD=iy1ieWaiphoofeixogai0quai0eevu2Vah9Aecei'
But with the following changes in the environment, it works:
version: '3.3'
services:
mysql:
image: mysql
volumes:
- type: bind
source: /srv/volumes/test/mysql
target: /var/lib/mysql
environment:
#- 'MYSQL_DATABASE=wordpress'
#- 'MYSQL_USER=wordpress'
#- 'MYSQL_PASSWORD=iy1ieWaiphoofeixogai0quai0eevu2Vah9Aecei'
- 'MYSQL_ROOT_PASSWORD=theeNgiHohzeph4LooW5iepohXei6Lieje8zo1he'
- 'MYSQL_ROOT_HOST=%'
command: --log-bin=/var/lib/mysql/mysql-bin.log --binlog-format=ROW --server-id=1
wordpress:
image: wordpress
ports:
- 8080:80
volumes:
- type: bind
source: /srv/volumes/test/wordpress
target: /var/www/html
- type: bind
source: /srv/volumes/config/volumes/uploads.ini
target: /usr/local/etc/php/conf.d/uploads.ini
environment:
#- 'WORDPRESS_DB_HOST=mysql'
#- 'WORDPRESS_DB_NAME=wordpress'
#- 'WORDPRESS_DB_USER=wordpress'
#- 'WORDPRESS_DB_PASSWORD=iy1ieWaiphoofeixogai0quai0eevu2Vah9Aecei'
- 'WORDPRESS_DB_PASSWORD=theeNgiHohzeph4LooW5iepohXei6Lieje8zo1he'
Why?
@Montego Do you have an already initializated database in ./mysql
? The environment variables are only use on first initialization. See also https://github.com/docker-library/postgres/issues/203#issuecomment-255200501.
@mwaeckerlin, probably related to the move from 5.7 to 8: https://github.com/docker-library/mysql/issues/409
adding
environment:
- MYSQL_ROOT_HOST=%
fix my problem
@tarikhagustia @yosifkit , I'd expect that configuration options remain the same even if the MySQL version changes, unless there is no very good reason for changing them.
Especially initializing a user and database, which seems not to work anymore, is a very useful and needed feature!
If required, MYSQL_ROOT_HOST=%
should be the default if MYSQL_ROOT_HOST
is not explicitely set.
@mwaeckerlin, '%' is the default if nothing is supplied:
And the non-root user is always %
:
As I mentioned above, your issue is probably #405 (and related issue #419). You need to either use mysql:5.7
as your image, or add --default-authentication-plugin=mysql_native_password
to your command. (I don't know why commenting out the non-root user makes your setup work, but if you can create a minimal reproducible case you could file a new issue)
@yosifkit , this is the minimum reproducible case that I posted in May: https://github.com/docker-library/mysql/issues/275#issuecomment-387735349
Not a docker-compose solution, just a console only:
docker run
--rm
--name mysql
-p 6603:3306
--env="MYSQL_ROOT_HOST=%"
--env="MYSQL_USER=username"
--env="MYSQL_PASSWORD=password"
--env="MYSQL_GENERAL_LOG=1"
--env="MYSQL_ROOT_PASSWORD=password"
--mount type=bind,src=/srv/databases/mysql57.docked,dst=/var/lib/mysql
--mount type=bind,src=/srv/databases/mysql-5.7.cnf,dst=/etc/my.cnf
mysql/mysql-server:5.7
So, we can use:
mysql --host=0.0.0.0 --port=6603 --user=username --password=password --prompt "MySQL 5.7 > "
or
mysql --host=0.0.0.0 --port=6603 --user=root --password=password --prompt "MySQL 5.7 > "
If it works - remove --rm
key and add -d
detach option.
I switched both tests to mysql:5.7
, since 8.0 is now latest
and fails in both tests as I linked above. I assume that you just haven't pulled latest
recently, otherwise you would get similar errors. I also just used a relative directory for the mysql mount so it works for anyone to test.
I am unable to reproduce the failures you are seeing; both tests work fine. Do you have an already initialized mysql data directory? The environment variables are only used if the data directory is empty: see also https://github.com/docker-library/postgres/issues/203#issuecomment-255200501.
Hi, I am getting this error with the mysql:5.7 image, but three weeks ago this was not happening.
This comment was helpful.
I had this:
volumes:
- ./data/db:/var/lib/mysql
Using this fixed it:
volumes:
- ./db:/var/lib/mysql
Similar problem here, mysql:8 up and running with docker-compose. I've created user administrator@% to create databases and another users. I'm logged in as administrator in adminer (same project as mysql), then i run jenkins, and it runs PHP script that create new database and user and grant user privileges to database. After that when i refresh adminer, im logged out and can't login again - i got message:
SQLSTATE[HY000] [1045] Access denied for user 'administrator'@'mysql_adminer_1.mysqlGlobal' (using password: YES)
with CLI i am still able to connect and login, with the same credentials...
I'm trying to connect to the MySQL by the DBeaver db manager.
But I get this error:
java.sql.SQLException: null, message from server: "Host '172.18.0.1' is not allowed to connect to this MySQL server"
I using docker-compose, here is my docker-compose.yml:
The problem is that, in my Mac it works, but in my Linux I get the error above.
Should I do some other configuration for my linux?