(It's probably a dumb question due to my limited knowledge with Docker or mysql administration, but since I spent a whole evening on this issue, I dare to ask it.)
In a nutshell
I want to run mysql in a docker container and connect to it from my host. So far, the best I have achieved is:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
More details
I'm using the following Dockerfile
:
FROM ubuntu:14.04.3 RUN apt-get update && apt-get install -y mysql-server # Ensure we won't bind to localhost only RUN grep -v bind-address /etc/mysql/my.cnf > temp.txt \ && mv temp.txt /etc/mysql/my.cnf # It doesn't seem needed since I'll use -p, but it can't hurt EXPOSE 3306 CMD /etc/init.d/mysql start && tail -F /var/log/mysql.log
In the directory where there is this file, I can succesfully build the image and run it with:
> docker build -t my-image . > docker run -d -p 12345:3306 my-image
When I attach to the image, it seems to work just fine:
# from the host > docker exec -it <my_image_name> bash #inside of the container now $ mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. [...]
However I don't have that much success from the host:
> mysql -P 12345 -uroot ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Even more details
grep -v
may feel weird. Admittedly, there may be cleaner way to do it. But when I attach my image, I can observe it actually worked as expected (ie: removed the bind-address
). And I can see in the container /var/log/mysql/error.log
:Server hostname (bind-address): '0.0.0.0'; port: 3306 - '0.0.0.0' resolves to '0.0.0.0'; Server socket created on IP: '0.0.0.0'.
Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.
Accessing the Host With the Default Bridge Mode You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.
To access host machine from the docker container you must attach an IP alias to your network interface. You can bind whichever IP you want, just make sure you're not using it to anything else. Then make sure that you server is listening to the IP mentioned above or 0.0. 0.0 .
If your Docker MySQL host is running correctly you can connect to it from local machine, but you should specify host, port and protocol like this:
mysql -h localhost -P 3306 --protocol=tcp -u root
Change 3306 to port number you have forwarded from Docker container (in your case it will be 12345).
Because you are running MySQL inside Docker container, socket is not available and you need to connect through TCP. Setting "--protocol" in the mysql command will change that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With