PHP连接MySQL报错:SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' (2)
如下所示,PHP连接MySQL报错:
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' (2)
测试代码如下:
<?php
try
{
$dsn = 'mysql:dbname=php-note;host=localhost;port=3306;charset=utf8';
$username = 'root';
$password = 'root';
new PDO( $dsn, $username, $password);
}
catch (\PDOException $e)
{
echo $e->getMessage();
}
【解决方案】
把 host=localhost 改为 host=127.0.0.1 即可!
连接Mysql提示Can’t connect to local MySQL server through socket的解决方法
mysql,Mysqldump,Mysqladmin,php连接mysql服务常会提示下面错误:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
这是由于修改mysql服务的了socket文件mysql.sock位置,而导致无法通过mysql socket文件连接到mysql服务引起的,具体解决办法如下:
1、查看mysql服务的socket文件位置:
mysql socket文件的位置是在/etc/my.cnf中设置的,cat /etc/my.cnf内容如下:
[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock
user=mysql
其中socket等于的路径就是socket文件的位置,我们只要修改my.cnf文件,告诉mysql,mysqldump,mysqladmin mysql服务的socket位置在哪里就可以。
2、修改my.cnf文件:
在/etc/my.cnf文件中添加如下内容,并重启mysqls服务,即可解决mysql,mysqldump,mysqladmin的“Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'”问题:
[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock[mysql]
socket=/storage/db/mysql/mysql.sock[mysqldump]
socket=/storage/db/mysql/mysql.sock[mysqladmin]
socket=/storage/db/mysql/mysql.sock
3、php连接mysql服务提示"Can't connect to local MySQL server through socket..."的解决方法
有时候mysql服务正常运行,用户名密码也完全正确,使用php的mysql_connect函数却连接不了mysql,调用php的mysql_error()函数提示“Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'”,这是我们需要修改/etc/php.ini文件。
在/etc/php.ini文件中"[MySQL]"项下找到"mysql.default_socket",并设置其值指向正确的mysql服务socket文件即可,如:
[MySQL]
...省略n行...
mysql.default_socket = "/storage/db/mysql/mysql.sock"
4、python连接mysql提示"Can't connect to local MySQL server through socket..."的解决方法:
在连接mysql数据库函数中指定socket文件,如下:
#!/usr/bin/python
from MySQLdb import connect
conn = connect(db="pzy", user="root", host="localhost", unix_socket="/storage/db/mysql/mysql.sock")
cur = conn.cursor()
count=cur.execute("show databases")
print 'there has %s dbs' % count
conn.commit()
conn.close()
5、php pdo连接mysql提示"Can't connect to local MySQL server through socket..."的解决方法:
同样在连接字符串添加mysql socket文件的位置即可,如下:
<?php
$dsn = "mysql:host=localhost;dbname=pzy;unix_socket=/storage/db/mysql/mysql.sock";
$db = new PDO($dsn, 'root', '');
$rs = $db->query("SELECT * FROM qrtest");
while($row = $rs->fetch()){
print_r($row);
}
?>
参考:
http://www.aiezu.com/db/mysql_cant_connect_through_socket.html
PHP连接MySQL报错:SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' (2)的更多相关文章
- SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 这个 ...
- laravel 报错SQLSTATE[HY000] [2002] No such file or directory
在mac中执行php artisan migrate时报错 SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from i ...
- MySQL 连接超时:报错SQLSTATE[HY000] [2002] Connection timed out解决
当你的代码部署到服务器里的时候,你的mysql 的host 值 应该为 127.0.0.1 而不是 你的服务器ip 不然就会报错. 其实当你的代码进入到服务器里的时候,mysql和代码是相当于在同一个 ...
- MySQL 连接超时:报错SQLSTATE[HY000] [2002] Connection timed out
在网上找了一堆,结果全部是错的 后来,我明白了其实是设置问题. 当你的代码部署到服务器里的时候,你的mysql 的host 值 应该为 127.0.0.1 而不是 你的服务器ip 不然就会报错. 其实 ...
- PHP连接MySQL的时候报错SQLSTATE[HY000] [2002] No such file or directory
错误环境:Mac OS 10.10 找到mysql.sock文件的位置 $sudo find / -name mysql.sock ------结果如下---------- find: /dev/fd ...
- thinkphp 数据库连接报错 SQLSTATE[HY000] [2002] No such file or directory
https://blog.csdn.net/tornge/article/details/51388233 找到mysql sokcet的路径 vim /etc/mysql/mysql.conf.d/ ...
- Navicat连接Mysql报错:Client does not support authentication protocol requested by server;
Navicat连接Mysql报错:Client does not support authentication protocol requested by server: 刚安装Mysql,想用Nav ...
- egg 连接 mysql 的 docker 容器,报错:Client does not support authentication protocol requested by server; consider upgrading MySQL client
egg 连接 mysql 的 docker 容器,报错:Client does not support authentication protocol requested by server; con ...
- 【Problem】xampp in ubuntu下命令行启动mysql报错: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/lampp/var/mysql/mysql.sock' (2)
xampp in ubuntu下命令行启动mysql报错: reddevil@reddevil-Lenovo:/opt/lampp$ ./bin/mysql -u root -p Enter pass ...
随机推荐
- 5.HBase In Action 第一章-HBase简介(1.1.3 HBase的兴起)
Pretend that you're working on an open source project for searching the web by crawling websites and ...
- IT男的”幸福”生活"续1
IT男的”幸福”生活"续1 我和LL开始下步追妹计划...... 知彼知已,方能把握机会.没想到孙子兵法太给力了,据LL了解,MM(她)是湖北人,找对象一般不会考虑外省的.高度165左右,相 ...
- 8个免费实用的C++GUI库(转载)
C++标准中并没有包含GUI,这也使得C++开发图形化界面需要依赖于第三方的库.实际上,图形界面恰恰是C++的强项,小到平常使用的各类桌面软件,大到魔兽世界这样的游戏,都是C++擅长的地方.C++ ...
- JqueryEasyUI教程入门篇
什么是jQueryEasyUI? JqueryUI是一组基于jQuery的UI插件集合 学习jQueryEasyUI的条件? 必须掌握Jquery的基本语法知识 jQueryEasyUI的特点? 1. ...
- node 通用的中间件
为什么学习Node,因为他的门槛比较高一点,现在比较热门一点. 技术这种东西,用最短的时间学会了收益终身. 1.常用的中间件: // 通用的中间件 //bodyParser connect 内建的中间 ...
- css边框阴影
<style type="text/css">.mydiv{width:250px;height:auto;border:#909090 1px solid;backg ...
- 'UserInfoBLL' node cannot be resolved for the specified context [MVC展示数据.Controllers.LoginController]问题解决
我们在配置Spring.Net时,往往会提示找不到,然而看了看都对着呢?那么问题出在了哪? 问题呈现: 进行如下修改,将名字随便取个,不为BLL方法名字即可,我这里添加了一个1,注意这里改了,控制器里 ...
- MySQL 5.6 my.cnf 参数说明
# 以下选项会被MySQL客户端应用读取. # 注意只有MySQL附带的客户端应用程序保证可以读取这段内容. # 如果你想你自己的MySQL应用程序获取这些值. # 需要在MySQL客户端库初始化的时 ...
- 每天一个linux命令(42):crontab命令
前 一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的. Linux 系统上面原本就有非常多的计划性工作,因此 ...
- WebService学习过程中的心得和问题
1.发布一个WebService 2.调用第三方提供的WebService服务