webman 无法连接mysql 提示SQLSTATE[HY000] [2002]

marsColin

我的配置如下

'default' => 'mysql',
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '192.168.1.15'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'tomdb'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', '841222'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
    ],

我直接使用mysql客户端在linux上是是可以正常连接的,
现在使用$name = Db::table('userInfo')->get();报错如下

PDOException: SQLSTATE[HY000] [2002] Connection refused in /home/tomServer/vendor/illuminate/database/Connectors/Connector.php:70
Stack trace:
#0 /home/tomServer/vendor/illuminate/database/Connectors/Connector.php(70): PDO->__construct()
#1 /home/tomServer/vendor/illuminate/database/Connectors/Connector.php(100): Illuminate\Database\Connectors\Connector->createPdoConnection()
#2 /home/tomServer/vendor/illuminate/database/Connectors/Connector.php(50): Illuminate\Database\Connectors\Connector->tryAgainIfCausedByLostConnection()
#3 /home/tomServer/vendor/illuminate/database/Connectors/MySqlConnector.php(24): Illuminate\Database\Connectors\Connector->createConnection()
#4 /home/tomServer/vendor/illuminate/database/Connectors/ConnectionFactory.php(184): Illuminate\Database\Connectors\MySqlConnector->connect()
#5 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\{closure}()
#6 /home/tomServer/vendor/illuminate/database/Connection.php(926): call_user_func()
#7 /home/tomServer/vendor/illuminate/database/Connection.php(961): Illuminate\Database\Connection->getPdo()
#8 /home/tomServer/vendor/illuminate/database/Connection.php(405): Illuminate\Database\Connection->getReadPdo()
#9 /home/tomServer/vendor/illuminate/database/Connection.php(331): Illuminate\Database\Connection->getPdoForSelect()
#10 /home/tomServer/vendor/illuminate/database/Connection.php(664): Illuminate\Database\Connection->Illuminate\Database\{closure}()
#11 /home/tomServer/vendor/illuminate/database/Connection.php(745): Illuminate\Database\Connection->runQueryCallback()
#12 /home/tomServer/vendor/illuminate/database/Connection.php(725): Illuminate\Database\Connection->tryAgainIfCausedByLostConnection()
#13 /home/tomServer/vendor/illuminate/database/Connection.php(634): Illuminate\Database\Connection->handleQueryException()
#14 /home/tomServer/vendor/illuminate/database/Connection.php(339): Illuminate\Database\Connection->run()
#15 /home/tomServer/vendor/illuminate/database/Query/Builder.php(2260): Illuminate\Database\Connection->select()
#16 /home/tomServer/vendor/illuminate/database/Query/Builder.php(2248): Illuminate\Database\Query\Builder->runSelect()
#17 /home/tomServer/vendor/illuminate/database/Query/Builder.php(2743): Illuminate\Database\Query\Builder->Illuminate\Database\Query\{closure}()
#18 /home/tomServer/vendor/illuminate/database/Query/Builder.php(2249): Illuminate\Database\Query\Builder->onceWithColumns()
#19 /home/tomServer/app/api/controller/UserLogin.php(19): Illuminate\Database\Query\Builder->get()
#20 /home/tomServer/vendor/workerman/webman-framework/src/App.php(168): app\api\controller\UserLogin->view()
#21 /home/tomServer/vendor/workerman/workerman/Connection/TcpConnection.php(660): Webman\App->onMessage()
#22 [internal function]: Workerman\Connection\TcpConnection->baseRead()
#23 /home/tomServer/vendor/workerman/workerman/Events/Event.php(195): EventBase->loop()
#24 /home/tomServer/vendor/workerman/workerman/Worker.php(2412): Workerman\Events\Event->loop()
#25 /home/tomServer/vendor/workerman/workerman/Worker.php(1537): Workerman\Worker->run()
#26 /home/tomServer/vendor/workerman/workerman/Worker.php(1367): Workerman\Worker::forkOneWorkerForLinux()
#27 /home/tomServer/vendor/workerman/workerman/Worker.php(1341): Workerman\Worker::forkWorkersForLinux()
#28 /home/tomServer/vendor/workerman/workerman/Worker.php(1674): Workerman\Worker::forkWorkers()
#29 /home/tomServer/vendor/workerman/workerman/Worker.php(1623): Workerman\Worker::monitorWorkersForLinux()
#30 /home/tomServer/vendor/workerman/workerman/Worker.php(544): Workerman\Worker::monitorWorkers()
#31 /home/tomServer/start.php(130): Workerman\Worker::runAll()
#32 {main}

Next Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `userInfo`) in /home/tomServer/vendor/illuminate/database/Connection.php:671
Stack trace:
#0 /home/tomServer/vendor/illuminate/database/Connection.php(745): Illuminate\Database\Connection->runQueryCallback()
#1 /home/tomServer/vendor/illuminate/database/Connection.php(725): Illuminate\Database\Connection->tryAgainIfCausedByLostConnection()
#2 /home/tomServer/vendor/illuminate/database/Connection.php(634): Illuminate\Database\Connection->handleQueryException()
#3 /home/tomServer/vendor/illuminate/database/Connection.php(339): Illuminate\Database\Connection->run()
#4 /home/tomServer/vendor/illuminate/database/Query/Builder.php(2260): Illuminate\Database\Connection->select()
#5 /home/tomServer/vendor/illuminate/database/Query/Builder.php(2248): Illuminate\Database\Query\Builder->runSelect()
#6 /home/tomServer/vendor/illuminate/database/Query/Builder.php(2743): Illuminate\Database\Query\Builder->Illuminate\Database\Query\{closure}()
#7 /home/tomServer/vendor/illuminate/database/Query/Builder.php(2249): Illuminate\Database\Query\Builder->onceWithColumns()
#8 /home/tomServer/app/api/controller/UserLogin.php(19): Illuminate\Database\Query\Builder->get()
#9 /home/tomServer/vendor/workerman/webman-framework/src/App.php(168): app\api\controller\UserLogin->view()
#10 /home/tomServer/vendor/workerman/workerman/Connection/TcpConnection.php(660): Webman\App->onMessage()
#11 [internal function]: Workerman\Connection\TcpConnection->baseRead()
#12 /home/tomServer/vendor/workerman/workerman/Events/Event.php(195): EventBase->loop()
#13 /home/tomServer/vendor/workerman/workerman/Worker.php(2412): Workerman\Events\Event->loop()
#14 /home/tomServer/vendor/workerman/workerman/Worker.php(1537): Workerman\Worker->run()
#15 /home/tomServer/vendor/workerman/workerman/Worker.php(1367): Workerman\Worker::forkOneWorkerForLinux()
#16 /home/tomServer/vendor/workerman/workerman/Worker.php(1341): Workerman\Worker::forkWorkersForLinux()
#17 /home/tomServer/vendor/workerman/workerman/Worker.php(1674): Workerman\Worker::forkWorkers()
#18 /home/tomServer/vendor/workerman/workerman/Worker.php(1623): Workerman\Worker::monitorWorkersForLinux()
#19 /home/tomServer/vendor/workerman/workerman/Worker.php(544): Workerman\Worker::monitorWorkers()
#20 /home/tomServer/start.php(130): Workerman\Worker::runAll()
#21 {main}

webman数据库采用的是 illuminate/database。
composer require illuminate/database最新的代码,求W大佬指点问题在哪里,完全按手册操作的

4413 1 0
1个回答

six

连接被拒绝。mysql的 IP 或者 端口配置错了,或者mysql没启动。
还有一种可能,mysql监听的是127.0.0.1,外部无法连接,改成监听0.0.0.0,这样外部及本地127.0.0.1都可以连。

  • marsColin 2020-09-02

    感谢回复,我昨晚洗完澡出来看了会发现既然配置文件是一个数组,那个ENV莫名其妙的放在哪里也不知道干什么的,我果断去掉就好了,
    'default' => 'mysql',
    'connections' => [
    'mysql' => [
    'driver' => 'mysql',
    'host' => '192.168.1.15',
    'port' => '3306',
    'database' => 'tomdb',
    'username' => 'root',
    'password' => '123456',
    'unix_socket' => '',,
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
    ],
    ],
    把配置文件改成这样就好了,每次被一些问题阻塞很久了,洗个澡出来就好了

年代过于久远,无法发表回答
×
🔝