soogle’s diary

soogle life log

MySQLが突然落ちたときの復旧方法メモ

色々あると思いますが、今回は結構ログをみたりとえぐかったので、
その調査ログもかいておこうと思います。

問題

ps aux でMYSQLのプロセスがたってない
/etc/rc.d/init.d/mysqld start
mysql daemonがたちあがらない

ログ解析

[root@XX]# id
uid=0(root) gid=0(root) 所属グループ=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
[root@XX]# tail /var/log/mysqld.log
InnoDB: the log sequence number in the ib_logfiles!
110904 20:49:52 InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
110904 20:49:52 InnoDB: Started; log sequence number 0 43665
110904 20:49:52 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
110904 20:49:52 mysqld ended

ib_logfile0 1 をけしてみる

でも消してもだめっだった。。。

[root@XX]# ls -l
合計 20536
drwx------ 2 mysql mysql 4096 3月 26 01:43 XXX

  • rw-rw---- 1 mysql mysql 5242880 9月 11 16:43 ib_logfile0
  • rw-rw---- 1 mysql mysql 5242880 11月 8 2010 ib_logfile1
  • rw-rw---- 1 mysql mysql 10485760 2月 21 2011 XXX

drwx------ 2 mysql mysql 4096 3月 26 01:58 XXXr
drwx------ 2 mysql mysql 4096 3月 26 01:20 mysql
drwx------ 2 mysql mysql 4096 7月 9 19:11 XXX
[root@XX]# cd ib

一応、tmpにMYSQL以下のデータは全部バックアップをとっておく

tmpフォルダに保存しておく

myconfの設定を見直してみる

[root@XX]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
default-character-set = utf8
skip-character-set-client-handshake

# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysql]
default-character-set = utf8

mysqldログをみてみる

未コミットなトランザクションなどのリカバリに失敗しているらしい
二重書き込みでなんかやっちゃてるらしい

110215 4:29:19 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.0.77' socket: '/var/lib/mysql/mysql.sock' port: 3306 Source distribution

110817 11:03:33 [Note] /usr/libexec/mysqld: Normal shutdown

110817 11:12:57 mysqld started
InnoDB: The log sequence number in ibdata files does not match
InnoDB: the log sequence number in the ib_logfiles!
110817 11:13:24 InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
110817 11:13:30 InnoDB: Started; log sequence number 0 43665
110817 11:13:32 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
110817 11:13:32 mysqld ended

MYSQLをもう一度立て直す

MySQL データを初期化

うまいぐあいにシステムテーブルだけ、初期化されて、InnoDBのデータは消えなかったので、
そのままパスワードをチョコチョコ設定していったら、普通につかえちゃったという何というラッキー具合!
http://dev.mysql.com/doc/refman/5.1/ja/error-creating-innodb.html
http://www.ark-web.jp/sandbox/wiki/228.html
[root@XX]# mysql_install_db
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h XX password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
[root@XXX]# cd /usr ; /usr/bin/mysqld_safe
Starting mysqld daemon with databases from /var/lib/mysql

MYSQLを再起動

mysql -u root -p
SET PASSWORD FOR root@localhost=PASSWORD('XXXXX');

権限追加

mysql> SET PASSWORD FOR root@XXX=PASSWORD('XXXXX');
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from mysql.user;

                                                                                                      • +
user host password
                                                                                                      • +
root localhost XXXXXXXXXXXXXXXX
root XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
root 127.0.0.1 XXXXXXXXXXXXXXXX
XXXX localhost XXXXXXXXXXXXXXXX
XXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
                                                                                                      • +

5 rows in set (0.00 sec)

そのほか気づいたこと

もしかしたらやらないといけないこと
MYSQLユーザ作るか、作らないか
再構築後は、最初はクエリーキャッシュがのるまで結構時間がかかる。しょうがない。ちょっとおもい。