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ÊðÀíЧÀÍÆ÷µÄ¹¹½¨ÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡