0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

コンテナ内でSQLをいじる方法

Last updated at Posted at 2025-03-29

はじめに

ポートフォリオを作成する中で、dockerのコンテナに入ってSQLをいじることが増えてきたので、調べてみました。

環境

・docker
・MySQL
・バックエンドにRailsを使用

操作方法

1、コンテナを起動させる

ターミナル
docker compose up -d

2、コンテナの中に入る

ターミナル
docker exec -it (dbのコンテナの名前) bash

※コンテナの名前は次のコードで調べられる。

ターミナル
docker compose ps

3、MySQLにログインする

ターミナル
mysql -u root -p

ここから先は目的ごとに異なります。

データベースを表示させたい時

ターミナル
show databases;

(出力例)
+--------------------+
| Database           |
+--------------------+
| exam_development   |
| exam_test          |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test-database      |
+--------------------+
7 rows in set (0.07 sec)

データベースのテーブルを見たい時

ターミナル
use (見たいデータベースの名前)
show tables;

(出力例)
+----------------------------+
| Tables_in_exam_development |
+----------------------------+
| admins                     |
| ar_internal_metadata       |
| comments                   |
| replies                    |
| schema_migrations          |
| users                      |
+----------------------------+
6 rows in set (0.01 sec)

テーブルのカラムを見たい時

ターミナル
show columns 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    |                |
+---------------+-------------+------+-----+---------+----------------+

テーブルの中のデータを取得したい時

ターミナル
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)

参考資料

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?