ÍêÈ«ÕÆÎÕSwooleµÄгÌMySQLÉè¼Æ˼Ð÷ÓëÓ¦ÓÃʵ¼ù
swooleÊÇÒ»¿î»ùÓÚphpÓïÑԵĸßÐÔÄÜÍøÂçͨѶ¿ò¼Ü£¬ËüÖ§³Ötcp/udp/http/websocketµÈ¶àÖÖÐÒ飬ͬʱҲ֧³ÖгÌʵÏÖ¡£ÔÚ»¥ÁªÍø¼ÓËÙ¡¢ÎïÁªÍø¡¢ÓÎÏ·¿ª·¢¡¢´óÊý¾Ý´¦ÀíµÈÁìÓò£¬swooleÒѾ³ÉΪÁËÖڶ࿪·¢ÕßµÄÊ×Ñ¡¿ò¼Ü¡£
ÔÚSwoole¿ò¼ÜÖУ¬Ð³ÌMySQLÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄ×é¼þ¡£Ð³ÌMySQLÌṩÁËÒ»ÖÖ¸ßЧ¡¢¼òÆӵķ½·¨À´ÅþÁ¬ºÍ²Ù×÷MySQLÊý¾Ý¿â¡£Ïà½ÏÓڹŰåµÄMySQLÅþÁ¬·½·¨£¬Ð³ÌMySQLÔÚÐÔÄÜÉÏÓÐ×ÅÏÔ×ŵÄÓÅÊÆ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ì½ÌÖSwooleµÄгÌMySQLÉè¼Æ˼Ð÷ÓëÓ¦ÓÃʵ¼ù£¬×ÊÖú¶ÁÕ߸üºÃµØÕÆÎÕÕâÒ»Ö÷Òª×é¼þ¡£
Ò»¡¢Ð³ÌMySQLµÄÉè¼Æ˼Ð÷
гÌMySQLµÄÉè¼Æ˼Ð÷Ö÷Òª·ÖΪÁ½¸ö·½Ã棺
³¤ÅþÁ¬ÓëÅþÁ¬³Ø
гÌMySQLʹÓó¤ÅþÁ¬ºÍÅþÁ¬³ØʵÏÖ¸ßЧµÄÊý¾Ý¿âÅþÁ¬¡£³¤ÅþÁ¬ÊÇÖ¸ÅþÁ¬MySQLÊý¾Ý¿âµÄ»ù´¡TCPÅþÁ¬Ò»Ö±¼á³Ö×ÅÒ»Ö±¿ª£¬Ö±µ½Àú³ÌÍ˳ö»òÕßÊÖ¶¯¹Ø±Õ¡£ÕâÑù¿ÉÒÔïÔÌÿ´ÎÇëÇóʱÖØн¨ÉèTCPÅþÁ¬µÄ¿ªÏú£¬Ìá¸ßÅþÁ¬Ð§ÂÊ¡£ÅþÁ¬³ØÔòÊÇΪÁ˳ä·ÖʹÓÃÅþÁ¬×ÊÔ´£¬½«ÅþÁ¬³ØÖеÄÅþÁ¬Æ¾Ö¤Ò»¶¨Õ½ÂÔ¾ÙÐзÖÅÉ¡£Í¨¹ýÅþÁ¬³Ø£¬¿ÉÒÔ¿ØÖÆÅþÁ¬µÄÊýÄ¿ºÍÓÐÓÃÆڵȣ¬×èÖ¹Êý¾Ý¿âµÄÅþÁ¬×ÊÔ´±»ºÄ¾¡¡£
Òì²½IOÓëгÌ
гÌMySQLʹÓÃÒì²½IOºÍгÌʵÏÖ¸ßЧµÄIO´¦Àí¡£¹Å°åµÄMySQLÅþÁ¬ÊÇͬ²½µÄ£¬Ò²¾ÍÊÇ˵ÔÚÖ´ÐÐÒ»¸öSQLÅÌÎÊʱ»áÒ»Ö±ÆÚ´ýÖ±µ½ÅÌÎÊЧ¹û·µ»Ø¡£¶øгÌMySQLʹÓÃÁËг̺ÍÒì²½IOÊÖÒÕ£¬½«Êý¾ÝÇëÇóÓë´¦ÀíÊèÉ¢¿ªÀ´£¬Ê¹µÃÅÌÎÊÇëÇó¿ÉÒÔÔÚ·¢ËͺóÁ¬Ã¦¾ÙÐÐÏÂÒ»´ÎÅÌÎÊ»òÕßÆäËû²Ù×÷£¬²»ÐèÒªÆÚ´ýÅÌÎÊЧ¹û¡£µ±ÅÌÎÊЧ¹û·µ»Øʱ£¬Ð³ÌMySQL»á×Ô¶¯´¥·¢Ð³Ì»Ö¸´£¬²¢½«Ð§¹ûת´ï¸øг̾ÙÐд¦Àí¡£
¶þ¡¢Ð³ÌMySQLµÄÓ¦ÓÃʵ¼ù
ÔÚSwoole¿ò¼ÜÖУ¬Ê¹ÓÃгÌMySQLÒ²ºÜÊǼòÆÓ¡£ÏÂÃæÎÒÃǽ«Í¨¹ýÒ»¸ö¼òÆÓʾÀý£¬À´ÑÝʾгÌMySQLµÄ»ù±¾Ó¦Óá£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚSwooleÖÐÆôÓÃгÌMySQLÀ©Õ¹£¬¿ÉÒÔͨ¹ýÒÔÏ´úÂë¾ÙÐÐʵÏÖ£º
Coun(function() { $mysql = new SwooleCoroutineMySQL(); $mysql->connect([ 'host' => '127.0.0.1', 'user' => 'root', 'password' => 'root', 'database' => 'test_db', ]); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×ÏÈʹÓÃCoun½¨ÉèÁËÒ»¸öг̣¬È»ºóͨ¹ýSwooleCoroutineMySQL£¨SwooleÖеÄгÌMySQLÀࣩ½¨ÉèÁËÒ»¸öMySQLÅþÁ¬¡£ÆäÖУ¬connectÒªÁìÖÐÉèÖÃÁËMySQLµÄÅþÁ¬²ÎÊý¡£Í¨¹ýÕâ¸öʾÀý£¬ÎÒÃÇʵÏÖÁËÒ»¸ö¼òÆÓµÄMySQLÅþÁ¬£¬²¢ÇÒ¿ÉÒÔ×îÏȾÙÐÐÊý¾ÝµÄ¶Áд²Ù×÷¡£
½ÓÏÂÀ´£¬ÎÒÃǽ«ÑÝʾÔõÑù¾ÙÐÐMySQLÊý¾Ý¿âµÄÅÌÎʲÙ×÷¡£ÒÔÏÂÊÇʾÀý´úÂ룺
Coun(function () { $mysql = new SwooleCoroutineMySQL(); $mysql->connect([ 'host' => '127.0.0.1', 'user' => 'root', 'password' => 'root', 'database' => 'test', ]); $result = $mysql->query('SELECT * FROM users where id = 1'); var_dump($result); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ýqueryÒªÁìʵÏÖÁËÒ»¸ö»ù±¾µÄÅÌÎʲÙ×÷£¬ÅÌÎÊusers±íÖÐidΪ1µÄ¼Í¼£¬²¢½«ÅÌÎÊЧ¹ûÊä³ö¡£
×îºó£¬ÎÒÃǽ«½²ÊöÔõÑù¾ÙÐÐMySQLÊý¾Ý¿âµÄдÈë²Ù×÷¡£ÒÔÏÂÊÇʾÀý´úÂ룺
Coun(function () { $mysql = new SwooleCoroutineMySQL(); $mysql->connect([ 'host' => '127.0.0.1', 'user' => 'root', 'password' => 'root', 'database' => 'test', ]); $data = ['name' => 'user1', 'password' => 'pwd1']; $tableName = 'users'; $keys = implode(',', array_keys($data)); $values = implode(',', array_fill(0, count($data), '?')); $sql = "INSERT INTO `{$tableName}` ({$keys}) VALUES ({$values})"; $params = array_values($data); $result = $mysql->prepare($sql)->execute($params); var_dump($result); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ýprepareÒªÁìºÍexecuteÒªÁìʵÏÖÁËÒ»¸ö»ù±¾µÄSQLдÈë²Ù×÷¡£prepareÒªÁì¿ÉÒÔ½¨ÉèÒ»¸öÔ¤´¦ÀíÓï¾ä£¬½«SQLºÍÐè°ó¶¨µÄ²ÎÊý´«È룬·µ»ØÒ»¸öгÌMySQLÔ¤´¦Àí¹¤¾ß¡£execute·½¹æÔòÊÇÖ´ÐÐÔ¤´¦ÀíÓï¾ä£¬²¢·µ»ØÖ´ÐÐЧ¹û¡£
×ܽá
гÌMySQLÊÇSwoole¿ò¼ÜÖкÜÊÇÖ÷ÒªµÄÄ£¿é£¬ËüʹÓÃÁ˳¤ÅþÁ¬¡¢ÅþÁ¬³Ø¡¢Òì²½IOºÍг̵ÈÊÖÒÕ£¬ÊµÏÖÁ˸ßЧ¡¢Îȹ̵ÄMySQLÅþÁ¬Óë¶Áд²Ù×÷¡£Í¨¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇ¿ÉÒÔ¸üºÃµØÏàʶгÌMySQLµÄÉè¼Æ˼Ð÷ºÍÓ¦ÓÃʵ¼ù£¬´Ó¶ø¸üºÃµØÕÆÎÕгÌMySQLµÄʹÓÃÒªÁì¡£
ÒÔÉϾÍÊÇÍêÈ«ÕÆÎÕSwooleµÄгÌMySQLÉè¼Æ˼Ð÷ÓëÓ¦ÓÃʵ¼ùµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡