0

AmfPHP 2.0 running on Nginx and PHP 7.0 (FPM), PDO is used as database.

As many users log in to the game, the CPU increases immediately to 100% on all 4 cores due to MySQL. If there are fewer players, the process calms down again and the CPU drops.

Code snippet of PDO connection:

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);
        }
    }

}

For further questions I am at any time available.

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
Robin
  • 11
  • 8
  • Are you closing your connections to your database? – Hackerman May 15 '17 at 21:31
  • You should provide some code snippet of your PDO connection. – Pinke Helga May 15 '17 at 21:37
  • @Hackerman No, but according to `show status where variable_name = 'Threads_connected'` it's closing the connection by itself. Code snippet: [Click](https://pastebin.com/QKGd9M6R) – Robin May 15 '17 at 21:59
  • You could reduce overhead on establishing connections by using persistent connections. Be aware of some issues, see [question about disadvandages](https://stackoverflow.com/questions/3332074/what-are-the-disadvantages-of-using-persistent-connection-in-pdo) Especially read the comment by Walter Tross to the accepted answer. – Pinke Helga May 16 '17 at 00:15
  • There's really not enough info here to address your problem. First you need to see what MySQL is doing to bring the CPU up so high. Just the connections is likely not the cause. – drew010 May 16 '17 at 00:15

0 Answers0