Unable to connect to dockerized mysql db remotely

不打扰是莪最后的温柔 提交于 2021-01-01 02:22:16

问题


On my AWS ec2 server I have docker 1.9.1 installed. In an image test_image based from ubuntu:trusty official docker image, I have tried to setup the LEMP(Linux, Nginx, MySQL, PHP) architecture.

Following is the docker command i have used to start my container:

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"

I have exposed port 80 and 3306 to the host's network interface and have also allowed AWS's security group to allow inbound connections to these ports. Connection type in security group is: MYSQL/Aurora and protocol is: TCP (I know its not very secure, its only for initial implementation. Production setup will be different)

I followed this DigitalOcean tutorial: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04

After installing Nginx and starting it I am able to test it in the browser via ec2's pubic ip i.e. http://xxx.xxx.xxx.xxx shows the default nginx welcome page.

While installing MySQL, I followed the following commands in the docker container:

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

I have given a password to my root user and during mysql_secure_installation i had allowed remote access to root user.

mysql -u root -p command from inside the container connects me to the mysql db but not from outside the container.

Also from my local machine: I tried with mysql-client: mysql -h xxx.xxx.xxx.xxx -u root -p

I got the following error: ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (111)

and also through mysql workbench but I still can't connect to the mysql db.

What am I doing wrong?


回答1:


In your host mysql's my.cnf set the bind address to 0.0.0.0 so that mysql listens on all network interfaces

bind-address = 0.0.0.0

The default config is:

# 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


来源:https://stackoverflow.com/questions/35580488/unable-to-connect-to-dockerized-mysql-db-remotely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!