LoginSignup
2
1

More than 3 years have passed since last update.

【Docker】Docker環境内でmysqlの操作方法

Last updated at Posted at 2021-03-14

はじめに

ローカル環境だとmysql -u root -pでデータベースに触れることができますが、Docker環境にいると一工夫必要です。

この記事では、Docker環境内でmysqlの操作方法について紹介します。

環境

Docker version 20.10.0
docker-compose version 1.27.4
Docker内の環境
ruby:2.6.5
Rails:6.0.0
mysql8.0

コマンド紹介

やり方は非常に簡単で、作ったコンテナのデータベース側に入って作業することです。
なので、下記のコマンドでデータベースのコンテナに入り、いつも通りmysqlのコマンドを打てば操作できます。

コンテナのデータベースに入る

ターミナル
docker-compose exec db bash 

mysqlのコマンドでデータベースにアクセスする。

パスワードはdetabase.ymlで指定してるパスワードです。

ターミナル
mysql -u root -p
Enter password

後はmysqlのコマンドを打つだけです。

どのんなデータベースがあるか確認した時

ターミナル

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| exam_development   |
| exam_test          |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test-database      |
+--------------------+
7 rows in set (0.07 sec)

特定のデータベースのテーブルをみたい時

ターミナル
mysql> show tables from データベース名;
+----------------------------+
| Tables_in_exam_development |
+----------------------------+
| admins                     |
| ar_internal_metadata       |
| comments                   |
| replies                    |
| schema_migrations          |
| users                      |
+----------------------------+
6 rows in set (0.01 sec)

特定のテーブルのカラムがみたい時

ターミナル
mysql>SHOW COLUMNS FROM テーブル名 FROM データベース名;
+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| id            | bigint      | NO   | PRI | NULL    | auto_increment |
| text          | text        | YES  |     | NULL    |                |
| comment_id_id | bigint      | YES  | MUL | NULL    |                |
| user_id_id    | bigint      | YES  | MUL | NULL    |                |
| admin_id_id   | bigint      | YES  | MUL | NULL    |                |
| created_at    | datetime(6) | NO   |     | NULL    |                |
| updated_at    | datetime(6) | NO   |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+

データ取得を取得したい時

ターミナル
mysql> USE データベース名
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
ターミナル
mysql> SELECT * FROM テーブル名;
+----+------+---------+----------+----------------------------+----------------------------+
| id | text | user_id | admin_id | created_at                 | updated_at                 |
+----+------+---------+----------+----------------------------+----------------------------+
|  3 | ???  |       1 |     NULL | 2021-02-21 02:34:34.818467 | 2021-02-21 02:34:34.818467 |
+----+------+---------+----------+----------------------------+----------------------------+
1 row in set (0.00 sec)

データの追加がしたい時

insert into テーブル名 values (1,'test','exam','2020-1-1',NULL);
今回は4っつのカラムがあったので、それぞれのデータ型にあったデータを追加しました。

ターミナル
mysql> insert into テーブル名 values (1,'test','exam','2020-1-1',NULL);
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM テーブル名;
+----+-------+---------+---------------------+------------+
| id | title | content | created_at          | updated_at |
+----+-------+---------+---------------------+------------+
|  1 | test  | exam    | 2020-01-01 00:00:00 | NULL       |
+----+-------+---------+---------------------+------------+
1 row in set (0.00 sec)

mysqlのコマンド自体はまだまだあるので下記の参考にさせていただいた記事を参照してみてください。

参考

2
1
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
2
1