无法连接到Windows计算机上Docker上托管的Mysql

时间:2018-12-16 18:49:53

标签: mysql docker docker-compose

我已经执行过这里的提及:Unable to connect to MYSQL from Docker Instance,但是这次我在Windows计算机上运行docker。

pc@DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ docker pull mysql/mysql-server
Using default tag: latest
latest: Pulling from mysql/mysql-server
e64f6e679e1a: Pull complete
799d60100a25: Pull complete
85ce9d0534d0: Pull complete
d3565df0a804: Pull complete
Digest: sha256:59a5854dca16488305aee60c8dea4d88b68d816aee627de022b19d9bead48d04
Status: Downloaded newer image for mysql/mysql-server:latest

pc@DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
79ff1c03452ab2eac0d798b576ffeabde24d4c5aa6954d3d5c5bef99dcc40ce8

pc@DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ mysql -uroot -ppassword
bash: mysql: command not found

pc@DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ docker exec -it mysql bash
bash-4.2# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
bash-4.2# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
bash-4.2#

3 个答案:

答案 0 :(得分:0)

您应尝试通过环回接口进行连接,因为您无权访问套接字。

mysql -h 127.0.0.1 -uroot -p

在这种情况下,就像您的服务器在另一台计算机上运行一样,与主机共享的唯一东西是暴露的端口。

答案 1 :(得分:0)

如果您从https://dev.mysql.com/downloads/windows/下载MySQL客户端,则可以访问Docker托管的MySQL,就像访问其他任何MySQL数据库一样(注意,无需获取root shell)在数据库容器中)。由于您使用的是Docker Toolbox,因此您可能会使用192.168.99.100作为数据库服务器的IP地址。

答案 2 :(得分:0)

尝试在docker-compose.y \ ml中添加以下内容

    services:
      db:
        image: mysql:5.7
        environment:
         MYSQL_DATABASE: 'Dbname'
         # So you don't have to use root, but you can if you like
         MYSQL_USER: 'username'
         # You can use whatever password you like
         MYSQL_PASSWORD: 'Password'
         # Password for root access
         MYSQL_ROOT_PASSWORD: 'RootPassword'
        ports:
         # <Port exposed> : < MySQL Port running inside container>
          - '3307:3306'
        expose:
         # Opens port 3306 on the container
          - '3306'
         # Where our data will be persisted
        volumes:
        - my-db:/var/lib/mysql
    volumes:
      my-db: