前提・実現したいこと
docker環境でポートフォリオを開発中にMySQLでFailed to find tablespace for table myapp_development
.schema_migrations
in the cache. Attempting to load the tablespace with space id 652
というエラーが発生しました。
このエラーを解消したいです。よろしくお願いします。
発生している問題・エラーメッセージ
開発中にdocker-compose upをすると下記のようなエラーが発生するようになってしまいました。
<考えられる原因>
エラーが発生する直前は、ポートフォリオ内で投稿をランキング表示させる機能を実装してローカル環境で動作を確認しようとしていました。このデータの処理が重くてエラーが発生してしまったのではないかなと思われます。(他に思い当たる節はないです。)
web_1 | Use Ctrl-C to stop web_1 | Started GET "/" for 172.19.0.1 at 2021-03-31 10:32:22 +0000 web_1 | Cannot render console from 172.19.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1 db_1 | 2021-03-31T10:32:22.896072Z 2 [ERROR] InnoDB: Failed to find tablespace for table `myapp_development`.`schema_migrations` in the cache. Attempting to load the tablespace with space id 652 db_1 | 2021-03-31T10:32:22.902023Z 2 [ERROR] InnoDB: In file './myapp_development/schema_migrations.ibd', tablespace id and flags are 697 and 33, but in the InnoDB data dictionary they are 652 and 33. Have you moved InnoDB .ibd files around without using the commands DISCARD TABLESPACE and IMPORT TABLESPACE? Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting-datadict.html for how to resolve the issue. db_1 | 2021-03-31T10:32:22.902085Z 2 [ERROR] InnoDB: Operating system error number 2 in a file operation. db_1 | 2021-03-31T10:32:22.902396Z 2 [ERROR] InnoDB: The error means the system cannot find the path specified. db_1 | 2021-03-31T10:32:22.902433Z 2 [ERROR] InnoDB: Could not find a valid tablespace file for `myapp_development/schema_migrations`. Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting-datadict.html for how to resolve the issue. db_1 | 2021-03-31T10:32:22.902529Z 2 [Warning] InnoDB: Cannot calculate statistics for table `myapp_development`.`schema_migrations` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting.html for how to resolve the issue. web_1 | web_1 | ActiveRecord::StatementInvalid (Mysql2::Error: Tablespace is missing for table `myapp_development`.`schema_migrations`.): web_1 | web_1 | mysql2 (0.5.3) lib/mysql2/client.rb:131:in `_query' web_1 | mysql2 (0.5.3) lib/mysql2/client.rb:131:in `block in query' web_1 | mysql2 (0.5.3) lib/mysql2/client.rb:130:in `handle_interrupt' web_1 | mysql2 (0.5.3) lib/mysql2/client.rb:130:in `query' web_1 | rack-mini-profiler (2.3.1) lib/patches/db/mysql2/alias_method.rb:25:in `block in query' web_1 | rack-mini-profiler (2.3.1) lib/patches/sql_patches.rb:12:in `record_sql' web_1 | rack-mini-profiler (2.3.1) lib/patches/db/mysql2/alias_method.rb:24:in `query'(下記、略)
dockercompose
1version: '3' 2 3services: 4 db: 5 image: mysql:5.7 6 environment: 7 MYSQL_USER: root 8 MYSQL_ROOT_PASSWORD: password 9 ports: 10 - "3306:3306" 11 volumes: 12 - ./db/mysql/volumes:/var/lib/mysql 13 14 web: 15 build: . 16 command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 17 volumes: 18 - .:/myapp 19 - gem_data:/usr/local/bundle 20 ports: 21 - 3000:3000 22 depends_on: 23 - db 24 tty: true 25 stdin_open: true 26 27volumes: 28 gem_data:
dockerfile
1version: '3' 2 3services: 4 db: 5 image: mysql:5.7 6 environment: 7 MYSQL_USER: root 8 MYSQL_ROOT_PASSWORD: password 9 ports: 10 - "3306:3306" 11 volumes: 12 - ./db/mysql/volumes:/var/lib/mysql 13 14 web: 15 build: . 16 command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 17 volumes: 18 - .:/myapp 19 - gem_data:/usr/local/bundle 20 ports: 21 - 3000:3000 22 depends_on: 23 - db 24 tty: true 25 stdin_open: true 26 27volumes: 28 gem_data:
試したこと
①他の投稿の解決方法を参考に、docker-compose downを行い、db:create,db:migrateを行いましたが解消されませんでした。どちらもalready existになってしまいます。
$ docker-compose run web bundle exec rake db:create Creating portfolio_app_web_run ... done Database 'myapp_development' already exists Database 'myapp_test' already exists
②already existsとあったのでmysqlにつなげ、myapp_development
.`schema_migrationsのデータベースを確認
mysql> show databases;+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
しかしmyapp_development.
schema_migrationsのデータベースは、mysql内に見つかりません。
データベースがいくつかあったのでmysqlに接続。
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +------------------------------------------------------+ | Tables_in_mysql | +------------------------------------------------------+ | columns_priv | | component | | db | | default_roles | | engine_cost | | func | | general_log | | global_grants | | gtid_executed | | help_category | | help_keyword | | help_relation | | help_topic | | innodb_index_stats | | innodb_table_stats | | password_history | | plugin | | procs_priv | | proxies_priv | | replication_asynchronous_connection_failover | | replication_asynchronous_connection_failover_managed | | role_edges | | server_cost | | servers | | slave_master_info | | slave_relay_log_info | | slave_worker_info | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +------------------------------------------------------+ 35 rows in set (0.01 sec)
一応テーブル見てみましたが特に何も分からず。。。(ローカルで、すでにuser、ramen、likeモデルを作っているのですが見当たらないのは正常なのでしょうか?)
③fileの確認
エラーの中にCannot calculate statistics for table myapp_development
.schema_migrations
because the .ibd file ****is missingとあったのでfileも見てみました。
公式レファレンスを(https://dev.mysql.com/doc/refman/5.7/en/innodb-information-schema-files-table.html)参考に下記の実行。
mysql> SELECT FILE_ID, FILE_NAME, FILE_TYPE, TABLESPACE_NAME, FREE_EXTENTS, -> TOTAL_EXTENTS, EXTENT_SIZE, INITIAL_SIZE, MAXIMUM_SIZE, AUTOEXTEND_SIZE, DATA_FREE, STATUS ENGINE -> FROM INFORMATION_SCHEMA.FILES WHERE TABLESPACE_NAME LIKE 'innodb_system' \G *************************** 1. row *************************** FILE_ID: 0 FILE_NAME: ./ibdata1 FILE_TYPE: TABLESPACE TABLESPACE_NAME: innodb_system FREE_EXTENTS: 2 TOTAL_EXTENTS: 12 EXTENT_SIZE: 1048576 INITIAL_SIZE: 12582912 MAXIMUM_SIZE: NULL AUTOEXTEND_SIZE: 67108864 DATA_FREE: 6291456 ENGINE: NORMAL 1 row in set (0.01 sec) mysql> SELECT FILE_ID, FILE_NAME FROM INFORMATION_SCHEMA.FILES -> WHERE FILE_NAME LIKE '%.ibd%' ORDER BY FILE_ID; +------------+----------------------+ | FILE_ID | FILE_NAME | +------------+----------------------+ | 1 | ./sys/sys_config.ibd | | 4294967294 | ./mysql.ibd | +------------+----------------------+ 2 rows in set (0.01 sec) mysql> SELECT FILE_ID, FILE_NAME FROM INFORMATION_SCHEMA.FILES -> WHERE FILE_NAME LIKE '%ibtmp%'; +------------+-----------+ | FILE_ID | FILE_NAME | +------------+-----------+ | 4294967293 | ./ibtmp1 | +------------+-----------+ 1 row in set (0.01 sec) mysql> SELECT FILE_ID, FILE_NAME FROM INFORMATION_SCHEMA.FILES -> WHERE FILE_NAME LIKE '%undo%'; +------------+------------+ | FILE_ID | FILE_NAME | +------------+------------+ | 4294967279 | ./undo_001 | | 4294967278 | ./undo_002 | +------------+------------+
./ibtmp1が.ibdファイルという事なのでしょうか?その場合、idbファイルは存在しているように見えるのですが。。
./undo_001がinvalidになってしまったということなのでしょうか?
具体的なエラー解消方法がわからないため、アドバイス等いただきたいです。
よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
ruby '2.5.8'
rails 6.1.3
mysql 8.0.23
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。