前提・実現したいこと
Dockerを使ってRailsとMySQLの環境構築をしている際のエラーについてです。
⇓こちらの方の動画を参考に行っています。
https://www.youtube.com/watch?v=ltDdZAJli8c&t=526s
環境
WSL2 Ubuntu 20.04 LTS
Docker 20.10.8
ruby 2.7.4
rails 6.1.4.1
mysql 8.0
発生している問題・エラーメッセージ
ファイルを用意してrails newを実行
docker-compose run web rails new . --force --database=mysql
Gemファイル更新のためビルド
docker-compose build
データベース作成のためdb:create
docker-compose run web rails db:create
をすると下記のエラーが出ます。
Starting rails_docker_db_1 ... done Creating rails_docker_web_run ... done Unknown MySQL server host 'db' (-3) Couldn't create 'app_development' database. Please check your configuration. rails aborted! ActiveRecord::ConnectionNotEstablished: Unknown MySQL server host 'db' (-3) /app/bin/rails:5:in `<top (required)>' /app/bin/spring:10:in `block in <top (required)>' /app/bin/spring:7:in `tap' /app/bin/spring:7:in `<top (required)>' Caused by: Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-3) /app/bin/rails:5:in `<top (required)>' /app/bin/spring:10:in `block in <top (required)>' /app/bin/spring:7:in `tap' /app/bin/spring:7:in `<top (required)>' Tasks: TOP => db:create (See full trace by running task with --trace) ERROR: 1
ログ内容
2021-08-20 15:53:56+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started. 2021-08-20 15:53:56+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' 2021-08-20 15:53:56+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started. 2021-08-20T15:53:56.209620Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.26) initializing of server in progress as process 44 2021-08-20T15:53:56.216668Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive 2021-08-20T15:53:56.224730Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2021-08-20T15:53:57.855692Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. mysqld: Cannot change permissions of the file 'ca.pem' (OS errno 1 - Operation not permitted) 2021-08-20T15:53:59.908034Z 0 [ERROR] [MY-010295] [Server] Could not set file permission for ca.pem 2021-08-20T15:53:59.908674Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it. 2021-08-20T15:53:59.909864Z 0 [ERROR] [MY-010119] [Server] Aborting 2021-08-20T15:54:01.967550Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.26) MySQL Community Server - GPL. 2021-08-20T16:09:07.907548Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.26) starting as process 1 2021-08-20T16:09:07.915075Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive 2021-08-20T16:09:07.923107Z 0 [Warning] [MY-000054] [Server] World-writable config file '/var/lib/mysql/auto.cnf' is ignored. 2021-08-20T16:09:07.923938Z 0 [Warning] [MY-010107] [Server] World-writable config file '/var/lib/mysql/auto.cnf' has been removed. 2021-08-20T16:09:07.987156Z 0 [Warning] [MY-010075] [Server] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f2cb3056-01d0-11ec-9067-0242ac120002. 2021-08-20T16:09:07.996197Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2021-08-20T16:09:09.202246Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. mysqld: Table 'mysql.plugin' doesn't exist 2021-08-20T16:09:09.344689Z 0 [ERROR] [MY-010735] [Server] Could not open the mysql.plugin table. Please perform the MySQL upgrade procedure. 2021-08-20T16:09:09.345352Z 0 [Warning] [MY-010441] [Server] Failed to open optimizer cost constant tables 2021-08-20T16:09:09.345780Z 0 [Warning] [MY-010441] [Server] Failed to open optimizer cost constant tables 2021-08-20T16:09:09.346231Z 0 [Warning] [MY-010441] [Server] Failed to open optimizer cost constant tables 2021-08-20T16:09:09.346666Z 0 [Warning] [MY-010441] [Server] Failed to open optimizer cost constant tables 2021-08-20T16:09:09.347063Z 0 [Warning] [MY-010441] [Server] Failed to open optimizer cost constant tables 2021-08-20T16:09:09.347465Z 0 [Warning] [MY-010441] [Server] Failed to open optimizer cost constant tables 2021-08-20T16:09:09.347832Z 0 [Warning] [MY-010441] [Server] Failed to open optimizer cost constant tables 2021-08-20T16:09:09.402812Z 0 [Warning] [MY-010015] [Repl] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-08-20T16:09:09.485179Z 0 [Warning] [MY-010015] [Repl] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-08-20T16:09:09.489172Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main 2021-08-20T16:09:09.489385Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main 2021-08-20T16:09:09.489977Z 0 [Warning] [MY-013595] [Server] Failed to initialize TLS for channel: mysql_main. See below for the description of exact issue. 2021-08-20T16:09:09.490242Z 0 [Warning] [MY-010069] [Server] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key mysqld: Cannot change permissions of the file 'private_key.pem' (OS errno 1 - Operation not permitted) 2021-08-20T16:09:09.665658Z 0 [ERROR] [MY-010295] [Server] Could not set file permission for private_key.pem 2021-08-20T16:09:09.666204Z 0 [ERROR] [MY-010119] [Server] Aborting 2021-08-20T16:09:10.894979Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.26) MySQL Community Server - GPL. 2021-08-20 15:53:56+00:00 [Note] [Entrypoint]: Initializing database files 2021-08-20 16:08:57+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started. 2021-08-20 16:08:58+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' 2021-08-20 16:08:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
該当のソースコード
Dockerfile
FROM ruby:2.7 RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \ && apt-get update -qq \ && apt-get install -y nodejs yarn WORKDIR /app COPY ./src /app RUN bundle config --local set path 'vendor/bundle' \ && bundle install
docker-compose.yml
version: '3' services: db: image: mysql:8.0 command: --default-authentication-plugin=mysql_native_password volumes: - ./src/db/mysql_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: password web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - ./src:/app ports: - "3000:3000" depends_on: - db
Gemfile
source 'https://rubygems.org' gem 'rails', '~> 6.1.4'
database.yml
//抜粋 default: &default adapter: mysql2 encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: password host: db
試したこと
image、container、volumeを全て削除してからやり直しましたが、だめでした。
ぜひアドバイスいただけますと幸いです。
あなたの回答
tips
プレビュー