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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hjf161105/article/details/78850658

  最近租了一个阿里云云翼服务器,趁着自己还是一个学生,享受一下优惠。我租的是阿里云Ubuntu16.04版本的服务器,在搭建mysql的时候,一开始是可以运行的,由于某次手抽,把mysql的套接字文件不小心删除了,然后无论怎么卸载重装,都出现了这个问题:

root@iZufkfljcZ:~# mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
  网上找了好久,搜到的全部是重复的文章而且也没讲明为什么出现这个问题,找了一天才找到一篇讲解mysql.sock文件作用的文章,然后分析一下为什么出现这个问题,现在讲之总结一下,以备后人查阅。

  我出现的问题是:找不到mysql.sock,如果你可以运行

find / -name mysql.sock
这条命令,并且能查到结果的话,只需将查到的结果做一个软连接到/tmp目录下即可解决(网上都是这么解决的)。

但是,我执行了这条语句之后,并没有任何反应,没有找到mysql.sock文件。

在这之前,需要明白mysql.sock这个文件有什么用?

连接localhost通常通过一个Unix域套接字文件进行,一般是/tmp/mysql.sock。如果套接字文件被删除了,本地客户就不能连接。这可能发生在你的系统运行一个cron任务删除了/tmp下的临时文件。

如果你因为丢失套接字文件而不能连接,你可以简单地通过重启服务器重新创建得到它。因为服务器在启动时重新创建它。

如果和我一样,重启服务器还是没有任何变化,你可以先执行下面的语句:

# mysql -uroot -h 127.0.0.1 -p 
不出意外,这句话应该是可以执行的,你现在不能用套接字建立连接因为它不见了,所以可以建立一个TCP/IP连接
如果套接字文件被一个cron任务删除,问题将重复出现,除非你修改cron任务或使用一个或使用一个不同的套接字文件,我的解决办法是重新指定一个不同的套接字,或者说,我现在没有mysql.sock文件,所以我要想办法生成一个。

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



接下来就是保存退出,然后确保这个目录存在,并且将这个目录的权限修改一下

# chmod 777 /var/lib/mysql
准备步骤做好,然后就是mysql和mysqld服务重启

# service mysql  restart
# service mysqld restart
我在重启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 "journalc
tl -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 (c) 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 (c) 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> 
  OK,搞定收工,用了快一天时间在解决这个问题上,感觉收获还是挺多的,Linux的学习任重而道远啊!

展开阅读全文

?mysql 的phpMyAdmin不可用,报错Can't connect to local MySQL server through socket '/tmp/mysql.sock'(2)

12-08

我使用的是apache+mysql+php, 想使用phpMyAdmin管理mysql,无奈不能成功,详情如下:rnrn安装步骤: rn1.安装mysqlrn $rpm -ivh mysql-3.23.56-1.73.i386.rpmrn $rpm -ivh mysqlclient9-3.23.22-8.i386.rpmrn $rpm -ivh mysql-devel-3.23.56-1.73.i386.rpmrn $rpm -ivh mysql-server-3.23.56-1.73.i386.rpmrn2.解压缩APACHE: rn shell>tar -xzvf apache_1.3.28.tar.gzrn shell> cd apache_1.3.28rn shell>patch -p0 < ../no_zombies.patch rnrn3.apache第一次编译rn 并不要安装,因为php的编译需要apache至少已经编译过一次 rn cd apache_1.3.28 rn ./configure --prefix=/var/www rn 解压缩PHP:rn shell> tar -xzvf php-4.1.2.tar.gz rn shell> cd php-4.1.2 rn 配置编译安装PHP:rn shell>./configure --with-mysql --with-apache=../apache_1.3.28 --enable-track-vars --with-xml --with-apx rn 配置PHP支持MYSQL,作为APACHE功能模块运行rn rn shell>makern shell>make installrn4.配置APACHE:rn shell>cd /root/apache_1.3.28rn shell> ./configure --prefix=/var/www/ --activate-module=src/modules/php4/libphp4.a rn 将PHP作为APACHE的一个模块静态编译rn shell> make rn shell> make install rnrn这样安装好了后,mysql命令行可用,apache可用,php可用,但是我将phpMyAdmin拷入主目录下,在别的机器的IE上键入: http://192.168.0.1/phpMyAdmin/后,报错:rn报错Can't connect to local MySQL server through socket '/tmp/mysql.sock'(2) rnrn注:mysql,phpMyAdmin配置应该无误.rn我自己怀疑是不是编译的时候,没把php支持mysql编译进去.因为:我后来编了一个测试页:rn rn结果显示项中:rnconfigure command : './configure' '--with-apache=../apache_1.3.28' '--enable-track- vars' '--with-xml' '--with-apx'rn好象与我编译时用的命令:>./configure --with-mysql --with-apache=../apache_1.3.28 --enable-track-vars --with-xml --with-apx 有出入.rn但由于我的mysql是rpm安装的,如果我用:>./configure --with-mysql=/usr/local/mysql --with-apache=../apache_1.3.28 --enable-track-vars --with-xml --with-apx rn编译又通不过,说是头文件没有.rn请问高手,此问题该怎么解决,那些头文件又在哪里呢?rn 论坛

mysql启动问题,ERROR 2002 (HY000): Can't connect to local MySQL server through socket

10-01

由于mysql的连接数过多,重启mysql服务后,导致的问题。。。rn如下所示:rn[root@radius ~]# mysqlrnERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)rn[root@radius ~]# /etc/rc.d/init.d/mysqld statusrnmysqld (pid 3789) 正在运行...rnrn---rn从网上找了很多资料都解决不了问题rnmysql_install_db---不起作用rnmysqld_safe ---同样不起作用rn权限设置也试过了不管用rnrn------【启动守护进程时报错】-----rn[root@radius mysqld]# service mysqld startrn初始化 MySQL 数据库: Installing MySQL system tables...rnOKrnFilling help tables...rnOKrnrnTo start mysqld at boot time you have to copyrnsupport-files/mysql.server to the right place for your systemrnrnPLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !rnTo do so, start the server, then issue the following commands:rn/usr/bin/mysqladmin -u root password 'new-password'rn/usr/bin/mysqladmin -u root -h radius password 'new-password'rnSee the manual for more instructions.rnYou can start the MySQL daemon with:rncd /usr ; /usr/bin/mysqld_safe &rnrnYou can test the MySQL daemon with mysql-test-run.plrncd mysql-test ; perl mysql-test-run.plrnrnPlease report any problems with the /usr/bin/mysqlbug script!rnrnThe latest information about MySQL is available on the web atrnhttp://www.mysql.comrnSupport MySQL by buying support/licenses at http://shop.mysql.comrn [确定]rnTimeout error occurred trying to start MySQL Daemon.rn启动 MySQL: [失败]rnrn---------【错误日志】------rnInnoDB: The first specified data file ./ibdata1 did not exist:rnInnoDB: a new database to be created!rn111001 12:49:05 InnoDB: Setting file ./ibdata1 size to 10 MBrnInnoDB: Database physically writes the file full: wait...rn111001 12:49:05 InnoDB: Error: Write to file ./ibdata1 failed at offset 0 0.rnInnoDB: 1048576 bytes should have been written, only -1 were written.rnInnoDB: Operating system error number 28.rnInnoDB: Check that your OS and file system support files of this size.rnInnoDB: Check also that the disk is not full or a disk quota exceeded.rnInnoDB: Error number 28 means 'No space left on device'.rnInnoDB: Some operating system error numbers are described atrnInnoDB: http://dev.mysql.com/doc/refman/5.0/en/operating-system-error-codes.htmlrnInnoDB: Error in creating ./ibdata1: probably out of disk spacernInnoDB: Could not open or create data files.rnInnoDB: If you tried to add new data files, and it failed here,rnInnoDB: you should now edit innodb_data_file_path in my.cnf backrnInnoDB: to what it was, and remove the new ibdata files InnoDB createdrnInnoDB: in this failed attempt. InnoDB only wrote those files full ofrnInnoDB: zeros, but did not yet use them in any way. But be careful: do notrnInnoDB: remove old data files which contain your precious data!rn^G/usr/libexec/mysqld: Disk is full writing './mysql-bin.000004' (Errcode: 28). Waiting for someone to free space... Retry in 60 secsrn^G/usr/libexec/mysqld: Disk is full writing './mysql-bin.000003' (Errcode: 28). Waiting for someone to free space... Retry in 60 secsrn111001 12:54:45 mysqld startedrnInnoDB: Error: auto-extending data file ./ibdata1 is of a different sizernInnoDB: 0 pages (rounded down to MB) than specified in the .cnf file:rnInnoDB: initial 640 pages, max 0 (relevant if non-zero) pages!rnInnoDB: Could not open or create data files.rnInnoDB: If you tried to add new data files, and it failed here,rnInnoDB: you should now edit innodb_data_file_path in my.cnf backrnInnoDB: to what it was, and remove the new ibdata files InnoDB createdrnInnoDB: in this failed attempt. InnoDB only wrote those files full ofrnInnoDB: zeros, but did not yet use them in any way. But be careful: do notrnInnoDB: remove old data files which contain your precious data!rn^G/usr/libexec/mysqld: Disk is full writing './mysql-bin.000005' (Errcode: 28). Waiting for someone to free space... Retry in 60 secsrn^G/usr/libexec/mysqld: Disk is full writing './mysql-bin.000005' (Errcode: 28). Waiting for someone to free space... Retry in 60 secsrnrnrn----------------------------rn很急。。。。rnrn望各位大虾鼎力相助。。。rnrn 论坛

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

11-27

公司用的是一个国外的服务器,这几天把写好的php网站挂上去发现报错:rnSQL statement execution error!,select * from t_userrnMysql error description: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)rnMysql error number: 2002rnDate: 2009-11-23 @ 03:18 rn我的代码如下:rntest.php:rnquery($sql);rnrnwhile($row = $DB->fetch_array($query))rn echo $row['username'].'-'.$row['password'];rnrn?>rnrnrnclass.mysql.php:rnconn = @ mysql_connect($dbhost, $dbusername, $dbpassword))rn rn $this->halt('database connection failed!');rn rn if ($this->varsion() > '4.1')rn rn global $dbcharset;rn mysql_query("SET NAMES 'utf8'");rn rn if ($dbname)rn rn mysql_select_db($dbname);rn rn rn function query($sql)rn rn $this->result = @ mysql_query($sql);rn $this->querycount++;rn if (!$this->result)rn rn $this->halt('SQL statement execution error!,'.$sql);rn rn return $this->result;rn rn function fetch_array($query)rn rn return mysql_fetch_array($query);rn rn function fetch_assoc($query)rn rn return mysql_fetch_assoc($query);rn rn function fetch_row($query)rn rn return mysql_fetch_row($query);rn rn function num_rows($query)rn rn return mysql_num_rows($query);rn rn function varsion()rn rn return mysql_get_server_info();rn rn function geterrdesc()rn rn return mysql_error();rn rn function geterrno()rn rn return intval(mysql_errno());rn rn function halt($msg)rn rn $message = "\n\n";rn $message .= " \n";rn $message .= " \n";rn $message .= "\n";rn $message .= "\n";rn $message .= " Database Error: ".htmlspecialchars($msg)."\n";rn $message .= "Mysql error description: ".htmlspecialchars($this->geterrdesc())."\n ";rn $message .= "Mysql error number: ".$this->geterrno()."\n ";rn $message .= "Date: ".date("Y-m-d @ H:i")."\n ";rn $message .= "Script: http://".$_SERVER['HTTP_HOST'].getenv("REQUEST_URI")."\n ";rn $message .= "\n";rn echo $message;rn exit;rn rnrn?>rnrn数据库连接字符串是没有问题的.也不能操作服务器上的mysql.只能建自己的数据库、表rn请大家一定帮忙! 我快要崩溃了! 论坛

CentOs:ERROR 2002 (HY000): Can't connect to local MySQL server through socket

09-03

使用CentOs7 安装 mysql,命令 yum install mysqlrn但是运行 mysql 一直提示 rn[color=#FF0000][b]ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)[/b][/color]rnrn整个流程:rn1.yum install mysqlrn2.rpm -qa | grep mysql //空的rn3.whereis mysql rn[code=perl][root@webmaster /]# whereis mysqlrnmysql: /usr/bin/mysql /usr/share/man/man1/mysql.1.gz[/code]rn4.那几个文件其实根本不存在的rn[code=perl]rn[root@webmaster /]# ls -l /var/lib/mysql/mysql.sockrnls: cannot access /var/lib/mysql/mysql.sock: No such file or directoryrn[root@webmaster /]# ls -l /var/lib/mysqlrnls: cannot access /var/lib/mysql: No such file or directoryrnrn[/code]rnrn安装过程的代码:rn[code=php]rn================================================================================rn Package Arch Version Repository Sizern================================================================================rnInstalling:rn mariadb x86_64 1:5.5.37-1.el7_0 updates 8.9 MrnrnTransaction Summaryrn================================================================================rnInstall 1 PackagernrnTotal download size: 8.9 MrnInstalled size: 49 MrnIs this ok [y/d/N]: yrnDownloading packages:rnmariadb-5.5.37-1.el7_0.x86_64.rpm | 8.9 MB 00:00:00rnRunning transaction checkrnRunning transaction testrnTransaction test succeededrnRunning transactionrn Installing : 1:mariadb-5.5.37-1.el7_0.x86_64 1/1rn Verifying : 1:mariadb-5.5.37-1.el7_0.x86_64 1/1rnrnInstalled:rn mariadb.x86_64 1:5.5.37-1.el7_0rnComplete!rn[root@webmaster /]# whereis mysqlrnmysql: /usr/bin/mysql /usr/share/man/man1/mysql.1.gzrn[root@webmaster /]# mysqlrnERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)rn[root@webmaster /]# rpm -qa | grep mysql --colorrn[root@webmaster /]# ls -l /etc/my.cnfrn-rw-r--r-- 1 root root 570 Jun 24 04:55 /etc/my.cnfrn[root@webmaster /]# nano /etc/my.cnfrn GNU nano 2.3.1 File: /etc/my.cnfrnrn[mysqld]rndatadir=/var/lib/mysqlrnsocket=/var/lib/mysql/mysql.sockrn# Disabling symbolic-links is recommended to prevent assorted security risksrnsymbolic-links=0rn# Settings user and group are ignored when systemd is used.rn# If you need to run mysqld under a different user or group,rn# customize your systemd unit file for mariadb according to thern# instructions in http://fedoraproject.org/wiki/Systemdrnrn[mysqld_safe]rnlog-error=/var/log/mariadb/mariadb.logrnpid-file=/var/run/mariadb/mariadb.pidrnrn#rn# include all files from the config directoryrn#rn!includedir /etc/my.cnf.drnrn[/code]rnrn 论坛

求助:linux mysql多实例,2002 (HY000): Can't connect to local MySQL server

08-01

我在 /etc/my.cnf 已经配置了 sock文件,可是它依然提示 :Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 可是,明明我的配的不是mysql.sock文件呀。rnmy.cnf 配置如下:[code=C/C++]rn 23# mysql more instancern 24 [mysqld_multi]rn 25 mysqld=/usr/local/mysql/bin/mysqld_safern 26 mysqladmin=/usr/local/mysql/bin/mysqladminrn 27 user=rootrn 28rn 29rn 30 # Here follows entries for some specific programsrn 31rn 32 # The first MySQL serverrn 33 [mysqld1]rn 34 socket = /tmp/mysql1.sockrn 35 port = 3307rn 36 pid-file = /tmp/mysql1.pidrn datadir=/data/mysqlrn 51 user=mysqlrn 57 #rn 58 #skip-networkingrn 59rn 62 log-bin=mysql-binrn 63rn 64 # binary logging format - mixed recommendedrn 65 binlog_format=mixedrn [mysqld2]rn144 socket = /tmp/mysql_queue.sockrn145 port = 3308rn146 pid-file = /tmp/mysql_queue.pidrn159rn160 datadir=/data/mysql_queuern161 log=/data/mysql_queue/mysql_queue.logrn183 [mysqldump]rn184 quickrn185 max_allowed_packet = 16Mrn186rn187 [mysql]rn188 no-auto-rehashrn189 # Remove the next comment character if you are not familiar with SQLrn190 #safe-updatesrn191rn192 [myisamchk]rn193 key_buffer_size = 128Mrn194 sort_buffer_size = 128Mrn195 read_buffer = 2Mrn196 write_buffer = 2Mrn197rn198 [mysqlhotcopy]rn199 interactive-timeoutrn[/code]rn求高人, 如果指定sock是可以登录的 (mysql -u root -P 3308 -S /tmp/mysql_queue.sock -p)rn不指定的话就提示(mysql -u root -P 3308 -p):ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)rnrn 论坛

没有更多推荐了,返回首页