2
2

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 1 year has passed since last update.

MySQL で「ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)」の解決方法

Last updated at Posted at 2023-04-11

以下MySQL8.0.32を前提として話を進めます。

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

の解決方法を書きますが、参考記事では

Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

のエラーを解決する方法が載っています。私はこの参考記事の通りにやって解決できたので、(38)と(2)のどちらのエラーでも参考になると思います。

状況・解決方法

MySQLをインストールし、ログインしようとしたところ以下のようにエラーが出ました

$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)

参考記事の通りmysql.sockを作成

$ sudo touch /tmp/mysql.sock
Password:

これでもまだログインできません。

$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)

参考記事の通り以下を実行

$ sudo mysql.server restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL
. ERROR! The server quit without updating PID file (/usr/local/var/mysql/MacBook-Pro.local.pid).

参考記事と同じエラーが出ました

参考記事の通り以下を実行

$ touch /usr/local/var/mysql/MacBook-Pro.local.pid

(MacBook-Pro.localのところには$ uname -nで確認できるマシン名が入ります)

参考記事の通り以下を実行すると、同じく参考記事のように長めのエラーが出ます

$ mysql.server start
Starting MySQL
.rm: /tmp/mysql.sock: Permission denied
2023-04-10T12:53:51.6NZ mysqld_safe Fatal error: Can't remove the socket file:
/tmp/mysql.sock.
Please remove the file manually and start /usr/local/Cellar/mysql/8.0.32/bin/mysqld_safe again;
mysqld daemon not started
 ERROR! The server quit without updating PID file (/usr/local/var/mysql/MacBook-Pro.local.pid).

参考記事の通り実行、control+Zで停止します

$ ls -la /tmp/mysql.sock
-rw-r--r--  1 root  wheel  0  4 10 21:51 /tmp/mysql.sock
$ sudo rm /tmp/mysql.sock
$ /usr/local/Cellar/mysql/8.0.32/bin/mysqld_safe
2023-04-10T12:57:03.6NZ mysqld_safe Logging to '/usr/local/var/mysql/MacBook-Pro.local.err'.
2023-04-10T12:57:03.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
^Z
[1]+  Stopped                 /usr/local/Cellar/mysql/8.0.32/bin/mysqld_safe

改めてログインすると

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32 Homebrew

Copyright (c) 2000, 2023, 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>

できました!

その他参考記事

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?