7๐
I had to grant the root user at the Django container permissions to access the DB:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.17.0.3' IDENTIFIED BY 'password' WITH GRANT OPTION;
SET PASSWORD FOR root@'172.17.0.3' = PASSWORD('root');
FLUSH PRIVILEGES;
Where 172.17.0.3
is the IP of the container with the app. MYSQL_ROOT_HOST
is not needed.
5๐
docker run --rm -d -p 9999:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST='%' mysql/mysql-server:5.7
The important part is MYSQL_ROOT_HOST='%'
- Custom label on ModelChoiceField form field
- Raising ValidationError from django model's save method?
- 400 Bad Request While Using `django.test.client`
- How to add namespace url to a django-rest-framework router viewset
- What cheat sheets exist for Django?
1๐
This behaviour is fully documented here: (https://github.com/mysql/mysql-docker#mysql_root_host) and works as expected.
Iโll quote it:
By default, MySQL creates the โrootโ@โlocalhostโ command. 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.
You can see actual .sh
script which set allowed host from this env variable: https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/docker-entrypoint.sh#L63-L68
- 400 Bad Request While Using `django.test.client`
- Transparent PNGs don't retain transparency after being transformed (Django + PIL)
- How to install cffi package on AWS Beanstalk
1๐
As @valignatev suggested, using this env variable solves an issue.
I did create container this way:
docker run --rm -dit -e MYSQL_ROOT_PASSWORD=pass -e MYSQL_ROOT_HOST=172.17.0.1 --name mysql_container -p 3306:3306 mysql-mysql-server
172.17.0.1 is my default docker gateway
Once container gets started I can connect from my host like this:
mysql -h localhost -P 3306 --protocol=tcp -u root -p
1๐
Not sure if this will be helpful but I had a similar issue and got it to work by adding MYSQL_ROOT_HOST=%
This is what my docker-compose looks like for the db:
mysql:
platform: linux/x86_64
image: mysql
environment:
- MYSQL_ROOT_HOST=%
- MYSQL_ROOT_PASSWORD=foobar123
- MYSQL_USER=test_user
- MYSQL_PASSWORD=foobar123
volumes:
- ./init:/docker-entrypoint-initdb.d
ports:
- 3306:3306
container_name: "mysql"
Source documentation: https://dev.mysql.com/doc/refman/8.0/en/docker-mysql-more-topics.html#docker_var_mysql-root-host
- Receiving 'invalid form: crispy' error when trying to use crispy forms filter on a form in Django, but only in one django app and not the other?
- AWS Elastic Beanstalk: WSGI path incorrect?
- When are threaded frameworks better than event-driven frameworks? (i.e., when is rails better than node.js?)