首页 > 解决方案 > 容器初始化后如何连接到docker-compose mysql?

问题描述

我有一个我一直在做的项目,现在我正在尝试使用 docker-compose 来构建它。在运行我自己的自定义脚本来创建表之前,我想我会试一试,看看它是否可以构建并且我可以连接到它。

我为 mysql 找到了一个合适的 docker-compose:

version: "3.8"
services:
  mysql:
    image: mysql:latest
    ports:
      - 3307:3307
    environment:
      MYSQL_ROOT_PASSWORD: SomeRootPassword1!
      MYSQL_USER: someuser
      MYSQL_PASSWORD: Password1!
      MYSQL_DATABASE: wedding

在 gitbash 中运行 docker-compose-up 后,我看到:

mysql_1  | 2020-08-26 20:00:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
mysql_1  | 2020-08-26 20:00:14+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_1  | 2020-08-26 20:00:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
mysql_1  | 2020-08-26 20:00:14+00:00 [Note] [Entrypoint]: Initializing database files
mysql_1  | 2020-08-26T20:00:14.502438Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.21) initializing of server in progress as process 43
mysql_1  | 2020-08-26T20:00:14.507639Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_1  | 2020-08-26T20:00:15.519301Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_1  | 2020-08-26T20:00:17.309017Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
mysql_1  | 2020-08-26 20:00:21+00:00 [Note] [Entrypoint]: Database files initialized
mysql_1  | 2020-08-26 20:00:21+00:00 [Note] [Entrypoint]: Starting temporary server
mysql_1  | 2020-08-26T20:00:22.149193Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.21) starting as process 90
mysql_1  | 2020-08-26T20:00:22.170767Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_1  | 2020-08-26T20:00:22.378726Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_1  | 2020-08-26T20:00:22.457272Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
mysql_1  | 2020-08-26T20:00:22.549270Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_1  | 2020-08-26T20:00:22.549394Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql_1  | 2020-08-26T20:00:22.553380Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1  | 2020-08-26T20:00:22.564741Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
mysql_1  | 2020-08-26 20:00:22+00:00 [Note] [Entrypoint]: Temporary server started.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
mysql_1  | 2020-08-26 20:00:24+00:00 [Note] [Entrypoint]: Creating database wedding
mysql_1  | 2020-08-26 20:00:24+00:00 [Note] [Entrypoint]: Creating user someuser
mysql_1  | 2020-08-26 20:00:24+00:00 [Note] [Entrypoint]: Giving user someuser access to schema wedding
mysql_1  |
mysql_1  | 2020-08-26 20:00:24+00:00 [Note] [Entrypoint]: Stopping temporary server
mysql_1  | 2020-08-26T20:00:24.697408Z 14 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.21).
mysql_1  | 2020-08-26T20:00:27.792197Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.21)  MySQL Community Server - GPL.
mysql_1  | 2020-08-26 20:00:28+00:00 [Note] [Entrypoint]: Temporary server stopped
mysql_1  |
mysql_1  | 2020-08-26 20:00:28+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
mysql_1  |
mysql_1  | 2020-08-26T20:00:28.931847Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.21) starting as process 1
mysql_1  | 2020-08-26T20:00:28.947655Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_1  | 2020-08-26T20:00:29.162873Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_1  | 2020-08-26T20:00:29.250374Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
mysql_1  | 2020-08-26T20:00:29.322041Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_1  | 2020-08-26T20:00:29.322181Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql_1  | 2020-08-26T20:00:29.328435Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1  | 2020-08-26T20:00:29.343021Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

问题

现在上面已经完成(不确定是否一切正确)我想我会尝试通过 mysql workbench 连接到它,但我得到以下信息:

在此处输入图像描述

我还尝试将绑定地址更改为:

在此处输入图像描述

我究竟做错了什么?

标签: mysqldockerdocker-compose

解决方案


在撰写文件中,在ports:您创建从localhost:3307到的转发规则container:3307

虽然您可以选择主机上未使用的任何源端口,但在另一端,您必须点击容器正在侦听的端口:在这种情况下,它将是3306.

您的 docker-compose.yml 文件应如下所示:

version: "3.8"
services:
  mysql:
    image: mysql:latest
    ports:
      - 3307:3306
    environment:
      MYSQL_ROOT_PASSWORD: SomeRootPassword1!
      MYSQL_USER: someuser
      MYSQL_PASSWORD: Password1!
      MYSQL_DATABASE: wedding

...那么你应该能够连接到localhost:3307


推荐阅读