(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 achieve 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
- I've seen that there's a question which looks like mine. However, it doesn't actually is the same (and it doesn't have any answer anyway)
- I've seen that there are images dedicated to mysql, but I didn't had more success with them
- My
grep -v
may feel weird. Admittedly, there may be cleaner way to do it. But when I'm attached to my image, I can observe it actually worked as expected (ie: removed thebind-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'.
For conversion,you can create
~/.my.cnf
file in host:Then next time just run
mysql
for mysql client to open connection.If you use "127.0.0.1" instead of localhost mysql will use tcp method and you should be able to connect container with:
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:
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.
I recommend checking out docker-compose. Here's how that would work:
Create a file named, docker-compose.yml that looks like this:
Next, run:
Notes:
Now, you can access the mysql console thusly:
Notes:
You can pass the -d flag to run the mysql/mariadb container in detached/background mode.
The password is "wp" which is defined in the docker-compose.yml file.
Same advice as maniekq but full example with docker-compose.
The simple method is to share the mysql unix socket to host machine. Then connect through the socket
Steps:
mkdir /host
docker run -it -v /host:/shared <mysql image>
./etc/my.cnf
and change socket entry in the file tosocket=/shared/mysql.sock
service mysql restart
in dockermysql -u root --socket=/host/mysql.sock
. If password use -p optionif you running docker under docker-machine?
execute to get ip:
returns the ip for the machine and try connect mysql: