1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails × Docker環境内でmysqlサーバーに接続するまでの流れ

Posted at

SQLの学習目的で、Dockerコンテナ内でSQLサーバーに接続する際に生じたエラーの原因とその解決方法を記します。

1. コンテナ内に接続 → ERROR 2002 (HY000) ~ (2) → ERROR 2002 (HY000) ~ (111)

docker-compose exec web bundle exec /bin/bash
root@b671078fa536:/myapp# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root@b671078fa536:/myapp# mkdir /var/run/mysqld
root@b671078fa536:/myapp# touch /var/run/mysqld/mysqld.sock
root@b671078fa536:/myapp# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

ERROR 2002 (HY000) ~ (2)

このエラーは、mysqlに接続する為のソケットが存在しない事に由来している(らしい)。
なので上記コードでソケットを作成している。

ERROR 2002 (HY000) ~ (111)

このエラーは、mysqlが起動していないことが原因(らしい)。

$ docker-compose ps 
Name                   Command               State                 Ports              
-------------------------------------------------------------------------------------------
myapp_chrome_1   /opt/bin/entry_point.sh          Up      0.0.0.0:4444->4444/tcp           
myapp_db_1       docker-entrypoint.sh --def ...   Up      0.0.0.0:3306->3306/tcp, 33060/tcp
myapp_web_1      entrypoint.sh bash -c rm - ...   Up      0.0.0.0:3000->3000/tcp

myapp_db_1は起動しているけどな?

ここで少し時間を有する。思考錯誤の結果、以下の記事を参考にしました。

コンテナ指定接続 → mysqlサーバーへログイン

結果として自分の場合は、コンテナを指定してログインする事でmysqlサーバーへ正常にログインできるようになりました。

$ docker exec -it myapp_db_1 bash # コンテナ指定でログイン
root@d9ca806f810a:/# mysql -u root # mysqlサーバーにログイン
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.# 正常にログイン!!
Your MySQL connection id is 8
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| myapp_development  |
| myapp_test         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

DB名を指定してログインする事で時間節約

$ docker exec -it myapp_db_1 bash
root@d9ca806f810a:/# mysql -u root -p myapp_development
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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> select works.title, SUM(footprints.counts) as total_footprint_counts from works inner join footprints where works.id = 1;
+--------------+------------------------+
| title        | total_footprint_counts |
+--------------+------------------------+
| Blade Runner |                      1 |
+--------------+------------------------+
1 row in set (0.00 sec)

やはりDocker周辺は知識不足により、難しく感じました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?