Connect to mysql in a docker container from the host

后端 未结 14 749
青春惊慌失措
青春惊慌失措 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:55

    In your terminal run: docker exec -it container_name /bin/bash Then: mysql

    0 讨论(0)
  • 2020-11-29 16:58

    For conversion,you can create ~/.my.cnf file in host:

    [Mysql]
    user=root
    password=yourpass
    host=127.0.0.1
    port=3306
    

    Then next time just run mysql for mysql client to open connection.

    0 讨论(0)
  • 2020-11-29 16:58

    change "localhost" to your real con ip addr
    because it's to mysql_connect()

    0 讨论(0)
  • 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 <mysql image>.
    • 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
    0 讨论(0)
  • 2020-11-29 17:01
    • docker run -e MYSQL_ROOT_PASSWORD=pass --name sql-db -p 3306:3306 mysql

    • docker exec -it sql-db bash

    • mysql -u root -p

    0 讨论(0)
  • 2020-11-29 17:06

    I was able to connect my sql server5.7 running on my host using the below command : mysql -h 10.10.1.7 -P 3307 --protocol=tcp -u root -p where the ip given is my host ip and 3307 is the port forwaded in mysql docker .After entering the command type the password for myql.that is it.Now you are connected the mysql docker container from the you hostmachine

    0 讨论(0)
提交回复
热议问题