LoginSignup
26
15

More than 3 years have passed since last update.

DockerのMySQL接続でテーブル確認する際、ERROR 1046 (3D000): No database selectedが出た時の対処法

Posted at

DockerのMySQLと接続してテーブル設計がどうなってるか確認しようとしたところ、
ERROR 1046 (3D000): No database selectedというエラーが出てしまったので、
対処法をまとめました。

MySQLと接続してテーブル設計確認を試みると..

dockerコンテナの名前を確認

terminal.
docker ps

で出てきたDBのコンテナ名(ここでいうとproject_file_db_1です)をコピペして、

ONTAINER ID   IMAGE              COMMAND                  CREATED        STATUS        PORTS                               NAMES
dcba6a097806   project_file_web   "entrypoint.sh bash …"   37 hours ago   Up 25 hours   0.0.0.0:3000->3000/tcp              project_file_web_1
cf035576b002   mysql:8.0.20       "docker-entrypoint.s…"   37 hours ago   Up 25 hours   33060/tcp, 0.0.0.0:3307->3306/tcp   project_file_db_1

以下コマンドを実行。(今回だとdocker exec -it project_file_db_1 bashになります)

terminal.
docker exec -it DBコンテナ名 bash
root@cf035576b002:/#root@cf035576b002:/#

するとroot@cf035576b002:/#というのが出てくるので、mysql -u root -pを打ってEnter。
docker-compose.ymlで設定したパスワードを入力してEnterを押すと、MySQLと接続ができます。

 % docker exec -it project_file_db_1 bash
root@cf035576b002:/# mysql -u root -p
Enter password: ここにMySQLのパスワードを入力してください

welcomeされました。

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

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> ここからMySQLにゴニョゴニョしていきます

show tables;でDBにあるテーブルを確認しようとしました。

mysql> show tables; #これでテーブルが見れるはずなのですが。
ERROR 1046 (3D000): No database selected #あれ..??
mysql> select * from users;
ERROR 1046 (3D000): No database selected #あれ..??

エラー出現! ERROR 1046 (3D000): No database selected

とりあえずググって出てきたこの記事を参考に、対処します。

エラー解決方法STEP1. use ;

データベース名画わからない場合は、SHOW databases;で一覧が出ます。

mysql> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| app_development    |
| app_test           |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

use app_developmentを実行。

mysql> use app_development
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

okぽい。
show tables;します。

mysql> show tables;
+---------------------------+
| Tables_in_app_development |
+---------------------------+
| ar_internal_metadata      |
| letters                   |
| schema_migrations         |
| users                     |
+---------------------------+
4 rows in set (0.01 sec)

見られる様になりました!

26
15
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
26
15