欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql服务启动却连接不上的解决方法

程序员文章站 2022-06-21 08:35:47
mysql服务启动,但是连接不上,如何解决? 登陆报错: root@localhost:~# mysql -u root -p enter passw...

mysql服务启动,但是连接不上,如何解决?

登陆报错:

  root@localhost:~# mysql -u root -p
  enter password:
  error 2002 (hy000): can't connect to local mysql server through socket '/var/lib/mysql/mysql.sock' (2)
  root@localhost:~# service mysqld restart
  shutting down mysql..                   [ ok ]
  starting mysql.                      [ ok ]
  root@localhost:~# mysql -u root -p
  enter password:
  error 2002 (hy000): can't connect to local mysql server through socket '/var/lib/mysql/mysql.sock' (2)

分析原因:

原因不详,知道了补上

解决方案一:

 root@localhost:~# find / -name mysql.sock
 /tmp/mysql.sock
 root@localhost:~# mysql -u root -p -s /tmp/mysql.sock
 enter password:
 welcome to the mysql monitor. commands end with ; or \g.
 your mysql connection id is 1
 server version: 5.6.15-log source distribution

 copyright (c) 2000, 2011, 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>

解决方案二:

 root@localhost:~# find / -name mysql.sock
 /tmp/mysql.sock
 root@localhost:~# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

# disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0

# 加上下面的这段代码即可
[mysql]
socket=/tmp/mysql.sock

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

root@localhost:~# mysql -u root -p
enter password:
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 3
server version: 5.6.15-log source distribution

copyright (c) 2000, 2011, 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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。