这里解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";与ERROR 1045 (28000):

这里解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";与ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using passwor:yes)问题

添加链接描述

方法一

1、亲测有效,解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";

root@iZufkfljcZ:~# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

2、查找
find / -name mysql.sock 无法解决

3、# mysql -uroot -h 127.0.0.1 -p 不出意外,这句话应该是可以执行的

4、首先,更改my.cnf文件,我的服务器中的目录为/etc/my.cnf,如果没有的话可以用find去查找第2步


接下来就是保存退出,然后确保这个目录存在,并且将这个目录的权限修改一下
# chmod 777 /var/lib/mysql
5、然后就是mysql和mysqld服务重启

这样,tmp目录下就有了my.cnf配置文件中需要的mysql.sock文件了,然后把my.cnf改回就行了,如图:

service mysqld restart

登录时会报错:ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
6、一般这个错误是由密码错误引起,解决的办法自然就是重置密码。
#vim /etc/my.cnf(注:windows下修改的是my.ini)
/mysqld(在vim编辑状态下直接输入该命令可搜索文本内容)
在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程,如下图所示:
在这里插入图片描述
保存文档并退出
7、接下来我们需要重启MySQL:
Service mysqld restart
重启之后输入mysql即可进入mysql 可直接进入mysql 不用输入密码
8、修改数据库密码:
Show databases;
Use mysql;
Show tables;
update user set password=password(“nideshengri18”) where user=“root”;
9、vim /etc/my.cnf将“skip-grant-tables”注释掉重新启动数据库 service mysqld restart
10、登录数据库:mysql -uroot -pnideshengri18 即可完成

问题二、

1、亲测有效,解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";

root@iZufkfljcZ:~# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

2、查找
find / -name mysql.sock 无法解决

3、# mysql -uroot -h 127.0.0.1 -p 不出意外,这句话应该是可以执行的

4、首先,更改my.cnf文件,我的服务器中的目录为/etc/my.cnf,如果没有的话可以用find去查找第2步
在这里插入图片描述
保存退出,然后确保这个目录存在,并且将这个目录的权限修改一下
# chmod 777 /var/lib/mysql
然后就是mysql和mysqld服务重启

service mysqld restart

6、如果重启mysqld服务的时候,重启失败了,显示如下:
root@iZufkfljcZ:~# service mysqld start
Job for mysqld.service failed because the control process exited with error code. See “systemctl status mysqld.service” and “journalctl -xe” for details.
这个时候,提示可以输入systemctl status mysqld.service去查看具体的失败原因,于是:
root@iZufkfljcZ:~# systemctl status mysqld.service(输入该命令)
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/init.d/mysqld; bad; vendor preset: enabled)
Active: failed(Result: exit-code) since 三 2017-12-20 10:38:30 CST; 45s ago
Docs: man:systemd-sysv-generator(8)
Process: 2154 ExecStart=/etc/init.d/mysqld start (code=exited, status=1/FAILURE)

12月 20 10:38:29 iZufkfljcZ systemd[1]: Starting LSB: start and stop MySQL…
12月 20 10:38:29 iZufkfljcZ mysqld[2154]: Starting MySQL
12月 20 10:38:29 iZufkfljcZ mysqld_safe[2689]:Logging to ‘/var/log/mysql/error.log’.
12月 20 10:38:29 iZufkfljcZ mysqld_safe[2693]:Directory ‘/var/run/mysqld’ for UNIX socket file don’t exists.
12月 20 10:38:30 iZufkfljcZ mysqld[2154]: . * The server quit without updating PID file (/var/run/mysqld/mysqld.pid).
12月 20 10:38:30 iZufkfljcZ systemd[1]: mysqld.service: Control process exited, code=exited status=1
12月 20 10:38:30 iZufkfljcZ systemd[1]: Failed to start LSB: start and stop MySQL.
12月 20 10:38:30 iZufkfljcZ systemd[1]: mysqld.service: Unit entered failed state.
12月 20 10:38:30 iZufkfljcZ systemd[1]: mysqld.service: Failed with result ‘exit-code’.
根据提示可知,/var/run/mysqld目录不存在,也就是说mysqld服务重启需要这个目录,那就建一个吧:

root@iZufkfljcZ:~# mkdir /var/run/mysqld 创建目录
root@iZufkfljcZ:~# chmod 777 /var/run/mysqld/ 给权限
root@iZufkfljcZ:~# service mysqld start 重启数据库服务
root@iZufkfljcZ:~#

建完目录后,重新运行mysqld服务,发现重启成功了,那么我们再来看看为什么刚才要建这个目录呢?打开这个目录看看:
root@iZufkfljcZ# ls /var/run/mysqld mysqld.pid mysqld.sock mysqld.sock.lock

发现了一个熟悉的东西,mysqld.sock,但是这个是不是我们需要的东西呢?不管他,先用这个sock文件登下mysql看看行不行:

root@iZufkfljcZ:/var/run/mysqld# mysql -uroot -p -S /var/run/mysqld/mysqld.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)

Copyright © 2000, 2017, 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>

发现好像可以了,那接下来好办了,我们把之前改的配置改回来就行了,之前的目录应该是/tmp/mysql.sock,我们可以建立一个软连接连上去就可以了,

root@iZufkfljcZ:~# ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock root@iZufkfljcZ:~# ls /tmp/ mysql.sock

这样,tmp目录下就有了my.cnf配置文件中需要的mysql.sock文件了,然后把my.cnf改回就行了,如图所示:
在这里插入图片描述
到此为止,mysql已经完全修复了,下面进行测试一下吧:
root@iZufkfljcZ:/var/run/mysqld# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)
Copyright © 2000, 2017, 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值