僕なりの docker-compose の備忘録

# MySQLコンテナが日本語対応していないときに、外からデータを流し込む

```bash
docker-compose exec -u $(id -u):$(id -g) mysql mysql -u user -ppassword -e 'クエリ'
```



# run

```bash

```



# exec

```bash
docker-compose exec -u $(id -u):$(id -g) <service-name> bash
```

```bash
docker-compose exec <service-name> bash -c "cd html/app && composer install"
docker-compose exec <service-name> bash -c "cd html/app && composer update"

docker-compose exec -u $(id -u):$(id -g) <service-name> bash -c "cd html/app && composer install"
docker-compose exec -u $(id -u):$(id -g) <service-name> bash -c "cd html/app && composer update"

docker-compose exec -u $(id -u):$(id -g) db bash
mysql -u user -ppassword -h db_service_name;
show databases;
create database my_database;
show grants for user;
GRANT ALL PRIVILEGES ON `my_database`.* TO `other_user`@`%`;
```



# dockerコンテナのMySQLへログイン

```bash
docker-compose exec db bash -c 'mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE}'
```



# 文字コードの設定確認

rootユーザーで実行

```bash
mysql> show variables like '%char%';
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8mb4                        |
| character_set_connection | utf8mb4                        |
| character_set_database   | utf8mb4                        |
| character_set_filesystem | binary                         |
| character_set_results    | utf8mb4                        |
| character_set_server     | utf8mb4                        |
| character_set_system     | utf8mb3                        |
| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.01 sec)
```



# ユーザーの確認

rootユーザーで実行

```bash
mysql> select Host, User from mysql.user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| %         | user            |
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.01 sec)
```



# バージョン確認

```bash
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.36    |
+-----------+
1 row in set (0.00 sec)

mysql> status
--------------
mysql  Ver 8.0.36 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:          31
Current database:
Current user:           root@172.20.0.3
SSL:                    Cipher in use is TLS_AES_256_GCM_SHA384
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.36 MySQL Community Server - GPL
Protocol version:       10
Connection:             db via TCP/IP
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4
TCP port:               3306
Binary data as:         Hexadecimal
Uptime:                 14 min 14 sec

Threads: 2  Questions: 147  Slow queries: 0  Opens: 202  Flush tables: 3  Open tables: 121  Queries per second avg: 0.172
```