Connect to a database from inside or outside the network.

  • I've set up a server in my office. It's working fine and I can connect to the database remotely through the Qt application that I'm building.

    From inside our office network, I can't see the website - or connect to the database - by using the domain name or external IP address. I need a way for the application to use either the internal or the external IP address to connect to the database, depending on the situation. I tried simply doing this:

    @QSqlDatabase cdb = QSqlDatabase::addDatabase ("QMYSQL");
    cdb.setHostName ("");
    cdb.setDatabaseName ("dbname");
    cdb.setUserName ("uname");
    cdb.setPassword ("password");
    if (!
    qDebug() << "Failed to connect to root mysql admin";
    if (!
    qDebug() << "Still no good";


    This worked, but it took time. It seems that when the application starts, it will take a short while to try to connect, then when I do something else it takes that time again. This could be something to do with my bad coding, but I'm not sure. How often would you normally use this function? When I started writing this, I thought I'd only need to connect once, but I seemed to lose the connection after a while, so now I have some other functions calling this function. Is this the right way to go about it or is there a simple way to get a persistent connection?

    That's quite a few questions. Thanks in advance for any help.

  • To solve this problem, you should ask your IT admins to set up a proper internal DNS that resolves to the correct IP addresses.

    In order to circumvent the problem in the time being: Do a check on application startup and save the hostname that works in a variable. In your case, the check is done every time you try to open the database. There's no need to check over and over again.

    To speed up things even further, you can store the hostname in the preferences, using [[Doc:QSettings]]. This is only useful, if you do not have your computer moving inside and outside the network (e.g. a laptop), though.

  • Thanks for the reply.

    Unfortunately, the 'IT admins' is me. (We're a small company). If you could point me to a way of doing that - setting up a proper internal DNS that resolves to the correct IP addresses - I'd appreciate it.

    Your suggestion of saving the 'good' hostname as a variable sounds good right now. I'm a bit of a beginner with Qt though, so I'm not sure how to set a variable that will persist. Again, if you could point me to that, I'd appreciate it.

    The QSettings idea wouldn't work so well for me because about half of the computers that will be using the application laptops moving in and out of the network.

    Thanks for your help.

  • For the DNS: setup a second name server that is used internally only. This one would deliver the private IPs for the names.

    Setting that variable is nothing Qt special, it seems that you're in need of some C++ introduction.

    Anyways, just add a QString to your main class. I'm sorry I can't give you a more detailed advice, because I don't know the overall design of your application.

  • You're right about needing a C++ introduction. I have read things, but the best way for me to learn it is to do it, so that's what I'm doing.

    I'll see what I can do about both of these issues.

    Thanks for your help.

  • You're welcome. The wiki and some older threads have pointers to good introductions to C++ in general and Qt. And of course feel free to ask questions here. We do have a dedicated "C++ Gurus forum": too, that's for the C++ questions that are not directly Qt related.

  • If you don't want to set-up a full DNS just place the internal ip in the hosts file of your computer when you are within the local network. Then your application will be able to connect always using the domain name of your server. When you are outside the network, comment out such line.

  • Yes, I'd seen that solution, but there are about 6 machines that come and go every day. I can't ask people to keep changing their host file. Good news - I've found some good software called "Simple DNS Plus" which seems to do the job. The only problem with this is that I'm using no-ip because I have a dynamic address. When my address changes, I'll need to update the configuration of Simple DNS Plus. In theory, I should be able to do this in a similar way to how no-ip works. If I can run a service that checks my external IP address and updates the config file of the software, I should be OK. I'm having problems now trying to use QHostInfo to get the IP address associated with my domain name. If you have any pointers there, that would be appreciated.

  • It seems to me that you are implementing a solution that is much more complex than fixing the problem (i.e., placing a DNS system that works). Anyway, depending on the system you are using (unix? linux? windows? mac?) it should not be difficult to make a script that changes your machine configuration to connect to the database. I will not change the application configuration.

    By the way, a good and quick solution could be to enable port forwarding on the LAN side of your gateway (local network) so that connecting to a certain port will redirect to the database machine. The same on the external side will give you a stable configuration.

  • Just setup a DNS server that is only used internally and delivers the internal, private IPs (eg. 192.168.x.x). This one works independently from the DNS that serves the rest of the world. Your internal DNS would forward everything else than your internal names to another name server. That's a reliable solution which is easy to setup and does not need any dynamic updates.

  • Thank you both for your input.

    I did lots of reading and testing of different DNS servers, and to be honest, a lot of it went over my head. The 'Simple' in the name of this one I'm trying to use was quite attractive.

    I realise that I could simply ask our staff to use 192.168.x.x to see the site inside the network, and I could use Volker's earlier idea of using a variable in the application to connect to the database at the right address - internal IP or domain name. I could do that, but I think it would confuse people. Maybe that's not a good reason though.

    Both of your ideas on how to fix this easily sound good, but I'm really not sure how I would actually implement them. Sorry to sound a bit useless.

    Right now I'm thinking that I might stick with my plan of updating the configuration file of the DNS server when the dynamic IP changes. I realised I can move the config file to somewhere within the web server, change the file with PHP (which I have a lot of experience with) and update the server by a command line command. (It's Windows by the way).

    I would love to do this a simple way, but as I said, I'm not really understanding the simple way.

    Thanks for your help.

Log in to reply