Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to connect database to online service



  • I have built simple application using MYSQL and it fetches data from mysql (CPANEL)

    Suppose
    The db name is
    ABC
    Password is
    PQRSTW123
    ADDRESS is
    168.498.1x.x
    Username is
    hellomysql

    I connected with this database using mysql Like simple querying

    In Cpanel we can access database with IP. So we should insert IP in that CPanel

    Wait, This is not the problem with CPanel

    Suppose I want to connect with that CPanel database every time although my IP changes

    In other languages(Just JS runtime environment) like NODE JS
    We can simply host it on somewhere fetch data from there and get data.(Here Just server is used, so one connection is OK)
    But In windows application I want to get the data from 10 computers and there is no any thing like SERVER. The application itself contains data to connect to database.

    So, Is there something like server in QT to serve all other QT application instead of making server like code in every application.

    LIKE

    // This is connection to connect to database from one machine
    
    // Again another connection from next machine
    

    Something like API
    I guess it would be very hard for me to code all things
    I would like to use my existing code

    
        {
            QSqlDatabase db  =  QSqlDatabase::addDatabase("QMYSQL","message_load");
            db.setDatabaseName(databaseName);
            db.setPort(databasePort);
            db.setHostName(databaseHostname);
            db.setUserName(databaseUsername);
            db.setPassword(databasePassword);
            int currentMessagesCount =0;
            if(db.open()){
                QSqlQuery query(db);
    
                if(query.exec(command)){
                    while (query.next()) {
                        QString id = query.value(0).toString();
                        QString sender = query.value(1).toString();
                        QString message = query.value(2).toString();
                        QString important = query.value(3).toString();
                        currentMessagesCount++;
                        if(important == "true"){
                            ui->messages->addItem(sender+"------important\n \t"+message);
                        }else{
                            ui->messages->addItem(sender+"\n \t"+message);
                        }
                    }
                }else{
                    QMessageBox::warning(this,"Second","second body"+query.lastError().text());
                }
    
            }else{
                QMessageBox::warning(this,"a","HELLO"+db.lastError().text());
            }
            if(totalMessages < currentMessagesCount){
                display.showTrayMessagesMessage(currentUsername);
            }
        }
        QSqlDatabase::removeDatabase("message_load");
    // This is simple code. Code is working properly 
    
    


  • This is Just answer compiled from all answers here.
    Special thanks to @mrjj and @SGaist

    Each app is a mysql client so each would have its own connection to the DB. a JS "app" would be running at a server and that server is the mysql client for all
    browsers showing the pages.
    Xampp is great but does your local server also need a web server?

    ONE WAY IS

    The easiest way to have a server is a virtual one.
    One way is to use dockers.
    https://www.docker.com/
    and then download a predefined setup
    https://hub.docker.com/r/mysql/mysql-server

    Another WAY IS

    For configuring Xampp for hosting

    • Go to Your XAMPP Control panel
    • Click on apache > config > Apache (httpd.conf)
      9222520c-f3a4-43cb-bf56-c518e9b85cea-image.png
    • Search for Listen 80 and replace with Listen 8080
    • After that check your local ip using ipconfig command (cmd console)
    • Search for ServerName localhost:80 and replace with your local ip:8080 (ex.192.168.1.156:8080)
      After that open apache > config > Apache (httpd-xampp.conf)
      759b2983-88f8-4afd-9476-b9c43c20ef70-image.png
    Search for
    
       <Directory "C:/xampp/phpMyAdmin">
           AllowOverride AuthConfig
           **Require local**   Replace with   **Require all granted**
           ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
       </Directory>```
    
    • Go to xampp > config > click on service and port setting and change apache port 8080
    • restart xampp
    • then hit your IP:8080 (ex.192.168.1.156:8080) from another computer

    In any case, you have to take another thing into account, if you want to connect to your database through the internet, you should really consider implementing a proper service that will shield the database from the internet.

    6d90fbf9-7604-4f47-8a39-62ae625f1a47-image.png

    If You get this error Then You have got the privileges problem

    Create new privileges with All accessed or certain IP

    CREATE USER 'demo'@'%' IDENTIFIED VIA mysql_native_password USING '***';
    GRANT ALL PRIVILEGES ON *.* TO 'demo'@'%' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
    GRANT ALL PRIVILEGES ON `users`.* TO 'demo'@'%'; 
    

    try with port 3306.

    You are using port 8080 for
    db.setPort(8080);

    but is that not the port the web server uses ?
    so while 192.168.100.4:8080/phpmyadmin Works, that is the port the web server answers
    and not to mysql clients.

    Also make sure port 3306 is not blocked by windows firewall.

    • Will private IP change overtime.

    Yes. if done by the local DHCP server then yes unless some IT dude promised you
    it would not. You can be lucky that it will get the same IP for a long time.

    • If so there will be problem because the database won't be hosted if IP changes.

    well if the IP changes then the clients cant find the server anymore.

    Be Happy , You are done now 🤘🤘


  • Lifetime Qt Champion

    Hi
    Each app is a mysql client so each would have its own connection to the DB.

    a JS "app" would be running at a server and that server is the mysql client for all
    browsers showing the pages.

    So no, there is not some Qt server that can do the same as its not quite the same setup.



  • @mrjj
    Oh! thanks for the information. I am happy that you understood my problem.
    Do you know any database hosting providers which provides database hosting without any problems. It should be quite fast.

    OR
    Should I create host in local computer and serve it to other computers via local network

    Example;
    I have computer where mysql is hosted and other peoples connected on the same ROUTER can access it

    Which one would be best. I need it to be fast. So I guess second one will be fast and best alternative. Main drawback is we can't use from anywhere.


  • Lifetime Qt Champion

    @Thank-You
    Hi
    There are many that hosts mysql but I have not tried them.
    I heard their own service is super fast
    https://www.mysql.com/cloud/
    but also expensive.

    • Should I create host in local computer and serve it to other computers via local network

    If this is an option. Having the MQSQL within the network, from a security perspective and
    performance, it's way better than in the cloud.
    So yes if this is on the table, i would host locally. For safety, for speed, and for generally an easier setup.



  • @mrjj

    I watched tutorials on local hosting but hadn't done succesfully.
    I am thinking of using Xampp for this purpose.
    Do you have any idea how to host database locally. (although it is offtopic from the question)
    I would be very happy to know about that thing


  • Lifetime Qt Champion

    Hi
    Xampp is great but does your local server also need a web server?

    The easiest way to have a server is a virtual one.
    One way is to use dockers.
    https://www.docker.com/
    and then download a predefined setup
    https://hub.docker.com/r/mysql/mysql-server

    it would then be able to run anyware/any pc. Do not, learning to use Dockers takes
    a day or so but in the end its not that complicated and once running it can live forever.
    Even the pc that plays server dies. Simply run it on some other pc.

    However, what is the setup?
    Do you have an real server already or you must provide one ?



  • @mrjj

    One way is to use dockers.

    I am somewhat aware about it. But main thing is
    My computer doesn't support it. I don't have virtualization in this PC.(So can't learn here) I will get new laptop within 2 months I guess. Currently It runs in Intel Dual Core 2.

    Even the pc that plays server dies.

    I am sure there will be no problem with it.
    I will run Xampp in the client machine. It runs pretty well in my machine. So that speed is sufficient for me.
    It will run in about 12 machines.

    However, what is the setup?
    Do you have an real server already or you must provide one ?

    No just windows machine. Nothing specific. If we need more user. They can give me different PC for hosting database(may be after using it for 2 months). So Xampp is best I guess for now.


  • Lifetime Qt Champion

    @Thank-You said in How to connect database to online service:

    Intel Dual Core 2.

    Wow thats a pretty old one.
    As far as I can recall, you can run virtual box on it 32 bit.
    but 64 bit demands VT-x.

    However, if Xampp works fine, i would just go with that. 12 clients is not that much.



  • @mrjj

    As far as I can recall, you can run virtual box on it 32 bit.

    Wow, Really I didn't know about that.

    Do you have any idea how can I do that?😂😂


  • Lifetime Qt Champion

    @Thank-You

    Well if you download virtual box and
    create a new virtual machine for 32-bit linux.
    Not all distros have 32 bit anymore.

    then
    From the VirtualBox Manager, find the Enable Nested VT-x/AMD-V check box on the Processor tab. To disable the feature, deselect the check box.

    then you can run most 32-bit linux or windows.
    just make sure mysql or Xampp is 32 bit

    To fast test if it still work you can grab a 32 bit win 7 here and test with
    https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/



  • @mrjj
    Hello Sir,
    I have finalized my thoughts and thought to use Xampp.
    I am stuck in small problem.
    It's middle night here. But still I can't find any solutions of it.(Almost searched about this topic for 5-6 hours)

    I have hosted on 192.168.100.8 But I want to make it fixed that it won't change.
    Like just hosting database on 192.168.100.8 wherever I am connected In (local network, I should be able to access the port from another computer on the same network).
    How can I make it host to fixed private Ip in LAN at any time
    Or
    Will it change overtime If so how can I solve this problem.


  • Lifetime Qt Champion

    Hi,

    Use a proper DNS and name your server appropriately. Otherwise you have to make the address static.

    In any case, you have to take another thing into account, if you want to connect to your database through the internet, you should really consider implementing a proper service that will shield the database from the internet.



  • @SGaist

    Yup, But main thing is I am not able to communicate to server.
    I have hosted it successfully. But can't connect to server

    I can go to

    192.168.100.4:8080/phpmyadmin

    and it shows as expected. But when I try to connect to database It fails
    saying

    53a55e2f-2888-46f7-ba79-90f09606931c-image.png

       db.setDatabaseName("users");
       db.setPort(8080);
       db.setHostName("192.168.100.4");
       db.setUserName("root");
       db.setPassword("");
    

    What do you think the problem is?


  • Lifetime Qt Champion

    @Thank-You
    Hi
    try with port 3306.

    You are using port 8080 for
    db.setPort(8080);

    but is that not the port the web server uses ?
    so while 192.168.100.4:8080/phpmyadmin Works, that is the port the web server answers
    and not to mysql clients.

    Also make sure port 3306 is not blocked by windows firewall.

    https://dev.mysql.com/doc/mysql-port-reference/en/mysql-ports-reference-tables.html



  • @mrjj
    Yes, I actually got success after seeing this thing for almost day.

       db.setDatabaseName("users");
       db.setPort(9906);
    // It  is 9906 because I changed from 3306 to 9906 before some time due to some errors
       db.setHostName("192.168.100.4");
       db.setUserName("host");
       db.setPassword("");
    

    I first did what you said

    changed to 9906(port)

    And I got the error
    322a9fd6-006e-401a-8c2e-4e15d0675ce2-image.png

    Then I created new privileges to all address

    Now It's finally done!

    Ok I made it work but Are there security risks in this situation(simple only) ?
    Another question for you

    Will private IP change overtime.

    If so there will be problem because the database won't be hosted if IP changes.
    Am I right?. Or
    It will be hosted on the same port?


  • Lifetime Qt Champion

    Hi

    • Ok I made it work but Are there security risks in this situation(simple only) ?

    Depends on what you did. if you allowed this port in the firewall on the internal network/pc
    its ok. That's how one normally allows new traffic on a port.

    • Will private IP change overtime.

    Yes. if done by the local DHCP server then yes unless some IT dude promised you
    it would not. You can be lucky that it will get the same IP for a long time.

    • If so there will be problem because the database won't be hosted if IP changes.

    well if the IP changes then the clients cant find the server anymore.

    But I don't know your LAN. if it's not a home setup then you most likely also have a DNS server and that means you can use a name instead of an IP. the DNS server will then translate the name to the Ip and hence
    the actual IP is no longer important.

    • It will be hosted on the same port?

    Yes unless you change it yourself, port remains the same.



  • @mrjj

    if it's not a home setup then you most likely also have a DNS server

    It's home server. A router that we use for normal purpose.
    With this I want to host this database to other users connected to the same router.


  • Lifetime Qt Champion

    @Thank-You said in How to connect database to online service:

    A router that we use for normal purpose

    Depending on the router you can assign static IPs to network devices.


  • Lifetime Qt Champion

    It's home server. A router that we use for normal purpose.

    Ok. you can most likely make it assign a fixed ip to the server so it never changes.
    If it cant directly, you can often change the range of ips it will give out and
    then use an ip outside of this range and hence keep it.

    • With this I want to host this database to other users connected to the same router.

    Wait ? from outside ? Like from the internet ?


  • Lifetime Qt Champion

    @Thank-You said in How to connect database to online service:

    Then I created new privileges to all address

    Depending on how you did that you allowed user from anywhere to connect to your database which might not be what you want.



  • @SGaist

    PRIVILEGES ON *.* FROM 'host'@'%';
    

    It is shown while I was updating
    I am sorry I did it with PHPMYADMIN So I don't know syntax much of this thing

    CREATE USER 'demo'@'%' IDENTIFIED VIA mysql_native_password USING '***';
    GRANT ALL PRIVILEGES ON *.* TO 'demo'@'%' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
    GRANT ALL PRIVILEGES ON `users`.* TO 'demo'@'%'; 
    

    I recreated another one for showing.



  • @mrjj said in How to connect database to online service:

    Wait ? from outside ? Like from the internet ?

    No, Just within the network only.
    Like how you use WIFI. (Available to only peoples in the LOCAL NETWORK)

    If you didn't understand this,
    Let me give an example

    Suppose I am connected to the WIFI(router). And you are also connected to the same wifi. I have hosted the database. You can use this database only if you are connected to the same wifi/router that I have hosted into. You are not allowed to access this outside of this network. If you get disconnected with network I won't be able to connect with it.

    I hope you understands


  • Lifetime Qt Champion

    @Thank-You
    Ah
    Ok super. That will work fine then.
    Yes I understand fully :)

    If you know the 10 clients pc. you can take their mac address and
    tell the router to only allow those MAC addresses to the MySQL port.
    That will block all others-



  • @mrjj

    well if the IP changes then the clients cant find the server anymore.

    As you said this, So I allowed every IP. It can change at anytime

    72a3c95b-d3f1-4d7c-b2c0-c4cf034b836a-image.png

    tell the router to only allow those MAC addresses to the MySQL port.

    I didn't found any place to write MAC address here.

    Some people on the network (other that people accessing the database can still access the internet and any things)

    OR
    Do you mean to block other MAC address(other than 10)



  • @mrjj

    tell the router to only allow those MAC addresses to the MySQL port.

    Oh! maybe I didn't understand it.

    Do you mean giving access to certain mac address in ROUTER itself?

    Something like these
    https://www.snbforums.com/threads/how-to-restrict-open-port-access-to-certain-mac-addresses.7528/
    https://community.cisco.com/t5/switching/limit-ports-access-for-particular-mac-addresses-only/td-p/723845


  • Lifetime Qt Champion

    @Thank-You
    Hi
    Yes in the router.
    so only the approved one can access the server. and not anyone on the same network.
    If it makes sense ofc.



  • @mrjj
    If I use only one router I will do as you said
    I am thinking of using new router just for this purpose. And giving connection to only very specific device. So it will be faster and easier also.



  • This is Just answer compiled from all answers here.
    Special thanks to @mrjj and @SGaist

    Each app is a mysql client so each would have its own connection to the DB. a JS "app" would be running at a server and that server is the mysql client for all
    browsers showing the pages.
    Xampp is great but does your local server also need a web server?

    ONE WAY IS

    The easiest way to have a server is a virtual one.
    One way is to use dockers.
    https://www.docker.com/
    and then download a predefined setup
    https://hub.docker.com/r/mysql/mysql-server

    Another WAY IS

    For configuring Xampp for hosting

    • Go to Your XAMPP Control panel
    • Click on apache > config > Apache (httpd.conf)
      9222520c-f3a4-43cb-bf56-c518e9b85cea-image.png
    • Search for Listen 80 and replace with Listen 8080
    • After that check your local ip using ipconfig command (cmd console)
    • Search for ServerName localhost:80 and replace with your local ip:8080 (ex.192.168.1.156:8080)
      After that open apache > config > Apache (httpd-xampp.conf)
      759b2983-88f8-4afd-9476-b9c43c20ef70-image.png
    Search for
    
       <Directory "C:/xampp/phpMyAdmin">
           AllowOverride AuthConfig
           **Require local**   Replace with   **Require all granted**
           ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
       </Directory>```
    
    • Go to xampp > config > click on service and port setting and change apache port 8080
    • restart xampp
    • then hit your IP:8080 (ex.192.168.1.156:8080) from another computer

    In any case, you have to take another thing into account, if you want to connect to your database through the internet, you should really consider implementing a proper service that will shield the database from the internet.

    6d90fbf9-7604-4f47-8a39-62ae625f1a47-image.png

    If You get this error Then You have got the privileges problem

    Create new privileges with All accessed or certain IP

    CREATE USER 'demo'@'%' IDENTIFIED VIA mysql_native_password USING '***';
    GRANT ALL PRIVILEGES ON *.* TO 'demo'@'%' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
    GRANT ALL PRIVILEGES ON `users`.* TO 'demo'@'%'; 
    

    try with port 3306.

    You are using port 8080 for
    db.setPort(8080);

    but is that not the port the web server uses ?
    so while 192.168.100.4:8080/phpmyadmin Works, that is the port the web server answers
    and not to mysql clients.

    Also make sure port 3306 is not blocked by windows firewall.

    • Will private IP change overtime.

    Yes. if done by the local DHCP server then yes unless some IT dude promised you
    it would not. You can be lucky that it will get the same IP for a long time.

    • If so there will be problem because the database won't be hosted if IP changes.

    well if the IP changes then the clients cant find the server anymore.

    Be Happy , You are done now 🤘🤘


Log in to reply