上下文:Rails 6
应用程序托管在Docker
中,使用docker-compose
当Rails容器启动时,我得到:Mysql2::Error::ConnectionError: Access denied for user 'user'@'%' to database 'db_production'
即使在MySQL完全初始化之后
My docker-composite.yml:
services:
app_production:
image: app_production
build:
context: .
dockerfile: ./dockerfiles/production.dockerfile
container_name: app_production
volumes:
- ./public:/app_production/public
- mysql_socket:/var/run/mysqld
command: bash -c "sleep 5 && bundle exec rails db:create db:migrate && bundle exec puma -C config/puma.rb"
restart: always
environment:
RAILS_ENV: production
SECRET_KEY_BASE: ${PRODUCTION_SECRET_KEY_BASE}
APP_DATABASE_PASSWORD: ${PRODUCTION_MYSQL_PASSWORD}
PORT: 5000
ports:
- "5000:5000"
depends_on:
- database_production
links:
- database_production
database_production:
image: mysql:8.0.20
command: mysqld --default-authentication-plugin=mysql_native_password
restart: always
container_name: database_production
environment:
MYSQL_USER: user
MYSQL_PASSWORD: ${PRODUCTION_MYSQL_PASSWORD}
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
volumes:
- dbdata_production:/var/lib/mysql
- mysql_socket:/var/run/mysqld
ports:
- "3307:3306"
volumes:
dbdata_production:
mysql_socket:
我的Dockerfile:
FROM ruby:2.6.1
RUN echo "deb http://archive.debian.org/debian stretch main" > /etc/apt/sources.list
RUN curl https://deb.nodesource.com/setup_12.x | bash
RUN curl https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y nodejs yarn
RUN apt-get update && apt-get install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
RUN apt-get install -y default-mysql-client
WORKDIR /app_production
RUN export RAILS_ENV=production
RUN gem install bundler:2.2.31
COPY Gemfile Gemfile.lock ./
COPY package.json ./
COPY vendor/gems/ vendor/gems/
RUN bundle install
COPY . ./
RUN yarn install --check-files
EXPOSE 5000
CMD ["RAILS_ENV=production", "bundle", "exec", "puma", "-C", "config/puma.rb"]
database.yml
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
production:
<<: *default
host: database_production
port: 3306
database: db_production
username: user
password: "some_password"
socket: "/var/run/mysqld/mysqld.sock"
注意事项:
- 此配置在Ubuntu 22.04上的另一台服务器上工作正常
- 我现在正试图让它在Ubuntu 20.04的服务器上工作
- 我必须将
RUN apt-get install -y default-mysql-client
添加到Dockerfile中,以使MySQL在Rails容器中工作,否则找不到它,但在我的其他服务器上,我不必这样做 - 我还必须为
mysql_socket:/var/run/mysqld
创建一个共享卷,否则找不到套接字 - 我能够使用相同的凭据在Rails容器上使用CLI登录MySQL
- 如果我将
phpMyAdmin
服务放在docker-compose中,我也可以使用相同的凭据登录MySQL - 如果我尝试通过容器的rails控制台连接到MySQL,它可以工作
- 如果我从rails container命令中删除
bundle exec rails db:create db:migrate
,则rails将启动
所以我不太明白到底发生了什么,因为我可以通过容器的CLI和PhpMyAdmin成功连接到MySQL。为什么Rails不能连接到它?没有详细的日志。
它在步骤rake db:create
失败,当试图通过rails控制台手动运行它时,我得到了同样的错误。很奇怪。
有线索吗
1条答案
按热度按时间xwbd5t1u1#
找到解决方案:这是MySQL用户的权限问题。
必须以root身份连接到MySQL并运行以下命令: