mysql ERROR 2002 (HY000): '/tmp/mysql.sock' 问题解决

时间:2022-06-26
本文章向大家介绍mysql ERROR 2002 (HY000): '/tmp/mysql.sock' 问题解决,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

mysql ERROR 2002 (HY000): '/tmp/mysql.sock' 问题解决

mysql 突然打不开了报出下面这个错:

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

一直用的好好的,突然就不能用了, 然后在网上搜了一圈,什么改配置my.cnf, 什么改软连接啊,换用5.7版本,都用了一次,结论是都不对.

我要用mysql, 所以只能硬来解决问题了。

升级以后的mysql版本是:

mysql: stable 8.0.16 (bottled) [pinned at 8.0.16]

网上讲的大部分说的都是5.7之类的版本,我看我本地根本没有网上说的文件夹

完全删除后碰到的问题

因为上面讲到的都没用,我想是不是完全删除以后就好了,然后就网上搜了怎么完全删除mysql

Remove MySQL completely

brew services stop mysql
ps -ax | grep mysql

kill mysql

brew remove mysql
brew cleanup

sudo rm /usr/local/mysql
sudo rm -rf /usr/local/var/mysql
sudo rm -rf /usr/local/mysql*
sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist


下面的在我的文件夹中完全没有

subl /etc/hostconfig` 
# Remove the line MYSQLCOM=-YES-

rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /private/var/db/receipts/*mysql*

重启

找了好几篇,这个是讲的比较全的。最重要的是这行

sudo rm -rf /usr/local/var/mysql

重新安装brew install mysql 完全就没有这个文件夹了。

使用mysql.server start, 碰到的就是这个文件夹里的东西找不到,懵掉。

Starting MySQL
./usr/local/Cellar/mysql/8.0.16/bin/mysqld_safe: line 144: /usr/local/var/mysql/jindeMacBookPro.err: Permission denied
/usr/local/Cellar/mysql/8.0.16/bin/mysqld_safe: line 144: /usr/local/var/mysql/jindeMacBookPro.err: Permission denied
/usr/local/Cellar/mysql/8.0.16/bin/mysqld_safe: line 199: /usr/local/var/mysql/jindeMacBookPro.err: Permission denied
/usr/local/Cellar/mysql/8.0.16/bin/mysqld_safe: line 144: /usr/local/var/mysql/jindeMacBookPro.err: Permission denied
 ERROR! The server quit without updating PID file (/usr/local/var/mysql/jindeMacBookPro.pid).

解决没有文件夹

没有pid文件,那么就搜这问题解决嘛

MySql server startup error 'The server quit without updating PID file '

其中看到这里

brew install mysql

unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

找了一圈完全没找mysql_install_db 这个命令。

这个命令应该是低版本mysql 初始化使用的初始化命令,反正我们没有暂时不管。

搜了半天应该是使用mysqld, 这个是新版本初始化使用的命令

mysqld --initialize-insecure

MySQL 5.7参考手册-2.10.1初始化数据目录

使用上面这个命令,那么就会帮你创建/usr/local/var/mysql目录了

解决 /tmp/mysql.sock

现在这个目录有了,那么继续运行

mysql.server start

然后回到了远点,依然报

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

绕了一圈又回到了原点,那么怎么解决问题,那么看哪里能看到报出来的详细问题呢, 就是找错误日志咯。

cd /usr/local/var/mysql

cat jindeMacBookPro.err  

错误日志就在local_computer.err, 也就是你电脑这个位置xxx.err文件

如果只想找错,也可以使用

cat jindeMacBookPro.err | grep '[ERROR]'

然后又看到了这几行错

2019-05-07T11:11:09.113601Z 0 [ERROR] [MY-010273] [Server] Could not create unix socket lock file /tmp/mysql.sock.lock.
2019-05-07T11:11:09.113613Z 0 [ERROR] [MY-010268] [Server] Unable to setup unix socket lock file.
2019-05-07T11:11:09.114096Z 0 [ERROR] [MY-010119] [Server] Aborting

不能创建这个文件,不能创建这个文件大部分原因是权限不足。

默认安装后这个文件是在/tmp目录下,那么我就给权限嘛。

chmod 777 /tmp

但是看文档下面这个权限的也可以,但是没试,有兴趣的可以试一下

chmod 750 mysql-files

好了创建以后,继续启动mysql.server start, 然后启动成功

Starting MySQL
.. SUCCESS!

启动成功,那么应该可以连接了吧,然而结果报出:

ERROR 1045 (28000): Access denied for user 'jinzhang'@'localhost' (using password: NO)

连接不成功,继续解决问题。

拒绝用户问题解决

拒绝用户, 那么就创建用户,使用brew info mysql时,看到这个

We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

启动命令

$ mysql_secure_installation

Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y  

完整打印是这个, 都选Y就好

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: root
Please set the password for root here.

New password:

Re-enter new password:
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

然后使用brew services start mysql, 使用brew 启动mysql 进行管理, 然后登陆

mysql -u root -proot

成功进入mysql

可视化软件问题

上面完了以后,就好了么,事情没有这么简单,在用可视化的时候,又报错了。

Unable to connect to host 127.0.0.1, or the request timed out.

Be sure that the address is correct and that you have the necessary privileges, or try increasing the connection timeout (currently 10 seconds).

MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/lib/plugin/caching_sha2_password.so, 2): image not found

这又是什么错呢,继续搜:

Authentication plugin 'caching_sha2_password' cannot be loaded

可以通过使用以下Alter命令更改用户来更改用户密码的加密:

ALTER USER 'username'@'ip_address' IDENTIFIED WITH mysql_native_password BY 'password';

那么我要怎么改这个命令呢,使用:

首先在命令行使用mysql命令进入mysql, 然后输入下面这个命令,账号/密码:root/root

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

然后选择可视化程序,测试成功进入。

整个排错过程记录一下,防止下次碰到同样的问题,也希望可以帮到碰到同样问题的同学

PS: 觉得不错的请点个赞吧!! (ง •̀_•́)ง