簡體   English   中英

無法通過Linux上的套接字連接到本地MySQL服務器

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

我嘗試在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收到此消息。

服務器:正在運行...

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中

我已經嘗試過php -m

[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模塊]

我得到的所有與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);

不,那不是正確的位置,我沒有正在運行的mysqld文件夾

好的,那么您需要更改套接字的位置。 您可以通過在DSN中指定每個PDO實例級別來執行此操作,也可以通過在php.ini中指定它來進行批量批發。

對於文檔中為PDO_Mysql DSN定義的PDO:

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

php.ini找到mysql.default_socket並進行更改:

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

除非首頁使用TCP DSN(在DSN中使用localhost以外的IP地址或主機名作為主機屬性),或者您對CLI和Web服務器使用不同的php.ini(這並不罕見)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM