MySQL クライアントをインストールする

私が管理しているシステムの一部を AWS から GCP へ移行しようと思い、RDS で動いている MySQL からダンプを取得しようとしたところ、EC2 上の Amazon Linux 2 に mysql コマンドがインストールされていなかったので、インストールしてみました。

環境

MySQL クライアント

MySQL サーバに接続するための「mysql」や、ダンプを取るための「mysqldump」などのコマンドがあります。
これらが使えないと、MySQL の管理がおぼつかない感じになってしまいます。

インストールする

yum-utils のインストール

まずは、yum-utils をインストールします。
これをインストールしておくことで、mysql-devel をインストールする際に任意のバージョンを選ぶことができます。

# yum install yum-utils

私の場合は、すでにインストール済みでした。

# yum install yum-utils
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                       | 2.4 kB  00:00:00
Package yum-utils-1.1.31-46.amzn2.0.1.noarch already installed and latest version
Nothing to do

MySQL リポジトリのインストール

以下のページから、自身の OS に対応した rpm ファイルをダウンロードし、インストールします。

私の場合は、Amazon Linux 2 を使っているので、Red Hat Enterprise Linux 7 用を選びました。

# uname -a
Linux ip-10-0-20-245.us-west-2.compute.internal 4.14.104-95.84.amzn2.x86_64 #1 SMP Sat Mar 2 00:40:20 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
# yum localinstall mysql80-community-release-el7-3.noarch.rpm

リポジトリの確認、有効化

MySQLリポジトリをインストールしたら、状態を確認してインストールしたいバージョンを有効化します。

# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64   MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source   MySQL Cluster 7.5 Community disabled
mysql-cluster-7.6-community/x86_64   MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source   MySQL Cluster 7.6 Community disabled
mysql-cluster-8.0-community/x86_64   MySQL Cluster 8.0 Community disabled
mysql-cluster-8.0-community-source   MySQL Cluster 8.0 Community disabled
mysql-connectors-community/x86_64    MySQL Connectors Community  enabled:  92+26
mysql-connectors-community-source    MySQL Connectors Community  disabled
mysql-tools-community/x86_64         MySQL Tools Community       enabled:     95
mysql-tools-community-source         MySQL Tools Community - Sou disabled
mysql-tools-preview/x86_64           MySQL Tools Preview         disabled
mysql-tools-preview-source           MySQL Tools Preview - Sourc disabled
mysql55-community/x86_64             MySQL 5.5 Community Server  disabled
mysql55-community-source             MySQL 5.5 Community Server  disabled
mysql56-community/x86_64             MySQL 5.6 Community Server  disabled
mysql56-community-source             MySQL 5.6 Community Server  disabled
mysql57-community/x86_64             MySQL 5.7 Community Server  disabled
mysql57-community-source             MySQL 5.7 Community Server  disabled
mysql80-community/x86_64             MySQL 8.0 Community Server  enabled:    129
mysql80-community-source             MySQL 8.0 Community Server  disabled

MySQL 8.0 系が有効(enabled)になっていますね。
私の RDS(MySQL) は 5.7系ですので、こちらを有効にします。

# yum-config-manager --disable mysql80-community
# yum-config-manager --enable mysql57-community
# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64   MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source   MySQL Cluster 7.5 Community disabled
mysql-cluster-7.6-community/x86_64   MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source   MySQL Cluster 7.6 Community disabled
mysql-cluster-8.0-community/x86_64   MySQL Cluster 8.0 Community disabled
mysql-cluster-8.0-community-source   MySQL Cluster 8.0 Community disabled
mysql-connectors-community/x86_64    MySQL Connectors Community  enabled:  92+26
mysql-connectors-community-source    MySQL Connectors Community  disabled
mysql-tools-community/x86_64         MySQL Tools Community       enabled:     95
mysql-tools-community-source         MySQL Tools Community - Sou disabled
mysql-tools-preview/x86_64           MySQL Tools Preview         disabled
mysql-tools-preview-source           MySQL Tools Preview - Sourc disabled
mysql55-community/x86_64             MySQL 5.5 Community Server  disabled
mysql55-community-source             MySQL 5.5 Community Server  disabled
mysql56-community/x86_64             MySQL 5.6 Community Server  disabled
mysql56-community-source             MySQL 5.6 Community Server  disabled
mysql57-community/x86_64             MySQL 5.7 Community Server  enabled:    364
mysql57-community-source             MySQL 5.7 Community Server  disabled
mysql80-community/x86_64             MySQL 8.0 Community Server  disabled
mysql80-community-source             MySQL 8.0 Community Server  disabled

8.0系が無効(disabled)になり、5.7系が有効(enabled)になりましたね。

MySQL クライアントのインストール

あとは、MySQL クライアントをインストールするだけです。
mysql-community-client」に含まれています。

# yum install mysql-community-client

試してみましょう。
オプション等を指定していないのでエラーになっていますが、コマンドは存在していますね。

# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

# mysqldump
Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help