Connect to mysql in a docker container from the host

后端 未结 14 748
青春惊慌失措
青春惊慌失措 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 17:07
    mysql -u root -P 4406 -h localhost --protocol=tcp -p
    

    Remember to change the user, port and host so that it matches your configurations. The -p flag is required if your database user is configured with a password

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

    OK. I finally solved this problem. Here follows my solution used in https://sqlflow.org/sqlflow.

    The Complete Solution

    To make the demo self-contained, I moved all necessary code to https://github.com/wangkuiyi/mysql-server-in-docker.

    The Key to the Solution

    I don't use the official image on DockerHub.com https://hub.docker.com/r/mysql/mysql-server. Instead, I made my own by installing MySQL on Ubuntu 18.04. This approach gives me the chance to start mysqld and bind it to 0.0.0.0 (all IPs).

    For details, please refer to these lines in my GitHub repo.

    SQLFLOW_MYSQL_HOST=${SQLFLOW_MYSQL_HOST:-0.0.0.0}
    
    echo "Start mysqld ..."
    sed -i "s/.*bind-address.*/bind-address = ${SQLFLOW_MYSQL_HOST}/" \
        /etc/mysql/mysql.conf.d/mysqld.cnf
    service mysql start
    

    To Verify My Solution

    1. Git clone the aforementioned repo.
      git clone https://github.com/wangkuiyi/mysql-server-in-docker
      cd mysql-server-in-docker
      
    2. Build the MySQL server Docker image
      docker build -t mysql:yi .
      
    3. Start MySQL server in a container
      docker run --rm -d -p 23306:3306 mysql:yi
      
    4. Install the MySQL client on the host, if not yet. I am running Ubuntu 18.04 on the host (my workstation), so I use apt-get.
      sudo apt-get install -y mysql-client
      
    5. Connect from the host to the MySQL server running in the container.
      mysql --host 127.0.0.1 --port 23306 --user root -proot
      

    Connect from Another Container on the Same Host

    We can run MySQL client from even another container (on the same host).

    docker run --rm -it --net=host mysql/mysql-server mysql \
       -h 127.0.0.1 -P 13306 -u root -proot
    

    Connect from Another Host

    On my iMac, I install the MySQL client using Homebrew.

    brew install mysql-client
    export PATH="/usr/local/opt/mysql-client/bin:$PATH"
    

    Then, I can access the above Ubuntu host (192.168.1.22).

    mysql -h 192.168.1.22 -P 13306 -u root -proot
    

    Connect from a Container Running on Another Host

    I can even run MySQL client in a container running on the iMac to connect to the MySQL server in a container on my Ubuntu workstation.

    docker run --rm -it --net=host mysql/mysql-server mysql \
        -h 192.168.1.22 -P 13306 -u root -proot
    

    A Special Case

    In the case that we run MySQL client and server in separate containers running on the same host -- this could happen when we are setting up a CI, we don't need to build our own MySQL server Docker image. Instead, we can use the --net=container:mysql_server_container_name when we run the client container.

    To start the server

    docker run --rm -d --name mysql mysql/mysql-server
    

    To start the client

    docker run --rm -it --net=container:mysql mysql/mysql-server mysql \
     -h 127.0.0.1 -P 3306 -u root -proot
    
    0 讨论(0)
提交回复
热议问题