You should make an API1 call:
Code:
https://example.com:2083/xml-api/cpanel?user=username&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduserdb&cpanel_xmlapi _apiversion=1&arg-0=dbname&arg-1=dbuser&arg-2=create,delete,lock
The above is an example of making an API1 call via the XML-API binary, specifically the adduserdb function which creates user grants for a mysql database; in this case the user 'dbuser' will get CREATE, DELETE, and LOCK TABLES for database 'dbname.'
The Mysql::* API1 functions are documented here:
ApiMysql < ApiDocs < TWiki
I you need to make the call to localhost, there's more efficient ways than making a an HTTP request; their outlined at docs.cpanel.net too. The forum should have more about that...and I help you too
Regards,
-Dave
PS. I highly recommend utilizing any functionality that is provided in regards to integration. cPanel does not always follow convention with regard to *nix sub-systems or the tools used to manage them. Add like any other software, can change it's methodologies at will. Therefore, if you start writing code that directly handles sub-systems, like the MySQL server, you may find your self in a rabbit hole; deep in a rabbit hole. However, having said that, I encourage you make cPanel work for you!
PSS. Like I stated before, cPanel handles somethings 'their own way' as you've touched on with the mysql root user. If you work with the various APIs you won't have to worry too much about that sort of thing provided you have valid credentials to the cPanel/WHM interface...or better the root hash access (which was designed for system-to-system integrations...it's found in WHM's "Main >> Cluster/Remote Access >> Setup Remote Access Key")