Connect to mysql in a docker container from the host

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

    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.

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

    I recommend checking out docker-compose. Here's how that would work:

    Create a file named, docker-compose.yml that looks like this:

    version: '2'
    
    services:
    
      mysql:
        image: mariadb:10.1.19
        ports:
          - 8083:3306
        volumes:
          - ./mysql:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: wp
    

    Next, run:

    $ docker-compose up

    Notes:

    • For latest mariadb image tag see https://hub.docker.com/_/mariadb/

    Now, you can access the mysql console thusly:

    $ mysql -P 8083 --protocol=tcp -u root -p

    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 5.5.5-10.1.19-MariaDB-1~jessie mariadb.org binary distribution
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>
    

    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.

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

    I do this by running a temporary docker container against my server so I don't have to worry about what is installed on my host. First, I define what I need (which you should modify for your purposes):

    export MYSQL_SERVER_CONTAINER=mysql-db
    export MYSQL_ROOT_PASSWORD=pswd 
    export DB_DOCKER_NETWORK=db-net
    export MYSQL_PORT=6604
    

    I always create a new docker network which any other containers will need:

    docker network create --driver bridge $DB_DOCKER_NETWORK
    

    Start a mySQL database server:

    docker run --detach --name=$MYSQL_SERVER_CONTAINER --net=$DB_DOCKER_NETWORK --env="MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD" -p ${MYSQL_PORT}:3306 mysql
    

    Capture IP address of the new server container

    export DBIP="$(docker inspect ${MYSQL_SERVER_CONTAINER} | grep -i 'ipaddress' | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])')"
    

    Open a command line interface to the server:

    docker run -it -v ${HOST_DATA}:/data --net=$DB_DOCKER_NETWORK --link ${MYSQL_SERVER_CONTAINER}:mysql --rm mysql sh -c "exec mysql -h${DBIP} -uroot -p"
    

    This last container will remove itself when you exit the mySQL interface, while the server will continue running. You can also share a volume between the server and host to make it easier to import data or scripts. Hope this helps!

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

    run following command to run container

    docker run --name db_name -e MYSQL_ROOT_PASSWORD=PASS--publish 8306:3306 db_name
    

    run this command to get mysql db in host machine

    mysql -h 127.0.0.1 -P 8306 -uroot  -pPASS
    

    in your case it is

    mysql -h 127.0.0.1 -P 12345 -uroot  -pPASS
    
    0 讨论(0)
  • 2020-11-29 16:49

    if you running docker under docker-machine?

    execute to get ip:

    docker-machine ip <machine>
    

    returns the ip for the machine and try connect mysql:

    mysql -h<docker-machine-ip>
    
    0 讨论(0)
  • 2020-11-29 16:53

    If you use "127.0.0.1" instead of localhost mysql will use tcp method and you should be able to connect container with:

    mysql -h 127.0.0.1 -P 3306 -u root
    
    0 讨论(0)
提交回复
热议问题