无法远程连接到dockerized mysql数据库

在我的AWS ec2服务器上,我安装了docker 1.9.1 。 在一个基于ubuntu:trusty官方docker镜像的图片test_image ,我试图设置LEMP(Linux,Nginx,MySQL,PHP)架构。

以下是我用来启动我的容器的docker命令:

 docker run --name test_1 -d -p 80:80 -p 3306:3306 test_image /bin/sh -c "while true; do echo daemonized docker container; sleep 5000; done" 

我将端口803306暴露给主机的networking接口,并且还允许AWS的安全组允许到这些端口的入站连接。 安全组中的连接types是: MYSQL/Aurora ,协议是: TCP (我知道它不是很安全,它只能用于初始实现,生产设置不同)

我遵循这个DigitalOcean教程: https ://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04

安装Nginx并启动后,我可以在浏览器中通过ec2的pubic ip进行testing,例如http://xxx.xxx.xxx.xxx显示默认的nginx欢迎页面。

在安装MySQL时,我在docker容器中遵循以下命令:

 apt-get install mysql-server mysql_install_db /etc/init.d/mysql start mysql_secure_installation 

我已经给我的root用户提供了一个密码,在mysql_secure_installation期间,我已经允许远程访问root用户。

从容器内的mysql -u root -p命令将我连接到mysql数据库,但不是从容器外部。

另外从我的本地机器:我试着用mysql-client: mysql -h xxx.xxx.xxx.xxx -u root -p

我得到了以下错误: ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (111)

也通过MySQL的工作台,但我仍然无法连接到MySQL数据库。

我究竟做错了什么?

在您的主机上,mysql的my.cnf将绑定地址设置为0.0.0.0,以便mysql监听所有的networking接口

 bind-address = 0.0.0.0 

默认configuration是:

 # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. #bind-address = 127.0.0.1