没有不值得去解决的问题,也没有不值得去学习的技术!

Resolving the SQLSTATE[HY000] [2002] Connection Timed Out Error in CakePHP v2.6.4

In CakePHP v2.6.4, the error SQLSTATE[HY000] [2002] Connection timed out occurs.

1. In CakePHP v2.6.4, the error SQLSTATE[HY000] [2002] Connection timed out occurs. See Figure 1.

In CakePHP v2.6.4, the error SQLSTATE[HY000] [2002] Connection timed out occurs.
Figure 1
Plaintext
2020-08-19 13:28:45 Error: [MissingConnectionException] Database connection "Mysql" is missing, or could not be created.
Exception Attributes: array (
  'class' => 'Mysql',
  'message' => 'SQLSTATE[HY000] [2002] Connection timed out',
  'enabled' => true,
)
Request URL: /?src=fly&username=test0819a&tenantid=2de4ad2e76a6d76ee2368b1c93cec41a&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0ZW5hbnRJRCI6IjJkZTRhZDJlNzZhNmQ3NmVlMjM2OGIxYzkzY2VjNDFhIiwidXNlcklEIjoiMiIsInVzZXJOYW1lIjoidGVzdDA4MTlhIiwiZ3JvdXBJRCI6bnVsbCwiZ3JvdXBDb2RlIjpudWxsLCJ0b2tlblR5cGUiOiJzc28iLCJyZWZyZXNoVG9rZW4iOiI1RjNDQjg2RTc5RjQ0RDFFOTk2MjAxOTkwQTJBMTFDNSJ9.m37jbpZ8uXc0uO5r7k8dAxA8DrEIL3lKTmyy_fv-FJA&uid=2&status=1
Stack Trace:
#0 /mcloud/creditshop/lib/Cake/Model/Datasource/DboSource.php(260): Mysql->connect()
#1 /mcloud/creditshop/lib/Cake/Model/ConnectionManager.php(105): DboSource->__construct(Array)
#2 /mcloud/creditshop/app/Model/AppModel.php(125): ConnectionManager::getDataSource('tmp')
#3 /mcloud/creditshop/lib/Cake/Model/Model.php(1155): AppModel->setDataSource('default')
#4 /mcloud/creditshop/lib/Cake/Model/Model.php(3644): Model->setSource('products')
#5 /mcloud/creditshop/lib/Cake/Model/Model.php(3022): Model->getDataSource()
#6 /mcloud/creditshop/lib/Cake/Model/Model.php(2994): Model->_readDataSource('all', Array)
#7 /mcloud/creditshop/lib/Cake/Controller/Component/PaginatorComponent.php(195): Model->find('all', Array)
#8 /mcloud/creditshop/lib/Cake/Controller/Controller.php(1086): PaginatorComponent->paginate(NULL, Array, Array)
#9 /mcloud/creditshop/app/Controller/ProductsController.php(115): Controller->paginate()
#10 [internal function]: ProductsController->index()
#11 /mcloud/creditshop/lib/Cake/Controller/Controller.php(490): ReflectionMethod->invokeArgs(Object(ProductsController), Array)
#12 /mcloud/creditshop/lib/Cake/Routing/Dispatcher.php(193): Controller->invokeAction(Object(CakeRequest))
#13 /mcloud/creditshop/lib/Cake/Routing/Dispatcher.php(167): Dispatcher->_invoke(Object(ProductsController), Object(CakeRequest))
#14 /mcloud/creditshop/app/webroot/index.php(117): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#15 {main}

2. I printed the database connection information and found that the host address is an internal IP, which the container cannot access.

Plaintext

stdClass Object
(
    [tmp] => Array
        (
            [datasource] => Database/Mysql
            [persistent] => 
            [host] => 192.168.150.23
            [login] => contenttest
            [password] => xxxxxxxx
            [database] => creditnew_2de4ad2e76a6d76ee2368b1c93cec41a
            [port] => 3306
            [prefix] => operation_
            [encoding] => utf8
        )

)

3. After changing it to an external IP, I printed the database connection information, and the container can now access it normally. See Figure 2.

After changing it to an external IP, I printed the database connection information, and the container can now access it normally.
Figure 2
Plaintext

stdClass Object
(
    [tmp] => Array
        (
            [datasource] => Database/Mysql
            [persistent] => 
            [host] => xxxxxxxxxxx.bj.tencentcdb.com
            [login] => contenttest
            [password] => xxxxxxxx
            [database] => creditnew_2de4ad2e76a6d76ee2368b1c93cec41a
            [port] => 10132
            [prefix] => operation_
            [encoding] => utf8
        )

)

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系