instead of inputting localhost, input the IP address of the server. You should be able to connect to it. If you still cannot connect, log into the server side and login to mysql as root and use this command.
GRANT ALL PRIVILEGES ON db_name.* username@'%';
Then try logging in from the client side, this should work now
using mysql -u username -p