简体   繁体   English

Docker-Rails 无法连接本地主机 mysql 数据库

[英]Docker-Rails Can't Connect localhost mysql database

I'm trying to have a docker container use my local mysql database.我正在尝试让 docker 容器使用我的本地 mysql 数据库。 Using docker-compose up, I am able to get all the pieces running, however, the db exits with status code 1, and the rails application cannot connect to any database instance.使用 docker-compose 启动,我可以让所有部分运行,但是,db 以状态码 1 退出,并且 rails 应用程序无法连接到任何数据库实例。 I know that mysql is running on my system and that the service has been started.我知道 mysql 正在我的系统上运行并且服务已经启动。 My docker-compose file:我的 docker-compose 文件:

version: '3'
services: 
    db:
        image: mysql
        restart: always
        ports:
            - "3306:3306"
        environment: 
            MYSQL_ROOT_PASSWORD: password
            MYSQL_DATABASE: db
            MYSQL_USER: root
            MYSQL_PASSWORD: password
        volumes: 
            - ./tmp/db:/var/lib/mysql
    web:
        build: .
        image: rails-devise:1.0.0
        command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
        volumes: 
            - ".:/baas-status"
        ports:
            - "3000:3000"
        depends_on: 
            - db

I know the containers are running because docker ps gives the following output:我知道容器正在运行,因为docker ps给出了以下 output:

CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                                         NAMES
1a49aba0cd68        rails-devise:1.0.0   "entrypoint.sh bash …"   23 minutes ago      Up 2 minutes        0.0.0.0:3000->3000/tcp                        status_web_1
38554baf5efc        mysql                "docker-entrypoint.s…"   23 minutes ago      Up 2 minutes        3306/tcp, 0.0.0.0:3002->3002/tcp, 33060/tcp   status_db_1

Even though I exposed port 3002 and want to use that as the port for the database, is it unable to connect because I'm on the wrong port?即使我暴露了端口 3002 并想将其用作数据库的端口,它是否因为我在错误的端口上而无法连接? I see in the docker ps output that mysql is on port 3306.我在docker ps output 中看到 mysql 在端口 3306 上。

My database.yml file:我的 database.yml 文件:

base: &base
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

default: &default
  <<: *base
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>
  database: <%= ENV['DB_NAME'] %>

development:
  <<: *base
  username: root
  password: password
  host: db
  database: db

I have also tried editing the host for development to be host: db .我还尝试将开发主机编辑为host: db That didn't change anything either.这也没有改变任何东西。 I tried running docker-compose run web rake db:create as well, and I get the following error:我也尝试运行docker-compose run web rake db:create ,但出现以下错误:

This has been fixed with the current changes to the docker-compose file Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)通过对 docker-compose 文件的当前更改已修复此问题 无法通过套接字“/var/run/mysqld/mysqld.sock”连接到本地 MySQL 服务器 (2)

At this point, I'm at a loss on where to proceed.在这一点上,我不知道从哪里开始。 Any information or help would be greatly appreciated.任何信息或帮助将不胜感激。 Thank you.谢谢你。

Edit:编辑:

I have changed the ports for the db and it seems to have fixed the connection issues.我已经更改了数据库的端口,它似乎已经解决了连接问题。 Now I'm running into an error that gives现在我遇到了一个错误

web_1   | Mysql2::Error::ConnectionError (Plugin caching_sha2_password could not be loaded: /usr/lib/x86_64-linux-gnu/mariadb19/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory):

I'm going to do some research to figure out the meaning behind this error, any tips to move in the right direction would be greatly appreciated though!我将进行一些研究以找出此错误背后的含义,但是,任何朝着正确方向前进的提示都将不胜感激!

Edit2:编辑2:

I ended up connecting the db by reverting mysql back to 5.7.18 to avoid the sha authentication.我最终通过将 mysql 恢复到 5.7.18 来避免 sha 身份验证来连接数据库。 The docker-compose file and settings that worked for me are as follows:对我有用的 docker-compose 文件和设置如下:

docker-compose.yml docker-compose.yml

version: '2'
services: 
    db:
        image: mysql:5.7.18
        restart: always
        ports:
            - 3306:3306
        environment: 
            - MYSQL_ROOT_PASSWORD=root
        volumes: 
            - db-data:/var/lib/mysql
    web:
        build: .
        image: rails-devise:1.0.0
        command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
        volumes: 
            - ".:/baas-status"
        ports:
            - 3000:3000
        links:
            - db
        depends_on: 
            - db
volumes: 
    db-data:
        driver: local

The database.yml file remained the same with the exception of changing the password to root.除了将密码更改为 root 之外,database.yml 文件保持不变。 This is the solution that worked for me.这是对我有用的解决方案。

Resource I ended up using! 我最终使用的资源!

Sounds like you figured out the connection issue.听起来你发现了连接问题。 You'll want to connect on 3306 , not 3002 based on the docker-compose file you posted.根据您发布的 docker-compose 文件,您需要连接3306 ,而不是3002

I assume you also made sure to define these environment variables in your rails docker container as well.我假设您还确保在您的 rails docker 容器中定义这些环境变量。 Is that correct?那是对的吗?

  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>
  database: <%= ENV['DB_NAME'] %>

Since you do not define a tag of the mysql image to use in your docker-compose file, it will pull the latest image which is mysql ~8.x .由于您没有在 docker-compose 文件中定义要使用的 mysql 图像的标签,因此它将提取latest的图像 mysql ~8.x MySQL 8 uses the caching_sha2_password authentication plugin by default (unless you specify otherwise). MySQL 8 默认使用caching_sha2_password身份验证插件(除非您另外指定)。 The Rails mysql2 driver version you're using likely does not support this authentication mechanism yet.您使用的 Rails mysql2驱动程序版本可能还不支持这种身份验证机制。

So, if you would like to revert to an older authentication plugin (like the one from mysql 5.7), you can use a command line flag in your mysql container like so:因此,如果您想恢复到较旧的身份验证插件(例如 mysql 5.7 中的插件),您可以在 mysql 容器中使用命令行标志,如下所示:

services:
  ...
  db:
    image: mysql:8.0
    ...
    command: mysqld --default-authentication-plugin=mysql_native_password

More information can also be found on this SO answer.更多信息也可以在这个 SO 答案中找到。

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

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