send local host name to a MYSQL server
-
Hi,
I would have a question about remote connections to a MYSQL server using Qt.
My question is about how to handle the host name defined on my computer in order to send it to the MYSQL server.
I’m probably taking the wrong direction as I’m a debutante but I’ll like to understand better and I couldn’t find out any solution so far.
Here’s the standard code pattern I currently use for my remote connexion :
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName(<my_ip_address>); db.setPort(<port_number>); db.setUserName(<user>); db.setPassword(<passwd>); db.setDatabaseName(<my_database_name>);
I had first a failure message saying that the localhost from my computer on which Qt is running (which is not called localhost in my case) was unknown to my MYSQL server :
« Access denied for user …@... ».To bypass that problem, I gave the host name associated with a user name to MYSQL with sufficient privileges and then it worked.
What I would like to do now, is to be able to establish a connection to the remote database with a specific set of « user / host » that already exist with permissions in my database (so that from any computer with the same Qt program I would be able to reach the database).
If I know how to indicate the local user name (with setUserName()), I don’t how I could indicate the local host name. I couldn’t find any method to do that.
Could anyone tell me if what I’m trying to do is possible? If so, is the code I use suited for that?
Thanks for your help
-
Hi and welcome to devnet,
Just to be sure I understand you correctly, are you trying to connect to a MySQL database that is running on the same computer as your application ? If so, set the hostname to
localhost
and you should be good to go. -
@SGaist : thanks for you reply!
No, I was not clear. I've got a MYSQL server installed on a remote machine (on a NAS).
From my computer under Qt, I try to connect to that server. I give the ip address of the server (the ip address of the NAS) through setHostName().
If I don't specify the user name trough setUserName(), by default Qt picks up the user name of my mac session (the one I can see when I use the Terminal before the prompt) and this is the same for the host name value.
I can "force" the user name with setUserName() but I'm not able to do it for the host name.
Let's say, I indicate setUserName('jim') and my local host name (which appears on the mac Terminal) is pc.home. I will have : access denied for the user 'jim'@'pc.home'.
If I give permissions under phpmyadmin to the user 'jim' associated to the host 'pc.home', it works but what I'd like to do is to indicate under Qt a 'user / host' that already exists in my MYSQL server. This way, my Qt program would use the same user "user / host" (and not the one related to the computer session) when running on a different computer.
Hope this is clearer.
-
I just make a rectification :
If I don't set the user name with setUserName(), Qt will not pick up the one of my mac session (but something that appears to be random). But that doesn't change my problem : when I indicate the ip address of my NAS on which is my MYSQL server with setHostName(), the host name value that appears on the failure output is the one of my session (the one on my mac Terminal) : access denied for user ...@<host name>
-
you could set the whole connection string in one go with QSqlDatabase::setDatabaseName to set exactly what you need
-
Sorry for my late answer.
I really strived trying to handle the connection string for QSqlDatabase::setDatabaseName. I would probably need more practice and reading to go through with it.
I finally found a solution that suits me but it's probably not very nice (don't know what are the "standards") :
I created a new user on my MYSQL server and I chose '%' as the client to indicate all possible values of client associated to the user name, which makes that I don't need anymore to set this parameter under Qt.
-
I'm just realizing that what I was trying to do was maybe nonsense : I was trying to pass on to the mysql server a value for the client on which my Qt program is running i.e. I was trying to "deceive" the server. And I could understand then that there would be no such method in QSqlDatabase (and that you couldn't understand my question initially).
Could you confirm my reasoning please?