连接Mysql提示Can’t connect to local MySQL server through socket的解决方法

    xiaoxiao2021-04-11  39

    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 产生此问题的原因一般有两个: 1、mysql服务未正常运行: 由于mysql的socket文件是由mysqld服务启动时创建的,如果mysqld服务未正常启动,socket文件自然也不会被创建,当然会找不到socket文件了。 对于判断mysql服务是否启动,我们可以使用下面命令: # 1、 端口是否打开,打开的话是这样的 [root@localhost ~]# lsof -i:3306 COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME mysqld  2940 mysql   10u  IPv4  26038      0t0  TCP *:mysql (LISTEN)   # 2、mysqld服务是否正在运行,正在运行中 mysqld (pid  2940) is running... [root@localhost ~]# 2、socket文件路径在配置文件中设置不完整: 这一般是由于我们修改了mysql配置“/etc/my.cnf”引起的。 比如我们修改了配置文件中“[mysql]”选项下的“socket”参数,而未指定“[client]”、“[mysql]”选项的“socket”参数,导致mysql使用默认的socket文件位置去寻找socket文件,从而导致未找到socket文件而引发此错误。 1、mysql服务未正常运行: 如果是服务未启动,我们运行“service mysqld start”启动服务即可。如果服务启动不了,就去查看mysql服务日志,寻找原因并解决再启动 [root@aiezu.com ~]# service mysqld start Starting mysqld:                                           [  OK  ] [root@aiezu.com ~]# lsof -i:3306 COMMAND   PID  USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME mysqld  14109 mysql   10u  IPv4 247183583      0t0  TCP *:mysql (LISTEN) [root@aiezu.com ~]# service mysqld status mysqld (pid  14109) is running... 如果出现这个错误:直接输入mysql即可进入 [root@localhost ~]# service mysqld start Starting mysqld:                                           [  OK  ] [root@localhost ~]# mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) [root@localhost ~]# 进入mysql中了就可以 [root@localhost ~]# mysql Welcome to the MySQL monitor.  Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, 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表空间 mysql> show databases; +--------------------+ | Database           | +--------------------+ | information_schema | | test               | +--------------------+ 2 rows in set (0.00 sec) mysql>

    解决方案太麻烦,还不如重新安装一下呢?

    下面是转载一些大大的;

    2、完善mysql配置文件: 如果确认mysql服务正常运行,还提示文章标题的此错误,那就是“/etc/my.cnf”配置文件的问题了。解决办法是修 改“/etc/my.cnf”配置文件,在配置文件中添加“[client]”选项和“[mysql]”选项,并使用这两个选项下的“socket”参数 值,与“[mysqld]”选项下的“socket”参数值,指向的socket文件路径完全一致。如下: [mysqld] datadir=/storage/db/mysql socket=/storage/db/mysql/mysql.sock ...省略n行(<span class='wp_keywordlink'><a href="http://www.aiezu.com" title="爱E族计算机网络技术博客" target="_blank">爱E族</a></span>)...   [client] default-character-set=utf8 socket=/storage/db/mysql/mysql.sock   [mysql] default-character-set=utf8 socket=/storage/db/mysql/mysql.sock      [mysqld] datadir=/storage/db/mysql socket=/storage/db/mysql/mysql.sock ...省略n行(爱E族)... [client] default-character-set=utf8 socket=/storage/db/mysql/mysql.sock [mysql] default-character-set=utf8 socket=/storage/db/mysql/mysql.sock 其中socket等于的路径就是socket文件的位置,我们只要修改my.cnf文件,告诉mysql,mysqldump,mysqladmin等命令,mysql服 务的socket文件位置在哪里,然后重启mysqld服务即可。 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"     [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()      #!/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); } ?>      <?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); } ?>
    转载请注明原文地址: https://ju.6miu.com/read-666910.html

    最新回复(0)