提问者:小点点

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


我尝试在ubto上运行一个php脚本,每次我都使用sudophp-f/opt/lampp/htdocs/scanner/server/start运行它。php我收到了这条消息。

服务器:正在运行...

PHP致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[HY000][2002]无法通过套接字连接到本地MySQL服务器”/var/run/mysqld/mysqld。sock'(2)在/opt/lampp/htdocs/scanner/server/start中。php:26堆栈跟踪:/opt/lampp/htdocs/scanner/server/start。php(26):PDO-

我已经尝试了php-m

[PHP模块]bcath bz2日历核心ctype日期dba dom ereg exif fileinfo过滤器ftp gettext哈希iconv json libxml mbstring mhash mhash mysql mysqli openssl pcntl pcre PDOpdo_mysqlPhar posx readline反射会话shmop SimpleXML肥皂套接字SPL标准sysvmsg sysvsem sysvshm令牌生成器wddx xml xmlReader xmlWriter zipZlib

[Zend模块]

我从sudo-yum-install-php-pdosudo-yum-install-php-pdo_-mysql得到的所有信息都表明它已经安装好了。

编辑:这是整个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);

共1个答案

匿名用户

不,那不是正确的位置,我没有运行mysqld文件夹

好的,那么您需要更改插座的位置。通过在DSN中指定,您可以在每个PDO实例级别执行此操作,或者通过在php中指定,您可以批量执行此操作。伊尼。

对于PDO_MysqlDSN文档中定义的PDO:

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

php中。ini查找mysql。默认\u套接字并更改它:

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

尽管对我来说,主页的工作方式很神秘,除非它使用TCP DSN(在DSN中使用ip地址或主机名而不是localhost作为主机属性),或者您使用的是不同的php。CLI和Web服务器的ini(这并不少见)。