简体   繁体   中英

Docker rails mysql is not connecting

I am trying to connect my rails app which is on my host to docker mysql image. 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:

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:

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.

As @vstm pointed out, this seems to be the same problem I was having with a PHP client. 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:

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

MySQL default-authentication-plugin

as of version 8, MySQL uses caching_sha2_password as the default authentication plugin. You can override it to use mysql_native_password by adding a command instruction in your docker-compose.yml file like this:

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. Probably should be 'host db'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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