RHEL5/CentOS5 付属 MySQL 5.0 を 公式 MySQL 5.5 にバージョンアップした記録

RHEL5/CentOS5 ディストリビューション付属の MySQL 5.0 から、MySQL の公式ビルドな MySQL 5.5 に入れ替えた時の記録。

バージョンアップなのでデータは引き継がれる。はず。DB の中身によってはしっかり引き継がれるのだろうけど、僕が試したところでは、結構な期間ため込んだ zabbix のデータをもった DB はアップデート時にいくつかエラーが出ました。欠損はしてないけど動きがおかしくなりそう?心配なので dump から戻す手法をとりました。というわけで事前に dump はとっておくべきです。dump から戻す(時間がかかる)覚悟で。ぼくはこんな感じで全DBの中身を取っておきました。

# /usr/bin/mysqldump --all-databases --opt -uroot | gzip > /backup/mysql.dump.gz

まずは ディストリビューション標準の MySQL 5.0 を停止。

# service mysqld stop

MySQL 公式ビルドを何も考えずに rpm -Uvh とかやってみると。長いけどそのまま引用する

A MySQL server package (mysql-server-5.0.22-2.1.0.1) is installed.
  
The current MySQL server package is provided by a different
vendor (Red Hat, Inc.) than MySQL AB, Sun Microsystems, Inc., or Oracle and/or its affiliates.
Some files may be installed to different locations, including log
files and the service startup script in /etc/init.d/.
  
Upgrading directly from MySQL 5.0 to MySQL 5.5 may not
be safe in all cases.  A manual dump and restore using mysqldump is
recommended.  It is important to review the MySQL manual's Upgrading
section for version-specific incompatibilities.
  
A manual upgrade is required.
  
- Ensure that you have a complete, working backup of your data and my.cnf
  files
- Shut down the MySQL server cleanly
- Remove the existing MySQL packages.  Usually this command will
  list the packages you should remove:
  rpm -qa | grep -i '^mysql-'
  
  You may choose to use 'rpm --nodeps -ev ' to remove
  the package which contains the mysqlclient shared library.  The
  library will be reinstalled by the MySQL-shared-compat package.
- Install the new MySQL packages supplied by Oracle and/or its affiliates
- Ensure that the MySQL server is started
- Run the 'mysql_upgrade' program
  
This is a brief description of the upgrade process.  Important details
can be found in the MySQL manual, in the Upgrading section.

言われたとおりに操作してみます

# rpm -qa | grep '^mysql-'
mysql-5.0.22-2.1.0.1
mysql-server-5.0.22-2.1.0.1
# rpm --nodeps -ev mysql mysql-server
警告: /var/log/mysqld.log は /var/log/mysqld.log.rpmsave として保存されした。
警告: /etc/my.cnf は /etc/my.cnf.rpmsave として保存されした。

微妙な和訳「保存されした」はとりあえずスルーして進めます。ここで注意すべきことは、最初から用意されていたユーザ mysql とグループ mysql が削除されるという点です。次に公式ビルドの MySQL をインストールしますが、そのとき作成される ユーザ mysql とグループ mysql は uid と gidディストリビューション標準のものと違うので、MySQL のデータディレクトリに関しては、インストール後かつ起動前に chown しておく必要があります。それではインストール。

# rpm -Uvh MySQL-server-5.5.24-1.rhel5.i386.rpm
準備中...                ########################################### [100%]
   1:MySQL-server           ########################################### [100%]
120531 16:24:53 [Warning] You need to use --log-bin to make --binlog-format work.
120531 16:24:55 [Warning] You need to use --log-bin to make --binlog-format work.
  
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 HOSTNAME 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.
  
Please report any problems with the /usr/bin/mysqlbug script!

注意事項がたくさん出てきましたがとりあえずスルーで。以前から使っていた my.cnf の修正をしておきます。いろいろあるんでしょうけどとりあえず動けばいいやという設定だけ。

~省略~
#default-character-set=utf8 ←コメントアウト
character-set-server=utf8 ←新規追加
binlog_format=2 ←新規追加
~省略~
[mysql.server]
user=mysql
#basedir=/var/lib ←コメントアウト
basedir=/usr ←新規追加

あとディレクトリの権限を設定しておきます。

# chwon -R mysql.mysql /var/lib/mysql

ようやく起動できます。

# service mysql start
Starting MySQL.......                                      [  OK  ]

おめでとう!次はデータのチェックです

# time mysql_upgrade
Looking for 'mysql' as: mysql
Looking for 'mysqlcheck' as: mysqlcheck
Running 'mysqlcheck with default connection arguments
Running 'mysqlcheck with default connection arguments
mysql.columns_priv                                 OK
mysql.db                                           OK
mysql.event                                        OK
mysql.func                                         OK
mysql.general_log                                  OK
mysql.help_category                                OK
mysql.help_keyword                                 OK
mysql.help_relation                                OK
mysql.help_topic                                   OK
mysql.host                                         OK
mysql.ndb_binlog_index                             OK
mysql.plugin                                       OK
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.proxies_priv                                 OK
mysql.servers                                      OK
mysql.slow_log                                     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.user                                         OK
Running 'mysql_fix_privilege_tables'...
OK
  
real    0m3.014s
user    0m0.044s
sys     0m0.042s

こんな感じできれいにいけばよいのだけど…上記の例はほとんどデータが無かった場合であったりシンプルな構造であったりした場合です。ごっちゃごちゃにデータが入ってた場合、以下のようなエラーが出たりします。

zabbix.items
error    : Table rebuild required. Please do "ALTER TABLE `items` FORCE" or dump/reload to fix it!

dump して reload してネっていうんで、もうおとなしく従うわけです。事前に作っておいた dump を mysql に食わせます。

# mysql -uroot < /backup/mysql.dump

よーやくおわりました。