×ðÁú¿­Ê±¹ÙÍøµÇ¼

SwooleʵÏÖ¸ßËÙMySQLÊðÀíЧÀÍÆ÷µÄ¹¹½¨ÒªÁì

Ëæ×Å»¥ÁªÍøÉú³¤µÄѸËÙ׳´ó £¬×ÔÁ¦¿ª·¢ÕߺÍÆóÒµÓû§¾­³£ÐèÒªÃæÁÙ´¦Àí´ó×ÚÊý¾ÝµÄʹÃü¡£mysqlÒѾ­³ÉΪÁË×î³£ÓõĹØϵÐÍÊý¾Ý¿âÖ®Ò» £¬Òò´Ë £¬ÎÒÃÇÐèÒªÌá¸ßÊý¾Ý¿âµÄ´¦ÀíЧÂʺÍÐÔÄÜ £¬¸ÄÉÆÓû§ÌåÑé¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃswooleʵÏÖ¸ßËÙmysqlÊðÀíЧÀÍÆ÷µÄ¹¹½¨ÒªÁì £¬ÒÔÌá¸ßmysqlµÄÐÔÄÜ¡£

MySQLÊðÀíЧÀÍÆ÷¼ò½é

MySQLÊðÀíЧÀÍÆ÷ÊÇÒ»ÖÖ¿ÉÒÔͨ¹ý±à³Ì·½·¨»á¼ûMySQLÊý¾Ý¿âµÄ¹¤¾ß £¬Ëüͨ³£ÓÃÓÚЭµ÷¶à¸öÇëÇó¡¢»º´æÅÌÎÊЧ¹û¡¢ÔöÇ¿Çå¾²ÐԵȡ£Ê¹ÓÃMySQLÊðÀíЧÀÍÆ÷¿ÉÒÔ¸ÄÉÆÊý¾Ý¿âÐÔÄÜ £¬¼õÇáÊý¾Ý¿â¼ç¸º £¬Ìá¸ßÓû§ÌåÑé¡£

SwooleÏÈÈÝ

SwooleÊÇÒ»¸ö¸ßÐÔÄÜÒì²½ÍøÂçͨѶÒýÇæ £¬Ö§³ÖTCP¡¢UDP¡¢UnixµÄSocket´«Êäģʽ £¬Ò²Ö§³ÖHTTPºÍWebSocketЭÒé £¬²¢ÇÒÖ§³ÖÒì²½MySQL¡¢RedisµÈ¹ØϵÐͺͷǹØϵÐÍÊý¾Ý¿â¡£Swoole»ùÓÚPHPÀ©Õ¹ £¬Æäµ×²ãʹÓÃÁËepollºÍkqueueµÈ¸ßЧÊÂÎñÂÖѯ»úÖÆ £¬¿ÉÒÔ½èÖúÆäЭ³ÌÊÖÒÕ £¬ÒÔ·ÇÛÕ±Õ·½·¨Ö´ÐÐPHP´úÂë £¬´ó´óÌá¸ßÁËI/O÷缯ÐÍʹÃüµÄ´¦ÀíЧÂÊ¡£

»ù±¾ÊµÏÖ˼Ð÷

¹¹½¨¸ßËÙMySQLÊðÀíЧÀÍÆ÷µÄ»ù±¾ÊµÏÖ˼Ð÷ÈçÏ£º

£¨1£©Ê¹ÓÃSwooleµÄÒì²½MySQL¿Í»§¶ËÅþÁ¬µ½MySQLЧÀÍÆ÷¡£

£¨2£©ÎüÊÕ¿Í»§¶ËÅþÁ¬ £¬²¢½«¿Í»§¶Ë·¢Ë͵ÄÖ¸Áîת·¢¸øMySQLЧÀÍÆ÷¡£

£¨3£©ÔÚMySQLЧÀÍÆ÷·µ»ØÅÌÎÊЧ¹ûʱ £¬½«Ð§¹û»º´æµ½ÄÚ´æÖÐ £¬ÒÔ¼ÓËÙºóÐøµÄÅÌÎʲÙ×÷¡£

£¨4£©½«MySQLЧÀÍÆ÷·µ»ØµÄЧ¹û·¢Ë͸ø¿Í»§¶Ë¡£

£¨5£©¹Ø±ÕÅþÁ¬¡£

´úÂëʵÏÖ

ÒÔÏÂÊÇʹÓÃSwooleʵÏÖ¸ßËÙMySQLÊðÀíЧÀÍÆ÷µÄ´úÂ룺

<?php
$server = new SwooleServer("127.0.0.1", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

$server->on("Connect", function($server, $fd){
    echo "Client connected.
";
});

$server->on("Receive", function($server, $fd, $from_id, $data){
    // ÅþÁ¬MySQL
    $mysql = new SwooleCoroutineMySQL();
    $mysql->connect([
        'host' => '127.0.0.1',
        'user' => 'root',
        'password' => '',
        'database' => 'test',
        'port' => 3306,
        'charset' => 'utf8mb4',
    ]);
    // Ö´ÐÐSQLÓï¾ä
    $result = $mysql->query($data);
    $server->send($fd, $result);
});

$server->on("Close", function($server, $fd){
    echo "Connection closed.
";
});

$server->start();

µÇ¼ºó¸´ÖÆ

×ܽá

±¾ÎÄÏÈÈÝÁËʹÓÃSwooleʵÏÖ¸ßËÙMySQLÊðÀíЧÀÍÆ÷µÄ¹¹½¨ÒªÁì £¬¸ÃЧÀÍÆ÷¿ÉÌá¸ßMySQLÊý¾Ý¿âµÄÐÔÄÜ¡¢ÔöÇ¿Çå¾²ÐÔ¡¢¼õÇáÊý¾Ý¿â¼ç¸º¡¢¼ÓËÙÅÌÎÊ´¦ÀíµÈ¡£Ê¹ÓÃSwooleµÄЭ³ÌÊÖÒÕ £¬¿ÉÒÔ´ó´óÌá¸ßI/O÷缯ÐÍʹÃüµÄ²¢·¢´¦ÀíÄÜÁ¦ £¬ÓÅ»¯´úÂëÐÔÄÜ¡£

¿ÉÊÇ £¬ÐèҪעÖصÄÊÇ £¬MySQLÊðÀíЧÀÍÆ÷ÐèÒª±£»¤ºÃ¿Í»§¶ËÊý¾Ý £¬ÁíÍâ £¬ÐèҪ˼Á¿µ½¶àÖÖÒì³£ÇéÐεĴ¦Àí £¬ÈçÍøÂç²»Îȹ̡¢MySQLЧÀÍÆ÷ÅþÁ¬³¬Ê±¡¢ÅÌÎÊЧ¹ûÒì³£µÈµÈ¡£×îºÃÍŽá×Ô¼ºµÄÏÖÕæÏàÐξÙÐе÷½âºÍÐÞ¸Ä £¬²Å»ªÊµÏÖÕæÕýµÄ¸ßËÙMySQLÊðÀíЧÀÍÆ÷¡£

ÒÔÉϾÍÊÇSwooleʵÏÖ¸ßËÙMySQLÊðÀíЧÀÍÆ÷µÄ¹¹½¨ÒªÁìµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ