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?

More than 3 years have passed since last update.

HomebrewでインストールしたmysqlクライアントからDockerのMySQLサーバに接続する最短手順

Posted at

前提

$ mysql --version
mysql  Ver 8.0.28 for macos12.2 on x86_64 (Homebrew)
$ docker run --rm mysql mysql --version
mysql  Ver 8.0.29 for Linux on x86_64 (MySQL Community Server - GPL)

手順

DockerでMySQLサーバを起動する。

$ docker run --rm -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<rootのパスワード> mysql

ローカルのmysqlクライアントからTCPでサーバに接続する。

$ mysql --protocol=TCP -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
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> 

なぜ--protocol=TCPを指定するのか

mysqlはデフォルトでlocalhostに接続しようとし、localhostに接続するときはデフォルトでUnixドメインソケットを使おうとする。しかしDockerとホストでファイルシステムが分かれているので、次のエラーになる。

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

そこで、プロトコルを明示的にTCPに設定して、暗黙のうちにUnixドメインソケットが使われることを回避する。

参考

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?