AmfPHP 2.0在Nginx和PHP 7.0(FPM)上运行,PDO用作数据库。
随着许多用户登录游戏,由于MySQL,CPU在所有4个核心上立即增加到100%。如果玩家较少,则该过程再次平静下来并且CPU会下降。
PDO连接的代码段:
class Database extends PDO {
public static function getConnection() {
$connectionString = sprintf("mysql:dbname=%s;host=%s;charset=utf8", MYSQL_DB, MYSQL_HOST);
try {
$pdo = new PDO($connectionString, MYSQL_USER, MYSQL_PASS);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $pdo;
} catch(PDOException $e) {
$error = date("Y-m-d g:i:s a T") . "\Database::getConnection\tError: (" . $e->getCode . ") " . $e->getMessage;
throw new Exception($error);
}
}
// Example
public static function Test($arg) {
try {
$pdo = Database::getConnection();
$stmt = $pdo->prepare("select * from xx where xx = :xx");
$stmt->bindParam(":xx", $arg, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($stmt->rowCount() > 0) {
// execute my code
}
} catch(PDOException $e) {
$error = date("Y-m-d g:i:s a T") . "\Database::Test\tError: (" . $e->getCode . ") " . $e->getMessage;
throw new Exception($error);
}
}
}
如有其他问题,我随时可以使用。