LoginSignup
15
14

More than 3 years have passed since last update.

【Docker】MySQLに接続してDB情報を確認したい

Posted at

開始タグ

Rails学習中な自分がDB情報を見たくなってもがいた結果をメモ。
Dockerを使用しています。
mySQLに関して無装備すぎたことを実感しました。

やりたいこと

先っちょだけでいいので、データベースを見させてください・・・。

やろうとしたこと

mySQLを操作してデータベース一覧を表示すればいいんじゃない?と思い立ち試行開始。

道中

とりあえず調べてみて、まずアプリ開発環境のルートディレクトリで以下を試す。

mysql -u root -p

すると以下のエラー文を突きつけられる。

RROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

what?
色々調べてみると、そもそも起動中のMySQLコンテナへ入らなければいけなかったよう。

まずはコンテナ名を確認するために以下を実行。

docker ps

MySQLのコンテナ名を確認したら、ターミナルで以下を実行。

docker exec -it mySQLのコンテナ名 bash

そうするとroot@~となるので、ここで最初に試した以下を実行。

mysql -u root -p

パスワードを入力してMySQL接続に成功したようで以下の文が。

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.

ここまできたらあとはSQL文でデータベース一覧を確認するため、以下を実行。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| app_development    |
| app_test           |
| mysql              |
| performance_schema |
| sys                |
| test_db            |
+--------------------+
7 rows in set (0.00 sec)

無事表示できました!長かった・・・。

閉じタグ

mySQL、Docker関連をもっと理解しなければ。
知らないことが多すぎて逆に楽しくなってきました!

参考リンク

Docker で MySQL コンテナを起動しました - Qiita
【Docker入門】5分でMySQLのDB、テーブル作成 - フリーランス チャレンジ!!
Dockerのコマンドでよく使うものまとめ | 大阪市天王寺区SOHOホームページ制作 | デザインサプライ-DesignSupply.-

15
14
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
15
14