Yes you can do it.
First of all your nextcloud server should be able to speak with the distant mysql instance.
Where is your distant mysql instance ? Inside the same LAN than the nextcloud server ?
It’s quite dangerous to talk with an over the lan mysql instance if it’s not behind a VPN.
When you start the install wizard of nextcloud you will have to write where the mysql server is :
Address : localhost or 192.168.1.2(for example) or 52.45.168.XXX
Login : mylogin
Password : my password
Port : 3306 (default) or something else