简体   繁体   English

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

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

I try to run a php script on ubunto, and everytime I run it with sudo php -f /opt/lampp/htdocs/scanner/server/start.php i get this message. 我尝试在sudo php -f /opt/lampp/htdocs/scanner/server/start.php上运行php脚本,并且每次使用sudo php -f /opt/lampp/htdocs/scanner/server/start.php运行它时, sudo php -f /opt/lampp/htdocs/scanner/server/start.php收到此消息。

Server: Running... 服务器:正在运行...

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' in /opt/lampp/htdocs/scanner/server/start.php:26 Stack trace: /opt/lampp/htdocs/scanner/server/start.php(26): PDO->__construct('mysql:host=loca...', 'root', 'datakvarnen') /opt/lampp/htdocs/scanner/server/start.php(51): openConnection() {main} thrown in /opt/lampp/htdocs/scanner/server/start.php on line 26 PHP致命错误:消息为'SQLSTATE [HY000] [2002]的未捕获异常'PDOException'无法通过/ opt / lampp中的套接字'/var/run/mysqld/mysqld.sock'(2)'连接到本地MySQL服务器/htdocs/scanner/server/start.php:26堆栈跟踪:/opt/lampp/htdocs/scanner/server/start.php(26):PDO-> __ construct('mysql:host = loca ...',' root','datakvarnen')/opt/lampp/htdocs/scanner/server/start.php(51):openConnection(){main}放在第26行的/opt/lampp/htdocs/scanner/server/start.php中

I have tried with php -m I get 我已经尝试过php -m

[PHP Modules] bcmath bz2 calendar Core ctype date dba dom ereg exif fileinfo filter ftp gettext hash iconv json libxml mbstring mhash mysql mysqli openssl pcntl pcre PDO pdo_mysql Phar posix readline Reflection session shmop SimpleXML soap sockets SPL standard sysvmsg sysvsem sysvshm tokenizer wddx xml xmlreader xmlwriter zip zlib [PHP模块] bcmath bz2日历核心ctype日期dba dom ereg exif fileinfo过滤器ftp gettext哈希iconv json libxml mbstring mhash mysql mysqli openssl pcntl pcre PDO pdo_mysql Phar posix readline反射会话shmop SimpleXML soap sockets SPL标准sysvmsg sysvsizer sysvsxml zip zlib

[Zend Modules] [Zend模块]

All I get with sudo yum install php-pdo and sudo yum install php-pdo_mysql it says it's already installed. 我得到的所有与sudo yum install php-pdosudo yum install php-pdo_mysql都表示已安装。

EDIT: Here is the whole start.php file 编辑:这是整个start.php文件

<?php

$ip     = "127.0.0.1";
$port   = 5012;

error_reporting(E_ALL);

set_time_limit(0);
ob_implicit_flush();

if(!$server = socket_create(AF_INET, SOCK_STREAM, 0)){
    echo socket_strerror(socket_last_error()); exit;
}

if(!socket_bind($server, $ip, $port)){
    echo socket_strerror(socket_last_error()); exit;
}

if(!socket_listen($server, 5)){
    echo socket_strerror(socket_last_error()); exit;
}

echo "Server: Running...\n\n";

function openConnection($db = "scanner"){
    $pdo = new PDO("mysql:host=localhost;dbname={$db};",'root','datakvarnen');
    $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo -> exec("SET CHARACTER SET utf8");
    return $pdo;
}

function sendMessage($connection, $message){
    $message .= "\r\n".chr(0);
    socket_write($connection, $message, strlen($message));
    usleep(5);
}

function readMessage($s){
    //TODO: Fix so it can read any lenght
    $message = @socket_read($s, 1024*10);

    if($message === false){
        return false;
    }

    return $message;
}

//The server is added in the $clients
//The reason for this is because new connection comes as read.
openConnection();
$clients = array();
$null = null;

while(true){
    //Copy $clients so the list doesn't get modified by socket_select();
    $read = $clients;
    $write = $clients;
    $read[] = $server;

    //Wait for read or write
    $ready = socket_select($read, $write, $null, 0);

    //Check if the servers is among the $read clients
    //If it is, then a someone new is trying to connect.
    if(in_array($server, $read)){
        //Search to find the server in $clients
        //It's needed since socket_select() demand we use $read instead of $server
        foreach($read as $client){
            if($client == $server){
                //We found the new connection, and accepts it.
                //TODO: Make a verify code to check it's a scanner.jar that joins
                $new = socket_accept($client);
                $clients[] = $new;
                continue 2;//<-- $server was found, so no need to search anymore.
            }
        }
    }

    foreach($read as $client){
        $message = readMessage($client);

        if($message === false){
            //Socket is closed or lost connection
            $key = array_search($client, $clients);
            unset($clients[$key]);
            continue 2;
        }else{
            //You got the message
            echo $message;
        }
    }

    foreach($write as $client){
        sendMessage($client,rand(0,99999));
    }

    sleep(1);
}

socket_close($server);

No that's not the right location, I have no mysqld folder in run 不,那不是正确的位置,我没有正在运行的mysqld文件夹

Ok then you need to change the location of the socket. 好的,那么您需要更改套接字的位置。 You can do this in a per PDO instance level by specifying it in the DSN or you can do it wholesale by specifying it in the php.ini. 您可以通过在DSN中指定每个PDO实例级别来执行此操作,也可以通过在php.ini中指定它来进行批量批发。

For PDO as defined in the docs for a PDO_Mysql DSN : 对于文档中为PDO_Mysql DSN定义的PDO:

$pdo = new PDO("mysql:unix_socket=/path/to/your/mysqld.sock;dbname={$db};",'root','datakvarnen');

In the php.ini find the mysql.default_socket and change it: php.ini找到mysql.default_socket并进行更改:

mysql.default_socket = /path/to/your/mysqld.sock

Though its a mystery to me how your home page is working unless its using a TCP DSN (using an ip address or hostname other than localhost as the host attribute in the DSN) or you are using a different php.ini for CLI and webserver (which isn't all that uncommon). 除非首页使用TCP DSN(在DSN中使用localhost以外的IP地址或主机名作为主机属性),或者您对CLI和Web服务器使用不同的php.ini(这并不罕见)。

暂无
暂无

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

相关问题 linux服务器上的“无法通过套接字连接到本地MySQL服务器” - “Can't connect to local MySQL server through socket” on linux server 无法通过套接字连接到本地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? mysqli_connect():(HY000 / 2002):无法通过套接字连接到本地MySQL服务器 - mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket 无法通过套接字&#39;/tmp/mysql.sock&#39;(2)错误连接到本地MySQL服务器 - Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM