This answer has been edited several times based on new info provided by the OP
Is root actually allowed to connect to the server from the host that you are connecting from? If the error string is returning the canonical name of the server, there's a very good chance that 'localhost' is not pointing to 127.0.0.1 :
"Access denied for user
'root'@'MY.SERVER.HOST.NAME' to
database 'dbname'"
That should echo something like "Access denied for user 'root'@localhost'", not the name of the server.
Try:
$con = mysql_connect("127.0.0.1","root","pass");
Edit (After more information provided in comments)
If you are connecting from a totally different host, you have to tell MySQL user@remote_hostname_or_ip
is allowed to connect, and has appropriate privileges to create a database and users.
You can do this rather easily using phpmyadmin (on the MySQL server), or a query like:
CREATE USER 'root'@'192.168.1.1' IDENTIFIED BY PASSWORD 'secret';
GRANT ALL PRIVILEGES ON * . * TO 'root'@'192.168.1.1' IDENTIFIED BY PASSWORD 'secret' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
I would advise not naming this user 'root' , just create a user with all of the global privileges needed. In the example, I used 192.168.1.1, that could easily be a hostname, just make sure DNS is set up appropriately. Specify the host to match exactly as it appears in logs when you connect to the remote server.
You may also want to adjust limits to taste. More information on the CREATE USER
syntax can be found here, GRANT
here.
Edit
If using MySQL 4 - CREATE is not an option. You would just use GRANT (4.1 Docs On User Management)
Edit
If using C-Panel, just use the API. While yes, it does have its quirks, its easier to maintain stuff that uses it rather than ad-hoc work arounds. A lot of successful applications use it without issue. Like any other API, you need to stay on top of changes when using it.