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
hellomysqlI 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 @SGaistEach 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-serverAnother WAY IS
For configuring Xampp for hosting
- Go to Your XAMPP Control panel
- Click on apache > config > Apache (httpd.conf)
- 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)
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.
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 🤘🤘
-
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 networkExample;
I have computer where mysql is hosted and other peoples connected on the same ROUTER can access itWhich 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.
-
@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. -
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 -
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-serverit 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 ? -
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.
-
@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.
-
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 bitTo 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 on192.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. -
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.
-
Yup, But main thing is I am not able to communicate to server.
I have hosted it successfully. But can't connect to serverI can go to
192.168.100.4:8080/phpmyadmin
and it shows as expected. But when I try to connect to database It fails
sayingdb.setDatabaseName("users"); db.setPort(8080); db.setHostName("192.168.100.4"); db.setUserName("root"); db.setPassword("");
What do you think the problem is?
-
@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
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 youWill 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? -
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.
-
@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.
-
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 ?
-
@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.