老师您好 我这边在处理协程 mysql时出现报错 (上部分报错 下部分 代码 )是需要修改什么配置吗

来源:5-5 异步Mysql详解(上)

1652833140

2019-08-06

[root@swoole io]# php sql.php
PHP Fatal error: Uncaught Swoole\Error: operation not support (reactor is not ready) in /root/study/work/demo/io/sql.php:4
Stack trace:
#0 /root/study/work/demo/io/sql.php(4): Swoole\Coroutine\MySQL->connect(Array)
#1 {main}
thrown in /root/study/work/demo/io/sql.php on line 4

Fatal error: Uncaught Swoole\Error: operation not support (reactor is not ready) in /root/study/work/demo/io/sql.php:4
Stack trace:
#0 /root/study/work/demo/io/sql.php(4): Swoole\Coroutine\MySQL->connect(Array)
#1 {main}
thrown in /root/study/work/demo/io/sql.php on line 4
[root@swoole io]# cat sql.php

<?php $swoole_mysql = new Swoole\Coroutine\MySQL(); $swoole_mysql->connect([ 'host' => '127.0.0.1', 'port' => 3306, 'user' => 'root', 'password' => '', 'database' => 'swoole', ]); $res = $swoole_mysql->query('select * from test where id = 1'); ?>
写回答

1回答

singwa

2019-08-07

您好。用这个试试 Swoole\Coroutine::create()

0
4
1652833140
回复
singwa
老师您好 又有个新的问题 就是现在数据库连接不上 虚拟机搭建的环境 数据库是使用本地的数据库连接的 代码如下:Swoole\Coroutine::create(function(){ $swoole_mysql = new Swoole\Coroutine\MySQL(); $connect = $swoole_mysql->connect([ 'host' => '127.0.0.1', 'port' => 3306, 'user' => 'root', 'password' => '', 'database' => 'test', ]); // $res = $swoole_mysql->query('select sleep(1)'); var_dump($swoole_mysql); $res = $swoole_mysql->query('select * from test where id = 1'); if($res === false){ echo $swoole_mysql->connect_error.PHP_EOL; } var_dump($res); }); 报错提示:object(Swoole\Coroutine\MySQL)#2 (9) { ["serverInfo"]=> NULL ["sock"]=> int(-1) ["connected"]=> bool(false) ["connect_errno"]=> int(2002) ["connect_error"]=> string(41) "SQLSTATE[HY000] [2002] Connection refused" ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["error"]=> string(0) "" ["errno"]=> int(0) } SQLSTATE[HY000] [2002] Connection refused bool(false)
2019-08-07
共4条回复

Swoole入门到实战打造高性能赛事直播平台

swoole创始人推荐,借助Swoole打造高性能直播平台

2228 学习 · 1062 问题

查看课程