Connect to mysql in a docker container from the host

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

    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.

提交回复
热议问题