Php Ratchet WebSocket连接到mysql数据库几个小时后出现错误-SQLSTATE[HY000]:一般错误:2006 mysql服务器已消失
目前,我已经在测试服务器上停靠并部署了我的webapp。在我的docker中,我正在使用mysql数据库运行Symfony4/React全堆栈web应用程序。另外,我正在运行另一个运行Ratchet websocket的容器。我的ratchet websocket从mysql数据库获取数据 在启动这些服务之后,我的webapp就可以完全正常工作但是,在空闲几个小时后,它开始出现以下错误。 SQLSTATE[HY000]:一般错误:2006 MySQL服务器已经离开Php Ratchet WebSocket连接到mysql数据库几个小时后出现错误-SQLSTATE[HY000]:一般错误:2006 mysql服务器已消失,php,mysql,docker,symfony,ratchet,Php,Mysql,Docker,Symfony,Ratchet,目前,我已经在测试服务器上停靠并部署了我的webapp。在我的docker中,我正在使用mysql数据库运行Symfony4/React全堆栈web应用程序。另外,我正在运行另一个运行Ratchet websocket的容器。我的ratchet websocket从mysql数据库获取数据 在启动这些服务之后,我的webapp就可以完全正常工作但是,在空闲几个小时后,它开始出现以下错误。 SQLSTATE[HY000]:一般错误:2006 MySQL服务器已经离开 web_1 |
web_1 | 202.21.101.226 - - [13/Dec/2019:08:39:50 +0000] "GET / HTTP/1.1" 200 1603 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
web_1 | - - 13/Dec/2019:08:39:50 +0000 "GET /index.php" 200
wss_1 | Client connected
wss_1 | Message Received
wss_1 | An exception occurred while executing 'SELECT t0.id AS id_1, t0.username AS username_2, t0.qrcode AS qrcode_3, t0.link AS link_4, t0.icon AS icon_5, t0.user_id AS user_id_6, t0.applink AS applink_7, t0.otp AS otp_8, t9.id AS id_10, t9.layout AS layout_11, t9.message AS message_12, t9.volume AS volume_13, t9.duration AS duration_14, t9.textcolor AS textcolor_15, t9.textsize AS textsize_16, t9.textweight AS textweight_17, t9.min_alert_donation AS min_alert_donation_18, t9.user_id AS user_id_19, t9.gif_id AS gif_id_20, t9.sound_id AS sound_id_21, t22.id AS id_23, t22.title AS title_24, t22.goal_amount AS goal_amount_25, t22.starting_amount AS starting_amount_26, t22.end_date AS end_date_27, t22.user_id AS user_id_28, t29.id AS id_30, t29.bg_color AS bg_color_31, t29.font_size AS font_size_32, t29.font_color AS font_color_33, t29.user_id AS user_id_34 FROM user t0 LEFT JOIN settings t9 ON t9.user_id = t0.id LEFT JOIN donation_goal_settings t22 ON t22.user_id = t0.id LEFT JOIN top_donator_settings t29 ON t29.user_id = t0.id WHERE t0.link = ? LIMIT 1' with params ["MTX6oPmIEKyDBVOFueRlhOi26WMWd1"]:
wss_1 |
web_1 | 202.21.101.226 - - [13/Dec/2019:08:39:51 +0000] "GET /wss HTTP/1.1" 101 4 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
wss_1 | SQLSTATE[HY000]: General error: 2006 MySQL server has gone awayConnection 699 has disconnected
web_1:这是我的全堆栈应用程序
wss_1:这是我的棘轮网套
我在phpMyAdmin上运行了SQL查询,结果显示“执行时发生异常”。它很好用
我不知道为什么我的websocket服务在空闲几个小时后开始出现这个错误
如有任何建议,将不胜感激!提前谢谢
编辑:下面的类处理我的websocket逻辑
class NotificationController implements MessageComponentInterface
{
protected $clients;
protected $container;
protected $objectManager;
public function __construct(ContainerInterface $container, ObjectManager $objectManager)
{
$this->container = $container;
$this->objectManager = $objectManager;
$this->clients = [];
}
/**
* A new websocket connection
*
* @param ConnectionInterface $conn
*/
public function onOpen(ConnectionInterface $conn)
{
echo "Client connected" . "\n";
}
/**
* Handle message sending
*
* @param ConnectionInterface $from
*/
public function onMessage(ConnectionInterface $from, $msg)
{
$myfile = fopen("./public/wsslog/log.txt", "w");
try{
$jsonData = json_decode($msg, true);
echo "Message Received\n";
if ($jsonData["request"] == "register") {
$user = $this->objectManager->getRepository(\App\Entity\User::class)->findOneBy(["link" => $jsonData["code"]]);
if($user){
$userId = $user->getUserId();
if(!isset($this->clients[$userId])){
$this->clients[$userId] = [$from];
}else{
array_push($this->clients[$userId], $from);
}
}else{
$from->close();
}
} else {
$from->close();
}
}catch(\Exception $e){
echo $e->getMessage();
fwrite($myfile, $e->getMessage()."\n");
$from->close();
}
}
/**
* A connection is closed
* @param ConnectionInterface $conn
*/
public function onClose(ConnectionInterface $conn)
{
$conn->close();
echo "Connection {$conn->resourceId} has disconnected\n";
}
/**
* Error handling
*
* @param ConnectionInterface $conn
* @param \Exception $e
*/
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$myfile = fopen("./public/wsslog/log.txt", "w");
fwrite($myfile, $e->getMessage()."\n");
$conn->close();
}
public function notifiedFromSymfony($jsonString)
{
$jsonData = json_decode($jsonString, true);
$userId = $jsonData["userId"];
if (isset($this->clients[$userId])) {
foreach($this->clients[$userId] as &$conn)
$conn->send(json_encode(["amount" => $jsonData["amount"], "username" => $jsonData["username"]]));
}
}
}
从mysql客户端到数据库的已打开连接不打算在几个小时内打开。一段时间后,客户端断开连接。我正在为web套接字客户端使用以下数据库包装器:
class Database {
private $pdo;
private $params;
public function __construct() {
$this->params = func_get_args();
$options = array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION);
if(!in_array($options, $this->params, true)) {
$this->params[] = $options;
}
$this->init();
}
public function __call($name, array $args) {
return call_user_func_array(array($this->pdo, $name), $args);
}
public function ping() {
try {
$this->pdo->query('SELECT 1');
}
catch (\PDOException $e) {
$this->init();
}
return true;
}
private function init() {
$class = new \ReflectionClass('PDO');
$this->pdo = null;
$this->pdo = $class->newInstanceArgs($this->params);
}
}
用法:
class WsClient {
private $db;
public function __construct($dsn, $user, $pwd) {
$this->db = $db = new Database($dsn, $user, $pwd);
}
public function doSomething() {
$this->db->ping();
//execute query...
}
}
是否需要为我的websocket逻辑创建数据库类?我不能只用ORM吗?你不必使用上面贴的包装器。只需在执行查询之前执行“ping”,如果您的框架返回与数据库连接相关的错误,请重新建立连接并执行查询。数据库服务器是否没有提供日志?所有容器是否仍在运行?请咨询Docker ps-a可能会出现一些错误down@NicoHaase不,很不幸。@James是的。所有的集装箱都装上了,这能回答你的问题吗?