22時に寝ようと思って2時に寝る。

備忘録や日記を書いてます。きょうは早く寝よう。

CentOS6.8 - MySQL5.1 から MySQL5.6へバージョンアップする

環境

  • CentOS release 6.8 (Final) 64bit

今回はRails5.0のアプリ開発の際に、絵文字(utf8mb4)に対応するためにMySQL5.1からMySQL5.6へのバージョンアップの必要がありました。

既存MySQL5.1を削除する

現在インストールされているMySQLを確認する

# rpm -qa|grep mysql
mysql-libs-5.1.73-8.el6_8.x86_64
mysql-server-5.1.73-8.el6_8.x86_64
mysql-5.1.73-8.el6_8.x86_64
mysql-devel-5.1.73-8.el6_8.x86_64

これらを削除する(今回は開発環境のためバックアップは取っていませんが、必要に応じて削除前に行うようにしてください)

# yum erase mysql-libs-5.1.73-8.el6_8.x86_64 mysql-server-5.1.73-8.el6_8.x86_64  mysql-5.1.73-8.el6_8.x86_64 ysql-devel-5.1.73-8.el6_8.x86_64
...
Removed:
  mysql.x86_64 0:5.1.73-8.el6_8                                  mysql-libs.x86_64 0:5.1.73-8.el6_8                                  mysql-server.x86_64 0:5.1.73-8.el6_8

Dependency Removed:
  MySQL-python.x86_64 0:1.2.3-0.3.c1.1.el6     cronie.x86_64 0:1.4.4-15.el6_7.1     cronie-anacron.x86_64 0:1.4.4-15.el6_7.1     crontabs.noarch 0:1.10-33.el6     mysql-devel.x86_64 0:5.1.73-8.el6_8
  perl-DBD-MySQL.x86_64 0:4.013-3.el6          postfix.x86_64 2:2.6.6-6.el6_7.1

Complete!

ログを見てみるとDependency Removed(依存関係にあったものも削除)されたことがわかりますが、必要に応じて再インストール後に導入してあげてください。

MySQL5.6を導入する

MySQL :: Download MySQL Yum Repository

今回は、MySQL公式のyumリポジトリを導入します。

# yum -y install http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm

正常にリポジトリが導入できたら、さっそくインストールします。

# yum -y install mysql-community-server
# mysqld --version
mysqld  Ver 5.6.36 for Linux on x86_64 (MySQL Community Server (GPL))

導入できました。自動起動設定をして、起動してみます。

# chkconfig mysqld on
# service mysqld start
Starting mysqld:                                           [  OK  ]

MySQLに入ってみます。

# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

いけました!

余談

Column count of mysql.user is wrong. Expected 43, found 39.

対話形式で初期設定できるmysql_secure_installationを実行するとエラーが起きました。

#  mysql_secure_installation
...

Set root password? [Y/n] Y
New password:
Re-enter new password:
ERROR 1558 (HY000) at line 1: Column count of mysql.user is wrong. Expected 43, found 39. Created with MySQL 50173, now running 50636. Please use mysql_upgrade to fix this error.
root password update failed!
Cleaning up...

これに対しては、MySQLのバージョンアップの際に既存テーブルの互換性をチェックして、必要に応じて「いい感じにやってくれる」らしいmysql_upgradeを実行したら、解決しました。

# mysql_upgrade -p
Enter password:
Looking for 'mysql' as: mysql
Looking for 'mysqlcheck' as: mysqlcheck
Error: Failed while fetching Server version! Could be due to unauthorized access.
FATAL ERROR: Upgrade failed
[root@localhost ~]# mysql_upgrade -p
...
OK
#  mysql_secure_installation
...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Cleaning up...
# 

Railsでのバージョンアップに関わっての対応

そのままではマイグレーションが通りませんでした。

# rake db:migrate
rake aborted!
LoadError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory - /usr/local/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-linux/2.2.0-static/mysql2-0.4.5/mysql2/mysql2.so
/vagrant/paters_rails/config/application.rb:7:in `<top (required)>'
/vagrant/paters_rails/rakefile:4:in `require_relative'
/vagrant/paters_rails/rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)

これは、gemのmysql2を再インストールすることで、いい感じに設定し直してくれます…が、その前に必要なモジュールであるmysql-develがインストールされてないエラーが出ました。

# gem uninstall mysql2
Successfully uninstalled mysql2-0.4.5
[root@localhost paters_rails]# gem install mysql2
Fetching: mysql2-0.4.5.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    /usr/local/rbenv/versions/2.2.3/bin/ruby -r ./siteconf20170417-21958-1ruy28h.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for rb_big_cmp()... yes
checking for mysql_query() in -lmysqlclient... no
-----
mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.
-----
*** extconf.rb failed ***

extconf failed, exit code 1

Gem files will remain installed in /usr/local/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/mysql2-0.4.5 for inspection.
Results logged to /usr/local/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-linux/2.2.0-static/mysql2-0.4.5/gem_make.out

yumでインストールしてあげます。

# yum install mysql-devel
...
Installed:
  mysql-community-devel.x86_64 0:5.6.36-2.el6

Complete!

再度、gemをmysql2をインストール

# gem install mysql2
Done installing documentation for mysql2 after 1 seconds
1 gem installed

いけました。以上です。

参考