简体   繁体   English

linux服务器上的“无法通过套接字连接到本地MySQL服务器”

[英]“Can't connect to local MySQL server through socket” on linux server

I'm having a hard time getting my website connected to the mySQL database on host. 我很难将我的网站连接到主机上的mySQL数据库。 However, when I was running my website on the PC by Apache it was connecting smoothly. 但是,当我在PC上运行我的网站时,它正在顺利连接。 The snippet I'm using to connect is: 我用来连接的片段是:

<?php
    $conn_error = 'Could not connect';

    $mysql_host = 'localhost';
    $mysql_user = 'root';
    $mysql_pass = '';

    $mysql_db = 'firstdatabase';

    if(!mysql_connect($mysql_host,$mysql_user,$mysql_pass)|| !@mysql_select_db($mysql_db)){
        die($conn_error);
    }else{
        //echo 'Connected';
    }
?>

And it gives me the following error when I try to connect to mysql on the web host: 当我尝试连接到Web主机上的mysql时,它给了我以下错误:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server
through socket 'MySQL' (2) in /home/umudo/public_html/connectserver.inc.php on line 10

I searched through the web for finding a solution but couldn't find any exact example of this. 我在网上搜索以查找解决方案,但找不到确切的示例。

Help: http://dev.mysql.com/doc/refman/5.0/en/connecting.html 帮助: http//dev.mysql.com/doc/refman/5.0/en/connecting.html

Says: 说:

On Unix, MySQL programs treat the host name localhost specially , in a way that is likely different from what you expect compared to other network-based programs. 在Unix上,MySQL 程序特别对待主机名localhost ,与其他基于网络的程序相比,它可能与您期望的方式不同。 For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. 为了连接到本地主机,MySQL程序尝试使用Unix套接字文件连接到本地服务器。

So you have to 所以你必须这样做

  • use "127.0.0.1" instead of "localhost" to use tcpip instead of sockets, or 使用“127.0.0.1”而不是“localhost”来使用tcpip而不是套接字,
  • enable sockets in mysql server config (socket connections are probably disabled and that causes that error on linux) 在mysql服务器配置中启用套接字(套接字连接可能已禁用,这会在Linux上导致该错误)

Second method is better - using sockets for local connections is better than TCP/IP (better performance), but you may have no privileges to mysql configuration on that server. 第二种方法更好 - 使用套接字进行本地连接比TCP / IP(性能更好)更好,但是您可能没有权限在该服务器上进行mysql配置。

尝试像apt-get install phpmyadmin中一样安装phpmyadmin或php5-mysql。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过Linux上的套接字连接到本地MySQL服务器 - Can't connect to local MySQL server through socket on linux 无法通过套接字连接到本地MySQL服务器 - Can't connect to local MySQL server through socket Issue 错误:无法通过套接字连接到本地MySQL服务器 - Error: Can't connect to local MySQL server through socket 如何解决“无法通过套接字连接到本地MySQL服务器” - How to solve “Can't connect to local MySQL server through socket” 随机无法通过套接字连接到本地MySQL服务器 - Randomly Can't connect to local MySQL server through socket 警告:mysql_connect():无法通过套接字“ /tmp/mysql.sock”连接到本地MySQL服务器(2) - Warning: mysql_connect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 无法连接:无法通过套接字“ /var/lib/mysql/mysql.sock”连接到本地MySQL服务器(2) - Could not connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) mysql_connect在远程主机连接上返回“无法通过套接字连接到本地MySQL服务器”? - mysql_connect returning “Can't connect to local MySQL server through socket” on remote host connection? 无法通过套接字连接到本地MySQL服务器,我不是服务器管理员(使用托管计划) - Can't connect to local MySQL server through socket, I'm not the server admin (using hosting plan) mysqli_connect():(HY000 / 2002):无法通过套接字连接到本地MySQL服务器 - mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM