first, your docker run must map the mysql port to an host port:
docker run -p host:container
(for instance: docker run -d -p 3306:3306 tutum/mysql)
second, if you are using docker in a VM (docker-machine, with boot2docker), you need to use the ip of docker-machine ip <VMname>, with the host mapped port.
# Enable remote access (default is localhost only, we change this
# otherwise our database would not be reachable from outside the container)
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
/usr/sbin/mysqld &
sleep 5
echo "Creating user"
echo "CREATE USER '$user' IDENTIFIED BY '$password'" | mysql --default-character-set=utf8
echo "REVOKE ALL PRIVILEGES ON *.* FROM '$user'@'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
echo "GRANT SELECT ON *.* TO '$user'@'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
echo "finished"
By default after deployment MySQL has following connection restrictions:
mysql> select host, user from mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | healthchecker |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
4 rows in set (0.00 sec)
Apparently, for the security purposes you will not be able to connect to it outside of the docker image.
If you need to change that to allow root to connect from any host (say, for development purposes), do:
Start your mysql image with all port mappings required:
docker run -p 3306:3306 --name=mysql57 -d mysql/mysql-server:5.7
or, if the complete port mapping is required:
docker run -p 3306:3306 -p 33060:33060 --name=mysql57 -d mysql/mysql-server:5.7
If this is the fresh installation - grab the default password:
docker logs mysql57 2>&1 | grep GENERATED
Connect using mysql client directly to the mysqld in docker:
docker exec -it mysql57 mysql -uroot -p
If this is the fresh installation you will be asked to change the password using ALTER USER command. Do it.
Run SQL:
update mysql.user set host = '%' where user='root';
Quit the mysql client.
Restart the container:
docker restart mysql57
Now you will be able to connect from MySQL Workbench to
I was trying to connect from Mysql Workbench but it wasn't allowing me. Turned out, I forgot to mention the port. Here is the complete command to run and then connect from workbench:
I found a video that showed another way to get this to work. You can specify the IP address when passing in the port number. That is, something like -p 127.0.0.1:3307:3306 instead of just -p 3307:3306 I've never seen that before
https://www.youtube.com/watch?v=20om-9Gwuc0#t=7m
Example start command
docker run -d -e MYSQL_ROOT_PASSWORD=test --name mysql8 -p 127.0.0.1:3307:3306 mysql:8
Then I was able to use MYSQL Workbench to connect to 127.0.0.1 at port 3307.