Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.


2 Answers

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
like image 163
Hamid Asghari Avatar answered Sep 18 '25 02:09

Hamid Asghari


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 e.g.

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'"  
like image 36
Ash Avatar answered Sep 18 '25 02:09

Ash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!