LoginSignup
1
2

MySQLにVScodeで接続するための備忘録

Last updated at Posted at 2023-11-19

Oracle CloudのMDSにVScodeで接続するためにやったことを忘れないように書く。
ローカルからの接続なので、Bastionを利用した踏み台経由接続を利用。

今回発生した問題

MDSのMySQLバージョンは8.0以上のためデフォルトの認証方式に「caching_sha2_password」が採用されているみたい
VScodeはこの認証方式をサポートしていないみたい(?)なので、VScodeがMDSに接続できるように対象ユーザの認証方式を「mysql_native_password」に変更する。

image.png

認証方式を変更する

まずは、MySQLクライアント経由でMDSに接続する
手元のOSがOracle Linux9なので、MySQLコミュニティから該当のパッケージをダウンロード

 $ sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm

MySQLのリポジトリが有効化されていることを確認

yum repolist enabled
repo id                                                                repo の名前
mysql-connectors-community                                             MySQL Connectors Community
mysql-tools-community                                                  MySQL Tools Community
mysql80-community                                                      MySQL 8.0 Community Server
ol9_UEKR7                                                              Oracle Linux 9 UEK Release 7 (x86_64)
ol9_addons                                                             Oracle Linux 9 Addons (x86_64)
ol9_appstream                                                          Oracle Linux 9 Application Stream Packages (x86_64)
ol9_baseos_latest                                                      Oracle Linux 9 BaseOS Latest (x86_64)
ol9_developer_EPEL                                                     Oracle Linux 9 EPEL Packages for Development (x86_64)
ol9_ksplice                                                            Ksplice for Oracle Linux 9 (x86_64)
ol9_oci_included                                                       Oracle Linux 9 OCI Included Packages (x86_64)

クライアントパッケージをインストール

$ sudo yum install mysql-community-client

MDSに接続し現在の認証方式を確認

$ mysql -u admin --host 接続先 -p

mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| admin            | %         | caching_sha2_password |
| administrator    | %         | caching_sha2_password |
| ocirpl           | %         | caching_sha2_password |
| ociadmin         | 127.0.0.1 | caching_sha2_password |
| ocidbm           | 127.0.0.1 | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+

adminユーザでVScodeから接続したいので、adminの認証方式を変更する
(本来はやってはいけない気がするけど、検証用途なのでOK)

 mysql> ALTER USER 'admin'@'%' IDENTIFIED WITH mysql_native_password BY 'パスワード';

 mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| admin            | %         | mysql_native_password |
| administrator    | %         | caching_sha2_password |
| ocirpl           | %         | caching_sha2_password |
| ociadmin         | 127.0.0.1 | caching_sha2_password |
| ocidbm           | 127.0.0.1 | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+

これで完了

ホスト情報やユーザ名やパスワードを所定の操作で入力することでVScode経由で接続できた。

image.png

接続方法の詳細とか認証方式の変更はこちらを参考にさせていただきました。
https://mebee.info/2021/12/22/post-52376/

MySQLのクライアントインストールについてはこちらを参考に。
https://kcfran.com/2022/11/17/mysql80-install-linux/

いつも忘れてしまうので、今後は忘れないように。。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2