简体   繁体   English

Docker mysql 连接失败,laravel

[英]Docker mysql connection fail with laravel

So I'm having troubles getting my Laravel app to connect to my MySQL container.所以我在让我的 Laravel 应用程序连接到我的 MySQL 容器时遇到了麻烦。 With the following docker-compose I get the error:使用以下 docker-compose 我收到错误:

Connection refused (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations)连接被拒绝(SQL:select * from information_schema.tables where table_schema = homestead and table_name = migrations)

database:
  image: mysql:5.7
    volumes:
      - dbdata:/var/lib/mysql
     environment:
      - "MYSQL_ROOT_PASSWORD=secret"
      - "MYSQL_DATABASE=homestead"
      - "MYSQL_USER=root"
      - "MYSQL_PASS=secret"
    ports:
      - "33061:3306"

If I change my image to mysql I get the error:如果我将图像更改为 mysql,则会出现错误:

The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations)服务器请求客户端未知的身份验证方法(SQL:select * from information_schema.tables where table_schema = homestead and table_name = migrations)

The server requested authentication method unknown to the client [caching_sha2_password]服务器请求客户端未知的身份验证方法 [caching_sha2_password]

My laravel .env looks like this:我的 laravel .env 看起来像这样:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=secret

I tried DB_HOST=database as well.我也试过 DB_HOST=database 。

Any help would be great.任何帮助都会很棒。 Thanks!谢谢!

OS: OSX操作系统:OSX

I guess you have some old data in dbdata folder and they are causing a problem.我猜您在dbdata文件夹中有一些旧数据,它们导致了问题。 If you don't need old DB, then just delete the content of this folder and start mysql container from the scratch.如果您不需要旧数据库,则只需删除此文件夹的内容并从头开始启动 mysql 容器。 Another problem is a port.另一个问题是端口。 You are publishing MySQL on the port 33061, so Laravel should be configured with DB_PORT=33061 .您正在端口 33061 上发布 MySQL,因此 Laravel 应配置为DB_PORT=33061

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

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