Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

今天在ubuntu上连接mysql数据库时,遇到一个小问题

Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘ (2)

第一次使用时还可以使用的,突然就不好了,不知道是不是没吃药,

先查看数据库状态:

[email protected]:~$ sudo /etc/init.d/mysql status

(因为我使用的是普通用户,所以要sudo一下)

1.如果出现:

[email protected]:~$ sudo /etc/init.d/mysql status

* /usr/bin/mysqladmin  Ver 8.42 Distrib 5.5.41, for debian-linux-gnu on x86_64

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

Server version        5.5.41-0ubuntu0.14.04.1

Protocol version    10

Connection        Localhost via UNIX socket

UNIX socket        /var/run/mysqld/mysqld.sock

Uptime:            8 min 2 sec

Threads: 1  Questions: 110  Slow queries: 0  Opens: 291  Flush tables: 1  Open tables: 4  Queries per second avg: 0.228

等相关数据库配置信息,表示数据库是start状态,是可用可连接的。

2.如果出现:

* MySQL is stopped.

表示数据库是stop状态,是不可用不可连接的。

也可以这样>

在连接本地数据库,很傻瓜的做法就是在命令行键入mysql,

1.如果出现:

ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘ (2)那么连接不上数据库的

2.如果出现:

ERROR 2002 (HY000): Access denied for user ‘jason‘@‘localhost‘ (using password: NO) 那么表示可以连接的

使用[email protected]:~$mysql -uroot -p 也会出现

ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘ (2)

不管怎样查看,最终问题都是指向

ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘ (2)

网上查了下,数据库不在启动状态,应该是权限的问题(因为我成功启动过,然会突然不行了,其中也有动过my.cnf文件),

1.首先键入” ps -ef | grep mysql“

jason@jason-Aspire-EC-471G:~#
ps -ef|grep mysql

root     21721  1958  0 14:14 pts/12   00:00:00 /bin/sh /usr/bin/mysqld_safe

mysql    22132 21721  0 14:14 pts/12   00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/lib/mysql/jason-Aspire-EC-471G.err --pid-file=/var/lib/mysql/jason-Aspire-EC-471G.pid --socket=/var/run/mysqld/mysqld.sock
--port=3306

root     24309 21554  0 14:29 pts/12   00:00:00 grep --color=auto mysql

可以查到mysql的进程,ok,kill掉mysql的进程

[email protected]:~$ kill  22132

2.然后把数据库重新启动,但每次都是【fail】,因为我使用的是普通用户"jason",然后"sudo -s"切换到管理员root,启动数据库,如下

[email protected]:~$ sudo -s

[sudo] password for jason:

[email protected]:~# /etc/init.d/mysql restart

* Stopping MySQL database server mysqld                                                                                                                    [ OK ]

* Starting MySQL database server mysqld                                                                                                                      [ OK ]

* Checking for tables which need an upgrade, are corrupt or were

not closed cleanly.

启动完成了

用管理员也可以连接数据库。数据库启动成功了,那么切换到普通用户吧"su jason" ,再次键入

[email protected]:~$ sudo /etc/init.d/mysql status查看数据库状态,则会出现数据库相关配置信息

这时普通用户也可重启数据库。可以进行备份,恢复等相关操作。

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

时间: 2024-10-13 01:18:46

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)的相关文章

Can't connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘ (2)解决思路

首先说明一下mysql.sock文件的作用: 连接mysql有两种方式,第一种是TCP/IP,第二种就是直接使用unix domain socket,它比TCP/IP块. mysql.sock是在mysql-server和client在同一服务器上时,发起本地连接时可用,而无需定义-h参数指定具体的IP.mysql.sock是随每次mysql server启动时生成,通常配置参数是将mysql.sock生成在/tmp/目录下.即在/etc/my.cnf文件中指定socket=/tmp/mysql

(MYSQL)ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 方案1. 1.#ps -A|grep mysql 显示类似: 1829 ?        00:00:00 mysqld_safe   1876 ?        00:00:31 mysqld 2.#kill -9 1829 3.#kill -9 1876 4.#/etc/init.d/

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

今天mysql不知道犯啥病,root登录的时候一直报这个错误. 在国内网站找了找,没什么合适的 答案,在stackoverflow和ubuntu的一个社区上找到了答案,搞定了,但是没太看懂,记录一下,以后再翻. That said, Here is what I did/ found out: 1. You don't have to change the tmp directory in the my.cnf file. Even adding a second directory, chan

mysql start error ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysql.sock’ (2)

When I type mysql, it shows “ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysql.sock’ (2)”. Then I try to start mysql service: $ service mysql start But then I get error “ No directory, logging in with HOME=

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'解决

安装上mysql后,报 Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock',试了网上的方法都没有用,于是打开/etc/my.cnf查看. 看到这么一段: # The MySQL server 26 [mysqld] 27 port            = 3306 28 socket          = /tmp/mysql.sock 29 skip-external-locking

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

First make a backup of your /var/lib/mysql/ directory just to be safe. sudo mkdir /home/<your username>/mysql/ cd /var/lib/mysql/ sudo cp * /home/<your username>/mysql/ -R Next purge MySQL (this will remove php5-mysql and phpmyadmin as well as

Can&#39;t connect to local MySQL server through socket &#39;/var/run/mysqld/mysqld.sock&#39;的解决

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'的解决 原因是目录 的访问权限问题,ubuntu下用下面的命令(其他发行版用管理员权限,把sudo去掉) sudo chown -R mysql:mysql /var/run/mysqld 然后启动服务器 sudo /etc/init.d/mysql start Can't connect to local MySQL server th

mysql之ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/var/run/mysqld/mysqld.sock&#39;解决方法

LAMPP安装完成之后,mysql -u root -p连不上,报这个错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' 在mysql的配置文件里,/opt/lampp/etc/my.cnf, 发现了这样的配置: [java] <SPAN style="FONT-SIZE: 24px"># The following

OperationalError: (2002, “Can&#39;t connect to local MySQL server through socket &#39;/var/run/mysqld/mysqld.sock&#39; (2)”)

OperationalError: (2002, “Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)”) python MySQLdb这个模块是独立的,有自己独立的socket,解决的方法就是找出mysql配置里的socket的地址(比如socket=/tmp/mysql.sock),然后做个软连接 ln -s /tmp/mysql.sock  /var/run/mysqld/