状況
docker compose run db bash
を実行したところ以下のようにエラーが発生しました。
ターミナル
$ docker compose run db bash
docker compose run db bash
bash-4.4$ mysql -u root -p
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
解消法
以下の手順で実行することで解消できました。
ターミナル
$ docker compose up -d
[+] Running 3/3
⠿ Container rails-gitlint-test2-db-1 Healthy 4.6s
⠿ Container rails-gitlint-test2-api-1 Started 5.1s
⠿ Container rails-gitlint-test2-web-1 Started 5.4s
$ docker compose exec db bash
bash-4.4$ mysql -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 44
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>