"Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2006] MySQL server has gone away"

I may have missed it but have you tried the following without specifying the port/socket:

...
'dbtype' => 'mysql',
'dbhost' => 'localhost',
'dbname' => 'nextcloud',
'dbuser' => 'root',
'dbpassword' => '',
'dbtableprefix' => 'oc_',
...

Looking at the port you are using, it’s the standard one anyway and you are on localhost, so it shouldn’t be necessary to specify the port.