絵文字などを扱う必要が出てきたので、文字コードをutf8からutf8mb4にしようと思ったのですが、utf8mb4を利用できるのはmysql5.5.3以降ということで、現状のバージョンでは利用できないことがわかりました。
どうせなら最新の5.7にアップデートをしようということで、作業に入ったんですが、作業中、はまったことをメモとして残します。
「CentOS 6.x に MySQL 5.7をyumで簡単にインストールする」を見るとmysqlのオフィシャルサイトのyumリポジトリを利用すると簡単ということなので、以下のコマンドを発行して、mysqlのアップデートを行いました。
rpm --import http://dev.mysql.com/doc/refman/5.7/en/checking-gpg-signature.html rpm -ihv http://dev.mysql.com/get/mysql57-community-release-el6-7.noarch.rpm yum -y update
mysqlの更新が完了したので、再起動しようとすると、起動しないという状態になりました。ログを見ると
2016-06-30T05:46:03.142526Z 0 [ERROR] Fatal error: mysql.user table is damaged. Please run mysql_upgrade. 2016-06-30T05:46:03.142702Z 0 [ERROR] Aborting
という感じで、mysql.userテーブルに障害が発生とのこと。
mysql_upgradeで修復しろということなので、コマンドを発行するんだけど、mysqlが起動していないとダメという困った状態になりました。困った。
ネットを調べた結果、mysql.userテーブルをスキップしてmysqlを起動できることがわかりました。
mysqld_safe --skip-grant-tables --user=root &
mysqlが起動したので、
mysql_upgrade
でテーブルを修復。その後、起動するも、まだ失敗。ログには以下のようなのが山盛り出てました。
2016-06-30T06:17:59.343605Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2016-06-30T06:17:59.343619Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2016-06-30T06:17:59.343630Z 0 [ERROR] InnoDB: Cannot open datafile for read-only: './affiliate/mags.ibd' OS error: 81 2016-06-30T06:36:58.743482Z 0 [ERROR] InnoDB: Failed to find tablespace for table `mysql`.`innodb_index_stats` in the cache. Attempting to load the tablespace with space id 12 2016-06-30T06:36:58.743502Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2016-06-30T06:36:58.743508Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2016-06-30T06:36:58.743511Z 0 [ERROR] InnoDB: Cannot open datafile for read-only: './mysql/innodb_index_stats.ibd' OS error: 81 2016-06-30T06:36:58.743514Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2016-06-30T06:36:58.743516Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2016-06-30T06:36:58.743519Z 0 [ERROR] InnoDB: Could not find a valid tablespace file for `mysql/innodb_index_stats`. Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting-datadict.html for how to resolve the issue. 2016-06-30T06:36:58.743605Z 0 [ERROR] InnoDB: Failed to find tablespace for table `mysql`.`innodb_table_stats` in the cache. Attempting to load the tablespace with space id 11 2016-06-30T06:36:58.743617Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2016-06-30T06:36:58.743620Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2016-06-30T06:36:58.743623Z 0 [ERROR] InnoDB: Cannot open datafile for read-only: './mysql/innodb_table_stats.ibd' OS error: 81 2016-06-30T06:36:58.743626Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2016-06-30T06:36:58.743628Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2016-06-30T06:36:58.743631Z 0 [ERROR] InnoDB: Could not find a valid tablespace file for `mysql/innodb_table_stats`. Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting-datadict.html for how to resolve the issue. 2016-06-30T06:36:58.793933Z 0 [ERROR] InnoDB: Cannot open '/var/lib/mysql/ib_buffer_pool' for reading: Permission denied 2016-06-30T06:36:58.855745Z 0 [ERROR] /usr/sbin/mysqld: Can't find file: './mysql/server_cost.frm' (errno: 13 - Permission denied)
datadirで指定している/var/lib/mysqlを確認してみると、一部のファイルやディレクトリの所有権がrootになっていて、mysqlのユーザアカウントではアクセスできないようになっていることがわかりました。
(所有権がrootになった原因は、mysql5.7に更新したことなのか、mysql_upgradeをrootで発行したことが原因なのか、あるいは他のことなのかは不明なんですが)
chown -R mysql:mysql /var/lib/mysql/
を発行して、datadirの所有権をmysqlに変更したところ、無事、mysqlを起動できるようになりました。