繁体   English   中英

Docker MySQL 容器未正确绑定到本地端口

[英]Docker MySQL container not binding to local port correctly

我正在尝试使用 docker-compose 将我的本地 3306 连接到我的 docker 容器 3306。 这一切正常,直到我尝试使用工作台更改表名时遇到 MySQL 错误。 从那以后,我一直无法使用正确的数据连接回容器。

这是我的 docker-compose.yml:

version: "3.3"
services:
  web_old:
    build:
      context: ./
      dockerfile: ./html/Dockerfile
    container_name: mackglobal_old
    depends_on:
      - db
    volumes:
      - ./:/var/www
      # - ./config/000-default.conf:/etc/apache2/000-default.conf
    ports:
      - 8000:80
  db:
    container_name: mysql8_old
    image: mysql:8.0.23
    environment:
      MYSQL_USER: "admin" # same as production so when we create views it doesnt cause definer errors
      MYSQL_PASSWORD: "root"
      MYSQL_ROOT_PASSWORD: "root"
    volumes:
      - ./mysql:/var/lib/mysql
      - ./my.cnf:/etc/mysql/conf.d/my.cnf
    ports:
      - "3306:3306"
    # command: mysqld --innodb_force_recovery=6 # USE THIS LINE IF THE MYSQL CONTAINER CRASHES TO RECOVER DATA IN READ

在我的应用程序中连接到数据库时,我遇到了这个 PHP PDO 错误:

Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'admin'@'172.18.0.3' (using password: YES)

当我连接到 docker 容器内的 mysql 实例时,我可以使用 docker-compose 文件中的用户名和密码访问服务器,并且我的所有数据库和表都在实例内。 但是当连接到工作台内的实例时,我所做的每个查询都会遇到:

Error Code: 1046. No database selected Select the default DB to be used by double-clicking its name in the SCHEMAS list in the sidebar.

以及运行SHOW databases; 我只得到information_schema

以下是我用来连接实例的详细信息:

主机名: MacBook-Pro-2.local(也试过:localhost,127.0.0.1)

端口: 3306(我还在撰写文件中将端口更改为 3307 并得到相同的错误)

用户名:管理员

密码: root

这是我的.cnf:

[mysqld]
default_time_zone='+00:00'
sql_mode=""
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init-connect='SET NAMES UTF8;'
innodb-flush-log-at-trx-commit=0
lower_case_table_names=2

这是运行docker-compose up --build时的日志:

mysql8_old | 2021-02-16 20:15:38+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
mysql8_old | 2021-02-16 20:15:38+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql8_old | 2021-02-16 20:15:38+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
mysql8_old | 2021-02-16T20:15:39.468943Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 1
mysql8_old | 2021-02-16T20:15:39.506466Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql8_old | 2021-02-16T20:15:40.895843Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql8_old | 2021-02-16T20:15:41.681805Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
mysql8_old | 2021-02-16T20:15:41.848877Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql8_old | 2021-02-16T20:15:41.849540Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql8_old | 2021-02-16T20:15:41.867833Z 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.
mysql8_old | 2021-02-16T20:15:42.028150Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

我还应该注意,我使用的最后一个环境是 mysql:8.0.21 但我遇到了同样的错误

任何帮助是极大的赞赏

添加:

我也试过重启 docker 服务和我的笔记本电脑

在容器处于活动状态时在端口 3306 上运行网络实用程序时:

端口扫描仪

编辑:

结果发现我的 docker 撰写文件中包含 mysql 数据./mysql的文件夹现在已损坏(没有错误),我仍然无法在恢复模式下访问任何内容。 幸运的是,我有一个备份,但我丢失了我最近所做的所有更改。 我仍然不确定出了什么问题,所以我没有解决这个问题

在我的 docker-compose.yml 中,我制作了这样的 mysql 容器:

mysql8-service:
        image: mysql:8
        container_name: mysql8-container
        ports:
            - "4306:3306"
        volumes:
            - ./mysql:/var/lib/mysql
        command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
        restart: always # always restart unless stopped manually
        environment:
            MYSQL_USER: root
            MYSQL_ROOT_PASSWORD: secret
            MYSQL_PASSWORD: secret
        networks:
            - nginx-php74-mysql8-node

它使我能够将我的数据库保存在我的应用程序的文件夹中,并且我的凭据很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM