mysqlコマンドをインストールする方法


# やりたいこと : mysqlコマンドだけインストールしたい
以前、[RDSに接続したくてUbuntuにmysqlコマンドだけインストールしたことがある。](https://ponsuke-tarou.hatenablog.com/entry/2020/02/27/214335)

Dockerで作成したMySQLのコンテナや、AWSのRDSに接速するために**mysqlコマンドだけ**ほしい。


# macOSにHomebrewでインストールする方法

- 環境 : macOS Monterey バージョン12.5.1
- 参考 : [Macのhomebrewでmysqlクライアントのみインストールする - Qiita](https://qiita.com/fault/items/b854d90ae6eef5aa3417)

```bash
# アップデートしておく
$ brew update
Already up-to-date.
$ brew -v
Homebrew 3.5.10
Homebrew/homebrew-core (git revision c1419f1771a; last commit 2022-08-26)
Homebrew/homebrew-cask (git revision 44ae9dcccb; last commit 2022-08-26)

# Homebrewが元気かお医者さんに聞いておく
$ brew doctor
Your system is ready to brew.

# インストールしちゃってないか確認する
$ brew list | grep mysql
$

# インストールするものを選ぶ
$ brew search mysql-client
==> Formulae
mysql-client             mysql-client@5.7

==> Casks
mysql-shell

# mysqlクライアントをインストールする
$ brew install mysql-client
==> Downloading https://ghcr.io/v2/homebrew/core/libevent/manifests/2.1.12
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libevent/blobs/sha256:b08b593fd2ac8480d59038ca46892844a526af8ddaef9788aed93c6aab63cd05
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:b08b593fd2ac8480d59038ca46892844a526af8ddaef9788aed
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libcbor/manifests/0.9.0
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libcbor/blobs/sha256:d07e3853a1d2d1cabe8ac3f4c005a3d90226f9f5faa8d174f61c76b121a351cc
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:d07e3853a1d2d1cabe8ac3f4c005a3d90226f9f5faa8d174f61
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libfido2/manifests/1.11.0
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libfido2/blobs/sha256:aeb657a45d240d679bebf88fd5d8fe9253464fe7aa986a34e6531c1c531baf25
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:aeb657a45d240d679bebf88fd5d8fe9253464fe7aa986a34e65
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/zlib/manifests/1.2.12
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/zlib/blobs/sha256:5072d7b94690a52220f7a9f6cc566f87998a380e3f2fcd8a386caf7dbd5f19c4
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:5072d7b94690a52220f7a9f6cc566f87998a380e3f2fcd8a386
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/lz4/manifests/1.9.4
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/lz4/blobs/sha256:88b369cea90a0a119c24aa96a614fe7d77de58d18cb1803023dc925679eb905f
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:88b369cea90a0a119c24aa96a614fe7d77de58d18cb1803023d
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/zstd/manifests/1.5.2-3
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/zstd/blobs/sha256:b0eabfa556c5aed039a5b22cd7e2e3dd52c7d2416c1141e4a8e9e825b9238fc3
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:b0eabfa556c5aed039a5b22cd7e2e3dd52c7d2416c1141e4a8e
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/mysql-client/manifests/8.0.30
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/mysql-client/blobs/sha256:a8b71724eb655a359756740ca25c755efd927700396935ca641c89de38221
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:a8b71724eb655a359756740ca25c755efd927700396935ca641
######################################################################## 100.0%
==> Installing dependencies for mysql-client: libevent, libcbor, libfido2, zlib, lz4 and zstd
==> Installing mysql-client dependency: libevent
==> Pouring libevent--2.1.12.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/libevent/2.1.12: 57 files, 2.0MB
==> Installing mysql-client dependency: libcbor
==> Pouring libcbor--0.9.0.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/libcbor/0.9.0: 31 files, 161.7KB
==> Installing mysql-client dependency: libfido2
==> Pouring libfido2--1.11.0.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/libfido2/1.11.0: 512 files, 1MB
==> Installing mysql-client dependency: zlib
==> Pouring zlib--1.2.12.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/zlib/1.2.12: 12 files, 407KB
==> Installing mysql-client dependency: lz4
==> Pouring lz4--1.9.4.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/lz4/1.9.4: 22 files, 685.2KB
==> Installing mysql-client dependency: zstd
==> Pouring zstd--1.5.2.monterey.bottle.3.tar.gz
🍺  /usr/local/Cellar/zstd/1.5.2: 31 files, 2.4MB
==> Installing mysql-client
==> Pouring mysql-client--8.0.30.monterey.bottle.tar.gz
==> Caveats
mysql-client is keg-only, which means it was not symlinked into /usr/local,
because it conflicts with mysql (which contains client libraries).

If you need to have mysql-client first in your PATH, run:
  echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql-client you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql-client/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql-client/include"

==> Summary
🍺  /usr/local/Cellar/mysql-client/8.0.30: 129 files, 148.8MB
==> Running `brew cleanup mysql-client`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> mysql-client
mysql-client is keg-only, which means it was not symlinked into /usr/local,
because it conflicts with mysql (which contains client libraries).

If you need to have mysql-client first in your PATH, run:
  echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql-client you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql-client/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql-client/include"


# PATHを通す
$ cat ~/.zshrc
# ...省略...
export MYSQL_CLIENT="/usr/local/opt/mysql-client/bin"
export PATH=${MYSQL_CLIENT}:${JAVA_HOME}:${PATH}:${GLASSFISH_HOME}/bin
# ...省略...
$ source ~/.zshrc


# バージョンを確認してみる
$ mysql --version
mysql  Ver 8.0.30 for macos12.4 on x86_64 (Homebrew)

```


# CentOSにMySQLリポジトリからインストールする方法
- 環境
    - CentOS Linux release 7.8.2003 (Core)
    - 接続先 : MySQL8.0.20

1. MySQLリポジトリをインストールする
    - 参考 : [MySQL :: Download MySQL Yum Repository](https://dev.mysql.com/downloads/repo/yum/)
1. ほしいバージョンが有効になっていることを確認する
    1. 参考 : [yum｜yum リポジトリの有効／無効](https://kazmax.zpp.jp/linux_beginner/yum_repository_enable_disable.html)
    1. ほしいバージョンが無効になっていたら`yum-config-manager`コマンドで有効化する
        - 参考 : [yum-config-manager でリポジトリの有効/無効を切り替える - らくがきちょう](https://sig9.hatenablog.com/entry/2016/11/20/120000)
1. インストールする
    1. `mysql`コマンドは`mysql-community-client`に含まれている
        - 参考 : [[MySQL][5.7]MySQL クライアントをインストールする](http://tech-wiki.pomme-verte.net/?p=793)
    1. `mysql-community-client`をインストールする
        - 参考 : [centos 7にmysql clientを入れる - S氏はたまにblogを更新してます](https://blog.suganoo.net/entry/2018/10/17/122339)
1. 使ってみる


```bash
# MySQLリポジトリをインストールする
$ sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
[sudo] password for ponsuke:
Retrieving https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
warning: /var/tmp/rpm-tmp.P6p8HL: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql80-community-release-el7-1  ################################# [100%]


# 欲しいバージョンが有効になっていることを確認する
$ sudo yum repolist enabled | grep mysql
Repository google-chrome is listed more than once in the configuration
mysql-connectors-community/x86_64 MySQL Connectors Community                 153
mysql-tools-community/x86_64      MySQL Tools Community                      110
mysql80-community/x86_64          MySQL 8.0 Community Server                 177


# mysqlコマンドはmysql-community-clientに含まれている
$ sudo yum search mysql-community-client
Loaded plugins: fastestmirror, ovl
Repository google-chrome is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * epel: d2lzkl7pfhq30w.cloudfront.net
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
============================================================================ N/S matched: mysql-community-client =============================================================================
mysql-community-client.i686 : MySQL database client applications and tools
mysql-community-client.x86_64 : MySQL database client applications and tools

  Name and summary matches only, use "search all" for everything.


# mysql-community-clientをインストールする
$ sudo yum install -y mysql-community-client.x86_64
Loaded plugins: fastestmirror, ovl
Repository google-chrome is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * epel: d2lzkl7pfhq30w.cloudfront.net
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-client.x86_64 0:8.0.20-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.20-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:8.0.20-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) >= 8.0.11 for package: mysql-community-libs-8.0.20-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-common.x86_64 0:8.0.20-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================
 Package                                              Arch                                 Version                                      Repository                                       Size
==============================================================================================================================================================================================
Installing:
 mysql-community-client                               x86_64                               8.0.20-1.el7                                 mysql80-community                                47 M
Installing for dependencies:
 mysql-community-common                               x86_64                               8.0.20-1.el7                                 mysql80-community                               609 k
 mysql-community-libs                                 x86_64                               8.0.20-1.el7                                 mysql80-community                               4.5 M

Transaction Summary
==============================================================================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 52 M
Installed size: 256 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-common-8.0.20-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-8.0.20-1.el7.x86_64.rpm is not installed
(1/3): mysql-community-common-8.0.20-1.el7.x86_64.rpm                                                                                                                  | 609 kB  00:00:00
(2/3): mysql-community-libs-8.0.20-1.el7.x86_64.rpm                                                                                                                    | 4.5 MB  00:00:00
(3/3): mysql-community-client-8.0.20-1.el7.x86_64.rpm                                                                                                                  |  47 MB  00:00:00
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                         138 MB/s |  52 MB  00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql80-community-release-el7-1.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : mysql-community-common-8.0.20-1.el7.x86_64                                                                                                                                 1/3
  Installing : mysql-community-libs-8.0.20-1.el7.x86_64                                                                                                                                   2/3
  Installing : mysql-community-client-8.0.20-1.el7.x86_64                                                                                                                                 3/3
  Verifying  : mysql-community-client-8.0.20-1.el7.x86_64                                                                                                                                 1/3
  Verifying  : mysql-community-libs-8.0.20-1.el7.x86_64                                                                                                                                   2/3
  Verifying  : mysql-community-common-8.0.20-1.el7.x86_64                                                                                                                                 3/3

Installed:
  mysql-community-client.x86_64 0:8.0.20-1.el7

Dependency Installed:
  mysql-community-common.x86_64 0:8.0.20-1.el7                                                   mysql-community-libs.x86_64 0:8.0.20-1.el7

Complete!


# アプリケーション用コンテナへ接続してみる
$ mysql -h {MySQLのコンテナ名} -u root -D {データベース名} -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> 
```