Connect to mysql in a docker container from the host

后端 未结 14 821
青春惊慌失措
青春惊慌失措 2020-11-29 16:25

(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.)

14条回答
  •  甜味超标
    2020-11-29 16:59

    The simple method is to share the mysql unix socket to host machine. Then connect through the socket

    Steps:

    • Create shared folder for host machine eg: mkdir /host
    • Run docker container with volume mount option docker run -it -v /host:/shared .
    • Then change mysql configuration file /etc/my.cnf and change socket entry in the file to socket=/shared/mysql.sock
    • Restart MySQL service service mysql restart in docker
    • Finally Connect to MySQL servver from host through the socket mysql -u root --socket=/host/mysql.sock. If password use -p option

提交回复
热议问题