简体   繁体   English

Docker Rails MySQL无法连接

[英]Docker rails mysql is not connecting

I am trying to connect my rails app which is on my host to docker mysql image. 我正在尝试将主机上的Rails应用程序连接到docker mysql映像。 But I am getting this error: 但我收到此错误:

 Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/Cellar/mysql/5.7.22/lib/plugin/caching_sha2_password.so, 2): image not found

My Docker compose file is like this: 我的Docker compose文件是这样的:

db:
  image: mysql
  restart: always
  ports:
    - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: password


adminer:
  image: adminer
  restart: always
  ports:
    - 8080:8080

I am using this inside my database.yml: 我在我的database.yml中使用它:

default: &default
adapter: mysql2
encoding: utf8
host: 127.0.0.1
username: root
password: password
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
# socket: /Applications/MAMP/tmp/mysql/mysql.sock

development:
  <<: *default
   database: meal_plan_development

What else I should do in order to connect my rails app to mysql docker image. 为了将Rails应用程序连接到mysql docker镜像,我还应该做什么。

As @vstm pointed out, this seems to be the same problem I was having with a PHP client. 正如@vstm指出的那样,这似乎我使用PHP客户端时遇到的问题相同 After the container has been created you could try changing the authentication scheme to one which will likely be supported eg 创建容器后,您可以尝试将身份验证方案更改为可能会支持的一种,例如

docker exec <container_id> /bin/sh -c "mysql -uroot -ppassword 
-e ALTER USER root@localhost IDENTIFIED WITH mysql_native_password BY 'PASSWORD'"

I'm not overly familiar with Docker, but I believe you can also add something like this to a Dockerfile so that the authentication method will be changed during the container initialization: 我对Docker不太熟悉,但是我相信您也可以在Dockerfile中添加类似的内容,以便在容器初始化期间更改身份验证方法:

RUN /bin/bash -c "mysql -uroot -ppassword 
    -e ALTER USER root@localhost IDENTIFIED WITH mysql_native_password BY 'PASSWORD'"  

MySQL default-authentication-plugin MySQL默认身份验证插件

as of version 8, MySQL uses caching_sha2_password as the default authentication plugin. 从版本8开始,MySQL使用caching_sha2_password作为默认身份验证插件。 You can override it to use mysql_native_password by adding a command instruction in your docker-compose.yml file like this: 您可以通过在docker-compose.yml文件中添加如下command指令来覆盖它以使用mysql_native_password

db:
   image: mysql
   command: --default-authentication-plugin=mysql_native_password
   restart: always
   ports:
      - "3306:3306"
   environment:
   MYSQL_ROOT_PASSWORD: password

adminer:
   image: adminer
   restart: always
   ports:
     - 8080:8080

I am certain 'host 127.0.0.1' is wrong. 我确定“主机127.0.0.1”是错误的。 Probably should be 'host db' 可能应该是“主机数据库”

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

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