前回、DockerでMySQLコンテナーを起動しましたが、今回はDockerでMySQLコンテナー内からMySQLサーバーに接続してみます。
■環境
Windows10(DockerDesktopインストール済み)
docker version
Client: Cloud integration: v1.0.22 Version: 20.10.12 API version: 1.41 Go version: go1.16.12 Git commit: e91ed57 Built: Mon Dec 13 11:44:07 2021 OS/Arch: windows/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.12 API version: 1.41 (minimum version 1.12) Go version: go1.16.12 Git commit: 459d0df Built: Mon Dec 13 11:43:56 2021 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.4.12 GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d runc: Version: 1.0.2 GitCommit: v1.0.2-0-g52b36a2 docker-init: Version: 0.19.0 GitCommit: de40ad0
■Docker上で稼働中のコンテナーを確認する
前回、DockerでMySQLコンテナーを起動しましたが、再度Docker上で稼働中のコンテナーを確認します。確認するために、Windows10のコマンドプロンプトを起動します。
>docker ps
起動後、上記のコマンドを入力し、Enterキーを押します。「ps」コマンドを用いてコンテナーを一覧表示します。
Enterキーを押すと上記のように現在稼働中のコンテナーが一覧で表示されます。この表示された中に、前回作成したMySQLコンテナーの稼働を確認することができました。
■MySQLコンテナー内からMySQLサーバーに接続する
>docker exec -it mysql-test mysql -uroot -p
確認後、上記のコマンドを入力し、Enterキーを押します。「docker exec」コマンドで、コンテナー内でコマンドを実行します。実行するコマンドは、「-it」オプションで、mysql-testの標準入力を開き続け操作することができます。「mysql -uroot -p」オプションでMySQLシェルに接続を行います。
Enter password:
Enterキーを押すと、上記のメッセージが出力されますので、パスワードを入力します。今回は前回MySQLイメージを使用しコンテナーを実行する際にパスワードの設定も行いましたので、そのパスワードを入力します。
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 97 Server version: 8.0.29 MySQL Community Server - GPL Copyright (c) 2000, 2022, Oracle and/or its affiliates. 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>
Enterキーを押すと、パスワードの照合が行われ、完了すると、MySQLシェルに接続されます。これでMySQLサーバーへの接続は完了となります。