叫ぶうさぎの悪ふざけ

うさぎが目印のWebエンジニアが、得たことや思ったことを言の葉に乗せて叫ぶ場所です。

学習記録:12月15日(土):【MySQL8.0アップグレード】5.7.19→5.7.24→8.0.11 アップグレード手順【5.7.24 アップグレード後編-MySQL 5.7.19の環境を新規に作ってみる】

これは 俺のインプットアウトプット記録 Advent Calendar 2018 の15日目のエントリーです。

昨日の振り返り

昨日のエントリーを出して間も無く、yoku0825さんに考察していただき、ログ( /var/log/mysqld.log )を遡った結果、エラーを起こしていたテーブルの .ibd ファイルが最初からなかったことが判明したので、ちょっと遠回りだけど以下の方法で再実行してみようと思います。

今後の方針

  • 5.7.19 の環境を作る
  • リストアする
  • .ibdファイルが揃っていることを確認する
    • 今回エラーのあったテーブルにSELECTできることも確認する
  • 5.7.24 へアップグレードする
  • 結果を確認する

んで、無事に 5.7.24 になったら、8.0.11 へさらにアップグレードしてみる計画です。

MySQL 5.7.19の環境を構築する

手順は以下を想定しています。

  • vagrantでubuntu18.04の環境を作る
  • MySQL 5.7.19 をインストール
  • データをリストア
  • MySQL 5.7.24 にアップグレード
  • エラーが出ないことを確認

昨年7月に vagrant で作った際は、リストアするデータが大きすぎて vagrant のデフォルト容量である 4GB をオーバーし、リストアできませんでした。

そこで、容量を 40GB まで増やして boxを作成し、リストアしました。が、昨年やったときはすごくめんどくさかった(イメージファイルを変換したり色々やった。正直覚えておりません…)です。しかし今はデフォルトで100Gあるのでそのままやります。

新しい vagrant box を作成

まっさらから作っていきます。Dockerでやればいいと思うかたもいらっしゃるとは思うんですが、今回の主題は

MySQL 5.7.19 から 5.7.24 にアップグレード したのち MySQL8.0.11(またはもっと最新)にアップグレード

することです。Dockerでそれをやる方法を少なくとも僕は知らないし、手軽に色々いじり倒したいので、vagrantを採用しています。

ここら辺はちゃんと理解してないんですが、主題とは逸れるので掘り下げずに進めます。

バックアップ

忘れずに以下をバックアップします。

  • Vagrantfile
  • my.cnf
  • データベース

box作成

ubuntuで作っていきます。

$ cd ~/dev/vagrant
$ mkdir mysql8.0
$ cd mysql8.0
$ vagrant init bento/ubuntu-18.04
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$ vim Vagrantfile
$ cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-18.04"

  # ポートとIPアドレスの設定
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "private_network", ip: "192.168.33.10"

  # マウントするディレクトリ
  config.vm.synced_folder "./vagrant", "/vagrant"

  # メモリ容量(6G)
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "6144"
  end
end
# マウントするディレクトリ忘れずに
$ mkdir vagrant

vagrant up でbox作成

ポートとか他のboxとぶつかってないか確認忘れずに。ぶつかると以下のエラー出ます。

ポートフォワードのエラー

ついうっかり他からコピーしてきた時のあるあるですかね。俺たちは雰囲気でvagrantを利用している。

==> default: Setting the name of the VM: mysql80_default_1544881511628_84089
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 8080 is already in use
on the host machine.

To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 80, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.

vagrant up でbox作成

$ vagrant up
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'bento/ubuntu-18.04' could not be found. Attempting to find and install...
(中略)
==> default: Mounting shared folders...
    default: /vagrant => /Users/mamy1326/dev/vagrant/mysql8.0/vagrant
$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

vagrant box の中で諸々更新

ubuntu18.04.1 のboxができました。次は諸々更新。

$ vagrant ssh
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-29-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sat Dec 15 13:56:05 UTC 2018

  System load:  0.0               Processes:           90
  Usage of /:   2.8% of 61.80GB   Users logged in:     0
  Memory usage: 3%                IP address for eth0: 10.0.2.15
  Swap usage:   0%                IP address for eth1: 192.168.33.10


0 packages can be updated.
0 updates are security updates.


$ sudo apt update -y
(中略)
$ sudo apt upgrade -y
(中略)
# 途中GUIが立ち上がり、キーボードについて聞かれるので適切に選択する、主に言語配列かな
(中略)
update-initramfs: Generating /boot/initrd.img-4.15.0-29-generic
vagrant@vagrant:~$ 

aptitude インストール

ubuntuのパッケージ管理ソフトウェアのaptitudeをインストールしておく。どうやら16.04あたりからデフォルトでインストールされないようになってるらしい。

$ sudo apt-get install aptitude
Reading package lists... Done
(中略)
update-alternatives: using /usr/bin/aptitude-curses to provide /usr/bin/aptitude (aptitude) in auto mode
Processing triggers for libc-bin (2.27-3ubuntu1) ...
vagrant@vagrant:~$ 

MySQL 5.7.24 のインストール

マイナーバージョンの指定の仕方がわからず。5.7系で最新の 5.7.24 をインストール。make installしかないのかな?

$ mysql --version
-bash: mysql: command not found

# パッケージを確認する
$ sudo aptitude search ^mysql-.*5.7
p   mysql-client-5.7                                                               - MySQL database client binaries                                                          
p   mysql-client-5.7:i386                                                          - MySQL database client binaries                                                          
p   mysql-client-core-5.7                                                          - MySQL database core client binaries                                                     
p   mysql-client-core-5.7:i386                                                     - MySQL database core client binaries                                                     
p   mysql-server-5.7                                                               - MySQL database server binaries and system database setup                                
p   mysql-server-5.7:i386                                                          - MySQL database server binaries and system database setup                                
p   mysql-server-core-5.7                                                          - MySQL database server binaries                                                          
p   mysql-server-core-5.7:i386                                                     - MySQL database server binaries                                                          
p   mysql-source-5.7                                                               - MySQL source                                                                            
p   mysql-source-5.7:i386                                                          - MySQL source                                                                            
p   mysql-testsuite-5.7                                                            - MySQL 5.7 testsuite                                                                     
p   mysql-testsuite-5.7:i386                                                       - MySQL 5.7 testsuite

# 必要な物を選んでインストール
$ sudo aptitude install -y mysql-client-5.7 mysql-client-core-5.7 mysql-server-5.7 mysql-server-core-5.7
The following NEW packages will be installed:
(中略)
Processing triggers for ureadahead (0.100.0-20) ...
                                         
vagrant@vagrant:~$ mysql --version
mysql  Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using  EditLine wrapper

マイナーバージョンアップの再現どうするよ

現時点でマイナーバージョン指定のインストール方法がわからず(あんまり指定してインストールすることないもんなあ)

マイナーバージョンアップを実体験して成功させる、のも主目的の1つなので、昨日使っていた環境に対し、以下を実行してみます。

  • データベース削除
  • リストア
  • mysql_upgrade 実行

前の環境に戻って作業

先ほどバックアップはとりましたので、順番に実行していきます。

vagrant に対する操作

一応快適に操作したいので、今作った環境は止めて、前の環境で作業します。

$ vagrant halt
(中略)
$ cd ~/dev/vagrant/mysql57
$ vagrant up
$ vagrant ssh

データベース削除&作成

リストアの前に削除&空のデータベース作成します。

$ mysql -uroot -p mysql
Enter password: 
mysql> drop database mamy1326;
Query OK, 40 rows affected (0.58 sec)
mysql> create database mamy1326;
Query OK, 1 row affected (0.00 sec)

リストア

$ mysql -uroot -p mamy1326 < mamy1326.sql
Enter password: 

時間切れ

ここで今日の学習時間オーバー。15GBくらいのリストアなので、そこそこ時間かかります。

一応問題なくテーブルはリストアされていて、くだんの大きなテーブルも順調にサイズが増えているので、待てばリストアされるでしょう。

ここはリストアを待って、引き続き日が変わってから(になると思う)作業を続けたいと思います。

感想

久しぶりにリストアとかやるんですけど、去年散々やったのでこの辺は覚えてるもんですね。

あと、今までマイナーバージョンまで指定(というか意識)してインストールしたことなかったので、やり方がわかりませんでした。

マイナーバージョンが最新のならサクッとインストールできるんですけどね(当たり前)

ここら辺、検証したいわー環境個別に作りたいわー、っていうニーズは確実にあるはずなので、方法を探したいところです。

幸い前の環境は削除してないので、そちらで作業を続け、バージョンアップについての知見を得られ、アウトプットできたら、素直にMySQL 8.0の環境を普通に作って今後の検証作業とスライド作成など進めようかな、って思っています。

ただ、yoku0825さんにいただいたご恩を返すには、バージョンアップを成功させることが必要。これだけはしっかりやっておきたいと思います。

よっし引き続きやるぞー。