How to modify MySQL in Nextcloud snap

I ran into an issue with an Nextcloud user account that needed Two Factor Authentication, despite the fact it was turned of in Nextcloud -> Security -> Two Factor Authentication.

In other words: to gain acces for this user I need to use a secondary device that he/she is logged into, but there isn’t any secondary device. The only way to solve this is to modify the MySQl tables directly, which takes us to the second challenge: Using Nextcloud -Snap for setting up nextcloud can’t be accessed directly from the Ubuntu linux command prompt using mysql -u root -p.

To make this work, you need to use the included cli-utility from nextcloud-snap:

sudo nextcloud.mysql-client -S /tmp/sockets/mysql.sock -u <db_user> -p 

and make sure you replace <db_user> with your own database account.

! If you do not know the username and password you should refer to the config.php file in your nextcloud setup dir. These values can be found using:

sudo nano /var/snap/nextcloud/current/nextcloud/config/config.php

and copy the database, username & password from:

....
  'dbname' => '<your dbname>',
  'dbhost' => 'localhost:/tmp/sockets/mysql.sock',
  'dbport' => '',
  'dbtableprefix' => 'tbl_',
  'mysql.utf8mb4' => true,
  'dbuser' => '<your db_user>',
  'dbpassword' => '<your db-passwd>',
  'installed' => true,
....

After login in with the sudo nextcloud.mysql-client -S /tmp/sockets/mysql.sock -u <db_user> -p  statement you do the following to select the nextclouddatabase, determine the right uid and update the enabled value:

USE <nextclouddatabase>;
SELECT * FROM oc_twofactor_providers;
UPDATE oc_twofactor_providers SET enabled='0' WHERE uid = '<UID>';

You can check if the value has been updated using the previous select statement. ! Little tip: using the ‘up’ arrow enables you to repeat previous SQL statements and saves you a lot of typing.

After finishing you can exit the mysql session using:

exit;

Sources:

https://help.nextcloud.com/t/how-to-disable-2fa-two-factor-auth/101381

https://help.nextcloud.com/t/cant-connect-to-mysql/51405