WordPress error establishing a database connection when clone to test subdomain

Solution:

Are you trying to use the same database for two WordPress installations? The main WordPress and the WordPress on subdomain?

Create a new database and note the name, username of the user associated with the database with all privileges and the password of the user. Take a backup of your main database and import the backup in your new database.

Then in your new database, find wp_options and change site_url and home_url to your subdomain.

Then as @F5 Buddy said, change the following in your config.php

define('DB_NAME', 'wordpress');
define('DB_USER', 'root');
define('DB_PASSWORD', '');

Try and access the admin panel by going to subdomain/wp-admin. See if that works. Login using your main WordPress username and password. Once logged into the admin panel, don’t forget to refresh the Permalinks by going to Settings -> Permalinks and clicking on Save; otherwise only your homepage will be linked correctly.

Hope this helps.