Ubuntuにmysqlコマンドをインストールする
RDSに接続するために使いたいのでMySQLサーバーはインストールしません。
RDSにつくったMySQLに接続するのでmysqlコマンドがほしいのです。
ponsuke-tarou.hatenablog.com
aptコマンドを使ってインストールします。
Ubuntuでは、パッケージ管理にyumではなくaptを使用するようなのでaptを使ってインストールします。
linuxfan.info
リポジトリ一覧を更新します。
注記
入手可能な最新バージョンを確実にダウンロードするために、インストールの前に apt-get インデックスファイルを更新してください。
MySQL :: MySQL 5.6 リファレンスマニュアル :: 2.5.7 ネイティブソフトウェアリポジトリから MySQL を Linux にインストールする
$ sudo apt update Hit:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic InRelease Hit:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease Hit:3 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease Reading package lists... Done Building dependency tree Reading state information... Done 29 packages can be upgraded. Run 'apt list --upgradable' to see them.
mysql-clientのパッケージを検索します。
$ apt search mysql-client Sorting... Done Full Text Search... Done default-mysql-client/bionic 1.0.4 all MySQL database client binaries (metapackage) default-mysql-client-core/bionic 1.0.4 all MySQL database core client binaries (metapackage) mysql-client/bionic-updates,bionic-security 5.7.29-0ubuntu0.18.04.1 all MySQL database client (metapackage depending on the latest version) mysql-client-5.7/bionic-updates,bionic-security 5.7.29-0ubuntu0.18.04.1 amd64 MySQL database client binaries mysql-client-core-5.7/bionic-updates,bionic-security 5.7.29-0ubuntu0.18.04.1 amd64 MySQL database core client binaries
mysql-client-coreをインストールします。
mysqlコマンドがほしいだけなのでコンパクトな「mysql-client-core-5.7」をインストールします。
参考 : mysql-clientとmysql-client-coreの違いは何ですか? - 初心者向けチュートリアル
$ sudo apt install mysql-client-core-5.7 Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libaio1 The following NEW packages will be installed: libaio1 mysql-client-core-5.7 0 upgraded, 2 newly installed, 0 to remove and 29 not upgraded. Need to get 6648 kB of archives. After this operation, 30.5 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libaio1 amd64 0.3.110-5ubuntu0.1 [6476 B] Get:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 mysql-client-core-5.7 amd64 5.7.29-0ubuntu0.18.04.1 [6642 kB] Fetched 6648 kB in 0s (46.6 MB/s) Selecting previously unselected package libaio1:amd64. (Reading database ... 85356 files and directories currently installed.) Preparing to unpack .../libaio1_0.3.110-5ubuntu0.1_amd64.deb ... Unpacking libaio1:amd64 (0.3.110-5ubuntu0.1) ... Selecting previously unselected package mysql-client-core-5.7. Preparing to unpack .../mysql-client-core-5.7_5.7.29-0ubuntu0.18.04.1_amd64.deb ... Unpacking mysql-client-core-5.7 (5.7.29-0ubuntu0.18.04.1) ... Setting up libaio1:amd64 (0.3.110-5ubuntu0.1) ... Setting up mysql-client-core-5.7 (5.7.29-0ubuntu0.18.04.1) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... Processing triggers for libc-bin (2.27-3ubuntu1) ... # mysqlコマンドがインストールできたことを確認するためにバージョンを確認します。 $ mysql --version mysql Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using EditLine wrapper
RDSのMySqlに接続してみます。
RDSのセキュリティグループを設定します。
今回はRDSとEC2が同じVPC内にある構成なので、RDSのセキュリティグループに
- EC2のプライベートIP
- EC2のセキュリティグループID(sg-xxxx)
のどちらかをMySQL用ポート(3306)に許可するように設定します。
mysqlコマンドでログインします。
$ mysql -h mysql-57.xxx.{リージョン}.rds.amazonaws.com -P 3306 -u admin -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 5.7.28-log Source distribution 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>
失敗したこと
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
- 事象 : apt updateしたら怒られた
- 原因 : 実行権限がないから
- 対応 : rootに切り替えるかsudoで実行する
# 失敗 $ apt update Reading package lists... Done E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied) E: Unable to lock directory /var/lib/apt/lists/ W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied) W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied) # 対応後 $ sudo apt update Hit:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic InRelease Hit:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease Hit:3 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease Reading package lists... Done Building dependency tree Reading state information... Done 29 packages can be upgraded. Run 'apt list --upgradable' to see them.
ERROR 2003 (HY000): Can't connect to MySQL server on
- 事象 : mysqlコマンドでRDSにログインできない。
- 原因 : RDSにEC2からの接続が許可されていないから
- 対応 : RDSのセキュリティグループを設定する
$ mysql -h mysql-57.xxx.{リージョン}.rds.amazonaws.com -P 3306 -u admin -p Enter password: ERROR 2003 (HY000): Can't connect to MySQL server on 'mysql-57.xxx.{リージョン}.rds.amazonaws.com' (110)