MariaDBが起動してすぐシャットダウンする

# 何もしてないのに壊れた

スタートアップにXAMPP Control Panelを入れてる。
使わない日もあるけど
使うタイミングが来てパネルを見てみたら
以下のようなエラーが出ていた
```
9:46:04  [mysql] 	Status change detected: running
9:46:06  [mysql] 	Status change detected: stopped
9:46:06  [mysql] 	Error: MySQL shutdown unexpectedly.
9:46:06  [mysql] 	This may be due to a blocked port, missing dependencies, 
9:46:06  [mysql] 	improper privileges, a crash, or a shutdown by another method.
9:46:06  [mysql] 	Press the Logs button to view error logs and check
9:46:06  [mysql] 	the Windows Event Viewer for more clues
9:46:06  [mysql] 	If you need more help, copy and post this
9:46:06  [mysql] 	entire log window on the forums
```

パネル上でもポート番号の表示が2秒ぐらい出てすぐ消えるといった感じ

# とりあえず動くように直したい
以下のサイトの手順を追って実行してみた

https://community.apachefriends.org/f/viewtopic.php?f=16&t=81537&p=275028&hilit=ibtmp1#p275028

## 不要ファイル削除
```\xampp\mysql\data```フォルダ内にある以下のファイルを削除してみる
* multi-master.info
* master-*
* mysql_relay-bin-*
* relay-log-*
* ib_logfile*
* aria_log.*

master-2023@...みたいなやたら長いファイル名のがいくつもあった。いつの間に？

削除後のフォルダ構成
mysql/data
┣データフォルダ(mysqlとかphpmyadminとか)
┣：
┣ib_buffer_pool
┣ibdata1
┣my.ini
┗mysql_error.log

## コマンド実行
パネルの「Shell」ボタンでコマンドプロンプトを開いて以下のコマンドを実行
```console
# mysqld --console --skip-grant-tables --skip-external-locking
2023-03-25  9:53:45 0 [Note] Using unique option prefix 'key_buffer' is error-prone and can break in the future. Please use the full name 'key_buffer_size' instead.
2023-03-25  9:53:45 0 [Note] mysqld (mysqld 10.4.25-MariaDB) starting as process 5008 ...
2023-03-25  9:53:45 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2023-03-25  9:53:45 0 [Note] InnoDB: Uses event mutexes
2023-03-25  9:53:45 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2023-03-25  9:53:45 0 [Note] InnoDB: Number of pools: 1
2023-03-25  9:53:45 0 [Note] InnoDB: Using SSE2 crc32 instructions
2023-03-25  9:53:45 0 [Note] InnoDB: Initializing buffer pool, total size = 16M, instances = 1, chunk size = 16M
2023-03-25  9:53:45 0 [Note] InnoDB: Completed initialization of buffer pool
2023-03-25  9:53:45 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2023-03-25  9:53:45 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2023-03-25  9:53:45 0 [Note] InnoDB: Setting file 'C:\xampp\mysql\data\ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2023-03-25  9:53:45 0 [Note] InnoDB: File 'C:\xampp\mysql\data\ibtmp1' size is now 12 MB.
2023-03-25  9:53:45 0 [Note] InnoDB: Waiting for purge to start
2023-03-25  9:53:45 0 [Note] InnoDB: 10.4.25 started; log sequence number 773141; transaction id 1474
2023-03-25  9:53:45 0 [Note] InnoDB: Loading buffer pool(s) from C:\xampp\mysql\data\ib_buffer_pool
2023-03-25  9:53:45 0 [Note] Plugin 'FEEDBACK' is disabled.
2023-03-25  9:53:45 0 [Note] Server socket created on IP: '::'.
2023-03-25  9:53:45 0 [Note] Zerofilling moved table:  '.\mysql\time_zone_leap_second'
2023-03-25  9:53:45 0 [Note] Zerofilling moved table:  '.\mysql\time_zone_name'
2023-03-25  9:53:45 0 [Note] InnoDB: Buffer pool(s) load completed at 230325  9:53:45
2023-03-25  9:53:45 0 [Note] Zerofilling moved table:  '.\mysql\time_zone'
2023-03-25  9:53:45 0 [Note] Zerofilling moved table:  '.\mysql\time_zone_transition_type'
2023-03-25  9:53:45 0 [Note] Zerofilling moved table:  '.\mysql\time_zone_transition'
2023-03-25  9:53:45 0 [Note] Reading of all Master_info entries succeeded
2023-03-25  9:53:45 0 [Note] Added new Master_info '' to hash table
2023-03-25  9:53:45 0 [Note] mysqld: ready for connections.
Version: '10.4.25-MariaDB'  socket: ''  port: 3306  mariadb.org binary distribution
```
このコマンドプロンプトは開きっぱなしで。
同様にもう一つコマンドプロンプトを開いて以下のコマンドを実行
```console
# mysqlcheck -r --databases mysql --use-frm
mysql.column_stats                                 OK
mysql.columns_priv                                 OK
mysql.db
info     : Wrong CRC on datapage at 1
info     : Wrong CRC on datapage at 2
info     : Wrong CRC on datapage at 3
info     : Wrong CRC on datapage at 4
info     : Wrong CRC on datapage at 5
info     : Wrong CRC on datapage at 6
info     : Wrong CRC on datapage at 7
info     : Wrong CRC on datapage at 8
status   : OK
mysql.event                                        OK
mysql.func                                         OK
mysql.global_priv
warning  : Number of rows changed from 0 to 5
status   : OK
mysql.gtid_slave_pos
note     : The storage engine for the table doesn't support repair
mysql.help_category
warning  : Number of rows changed from 0 to 48
status   : OK
mysql.help_keyword
warning  : Number of rows changed from 0 to 37
status   : OK
mysql.help_relation
warning  : Number of rows changed from 0 to 114
status   : OK
mysql.help_topic
warning  : Number of rows changed from 0 to 752
status   : OK
mysql.index_stats                                  OK
mysql.innodb_index_stats
note     : The storage engine for the table doesn't support repair
mysql.innodb_table_stats
note     : The storage engine for the table doesn't support repair
mysql.plugin                                       OK
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.proxies_priv
info     : Wrong CRC on datapage at 1
info     : Wrong CRC on datapage at 2
status   : OK
mysql.roles_mapping                                OK
mysql.servers                                      OK
mysql.table_stats                                  OK
mysql.tables_priv                                  OK
mysql.time_zone                                    OK
mysql.time_zone_leap_second                        OK
mysql.time_zone_name                               OK
mysql.time_zone_transition                         OK
mysql.time_zone_transition_type                    OK
mysql.transaction_registry
note     : The storage engine for the table doesn't support repair

#
```
二つ目のコマンド実行後、一つ目のコマンドプロンプトが更新されます。
```console
# mysqld --console --skip-grant-tables --skip-external-locking
2023-03-25  9:53:45 0 [Note] Using unique option prefix 'key_buffer' is error-prone and can break in the future. Please use the full name 'key_buffer_size' instead.
2023-03-25  9:53:45 0 [Note] mysqld (mysqld 10.4.25-MariaDB) starting as process 5008 ...
2023-03-25  9:53:45 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2023-03-25  9:53:45 0 [Note] InnoDB: Uses event mutexes
2023-03-25  9:53:45 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2023-03-25  9:53:45 0 [Note] InnoDB: Number of pools: 1
2023-03-25  9:53:45 0 [Note] InnoDB: Using SSE2 crc32 instructions
2023-03-25  9:53:45 0 [Note] InnoDB: Initializing buffer pool, total size = 16M, instances = 1, chunk size = 16M
2023-03-25  9:53:45 0 [Note] InnoDB: Completed initialization of buffer pool
2023-03-25  9:53:45 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2023-03-25  9:53:45 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2023-03-25  9:53:45 0 [Note] InnoDB: Setting file 'C:\xampp\mysql\data\ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2023-03-25  9:53:45 0 [Note] InnoDB: File 'C:\xampp\mysql\data\ibtmp1' size is now 12 MB.
2023-03-25  9:53:45 0 [Note] InnoDB: Waiting for purge to start
2023-03-25  9:53:45 0 [Note] InnoDB: 10.4.25 started; log sequence number 773141; transaction id 1474
2023-03-25  9:53:45 0 [Note] InnoDB: Loading buffer pool(s) from C:\xampp\mysql\data\ib_buffer_pool
2023-03-25  9:53:45 0 [Note] Plugin 'FEEDBACK' is disabled.
2023-03-25  9:53:45 0 [Note] Server socket created on IP: '::'.
2023-03-25  9:53:45 0 [Note] Zerofilling moved table:  '.\mysql\time_zone_leap_second'
2023-03-25  9:53:45 0 [Note] Zerofilling moved table:  '.\mysql\time_zone_name'
2023-03-25  9:53:45 0 [Note] InnoDB: Buffer pool(s) load completed at 230325  9:53:45
2023-03-25  9:53:45 0 [Note] Zerofilling moved table:  '.\mysql\time_zone'
2023-03-25  9:53:45 0 [Note] Zerofilling moved table:  '.\mysql\time_zone_transition_type'
2023-03-25  9:53:45 0 [Note] Zerofilling moved table:  '.\mysql\time_zone_transition'
2023-03-25  9:53:45 0 [Note] Reading of all Master_info entries succeeded
2023-03-25  9:53:45 0 [Note] Added new Master_info '' to hash table
2023-03-25  9:53:45 0 [Note] mysqld: ready for connections.
Version: '10.4.25-MariaDB'  socket: ''  port: 3306  mariadb.org binary distribution
[ここから下が続き]
2023-03-25  9:54:49 8 [ERROR] mysqld: Index for table '.\mysql\db' is corrupt; try to repair it
2023-03-25  9:54:49 8 [Note] Found 5 of 0 rows when repairing '.\mysql\global_priv'
2023-03-25  9:54:49 8 [Note] Found 48 of 0 rows when repairing '.\mysql\help_category'
2023-03-25  9:54:49 8 [Note] Found 37 of 0 rows when repairing '.\mysql\help_keyword'
2023-03-25  9:54:49 8 [Note] Found 114 of 0 rows when repairing '.\mysql\help_relation'
2023-03-25  9:54:49 8 [Note] Found 752 of 0 rows when repairing '.\mysql\help_topic'
```

エラーが吐かれてますがMariaDBは起動してくれました。
![MariaDB修復_20230325.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2846175/1c7467cf-8a42-3236-22a2-bf9aad05f49d.png)
とりあえず動くようになってよかった。

# 結局原因は？
エラーログ(```mysql_error.log```)を遡ってみると
```
2023-03-22 18:38:33 14 [ERROR] Master 'Version: '10.4.25-MariaDB'  socket: ''  port: 3306  mariadb.org binary distribution
': Slave I/O: Fatal error: Invalid (empty) username when attempting to connect to the master server. Connection attempt terminated. Internal MariaDB error code: 1593

```
が一番最近のエラーだった。
```MariaDB error code: 1593```で検索してみると…

https://runebook.dev/ja/docs/mariadb/mariadb-error-codes/index

| Error Code | SQLSTATE | Error | Description |
|:-:|:-:|:-:|:-:|
| 1593  | HY000  | ER_SLAVE_FATAL_ERROR  | 致命的なエラー:%s  |

…だそうです。

「これが原因じゃない？」「こんな設定してない？」みたいなのがあればコメントいただけると助かります。