无法连接到windows计算机上docker上托管的mysql

k75qkfdt  于 2021-06-15  发布在  Mysql
关注(0)|答案(3)|浏览(380)

我已经在这里提到:无法从docker示例连接到mysql,但这次我在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#
col17t5w

col17t5w1#

如果你从https://dev.mysql.com/downloads/windows/ 然后,您就可以访问docker托管的mysql,访问方式与访问任何其他mysql数据库相同(注意,不需要在数据库容器中获取根shell)。因为您使用的是docker工具箱,所以可能会使用192.168.99.100作为数据库服务器的ip地址。

1bqhqjot

1bqhqjot2#

您应该尝试通过环回接口连接,因为您没有访问套接字的权限。

mysql -h 127.0.0.1 -uroot -p

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

v1uwarro

v1uwarro3#

尝试在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:

相关问题