I've been recently learning how databases work and how to write a service that can operate the database. So the situation is following:
I have a MySQL database ran on a localhost server (XAMPP). Accessing the database is simple: localhost:3036/my_database.
In addition to that, I wrote a Java Spring service (actually just copied the sample from https://github.com/spring-guides/gs-accessing-data-mysql.git) that is supposed to be an interface accepting curl requests and based on them to operate the database. So far so good. Everything works. The problem is that this service also runs on a localhost address: localhost:8080.
I would like to implement the MySQL server into my Spring service so that I can just run this service and the database will run and be hosted on the same port. The data is supposed to be available only through this service so I don't want an additional independent MySQL server to be running on a different localhost port.
At the end of the day, I would like to generate an executable jar file that when ran provides me the service on a localhost address and deals with locally stored database seamlessly.
Edit:
I wouldn't like my database to be independent form the service. I don't want to take care of running it and connecting to the service. What I'm searching for is sommething like #jr593 mentioned in the answers below, an embedded database. But is it possible for such databases to save the data locally on the device that the service is running beetwen service runs?
There is a few solution for this.
Change port for java app. It it's Spring Boot you could check property server.port and set it to e.g 8081.
You can chcek what is working on XAMPP on port 8080 and shoud down it (it could be apache or something else)
You don't need XAMPP to have MySQL on local host. You can stop/remove xampp and install standalone MySql, so you will take only port 3036 and nat 8080 and 3036.
You could use Docker with MySql image instead of XAMPP.
Related
I am trying to connect locally by using pgAdmin or sql developer to the remote database. Database is on GCP however I dont know what I am missing.
On the screenshot
It is configured in GCP as a Cloud SQL instance and the problem is that initially It did not have public IP only private IP. So I exposed one by following the instruction from: https://cloud.google.com/sql/docs/postgres/authorize-networks#authorized-networks
This is how currently looks like (I am using username and password from the application.properties of a java project that is in VM instance to connect to sqlDeveloper):
screen of the configuration of CloudSql
And the configuration of the sql developer:
sqlDeveloper setup
There is definitely something missing or I am doing it wrong.
Can someone help me with connecting locally to the database.
I am totally new to GCP. I am used to configuration files like in tomcat context.xml
If you haven't a public IP, you can't connect your instance from outside GCP. So you can't (easily) from your local environment.
I wrote an article on this. Have a look, you will understand what's the problem and how to solve it (the hard way!)
Currently, I have two GCE auto-scaled groups of servers, bootstrapped by the chef. 1st - redis servers (db), 2nd - java servers (app).
Any app server can talk to any db server. Every db need to be served by app server, and there should be no situation when one app server has connections to two separate db servers.
So, I need to figure out if I can connect newly created app-server with newly-created DB-server (all in the same network), using consul.
All in all, I need to automatically pair new up-scaled servers, by adding appropriate db server IP or hostname to the command, that starting java on the app server.
I'm very new to service discovery and stuff, so any help is greatly appreciated.
Answer after edit:
So if I understand you correctly, any new app server can talk to any new redis server, but once they picked a db server to talk to, they should stick with that server.
I can see a few ways to achieve that with consul:
Map each app server 1-to-1 to a redis server and expose each server as a different service in consul with domain names like: app1.service.consul, redis1.service.consul. The drawback here is that you can not scale your app servers independently from your redis servers.
Use Redis Sentinel and let it abstract the sharding of the data for you and the just expose it under one domain name in consul: redis.service.consul
I would suggest to look into the second option, since it allows you to scale your app and db servers independently.
Old answer:
It sounds like you have two services in your network: An app service and a db service. You would then typically make consul serve as the DNS server for both of them.
This can be achieved by creating a service file for each of them.
On the server where your app is running you would create a service file in /usr/local/etc/consul.d/my_app.json:
{
"service": {
"name": "my_app",
"port": 1234
}
}
Where you replace port with the port your app is listening on.
You then need to reload consul with consul reload. You can check that the changes where applied correctly by running consul monitor.
Your app should now be reachable from my_app.service.consul on your internal network.
You can check this by issuing a DNS query with dig my_app.service.consul. This should return the IP address of the app server in the ANSWER section of the DNS response.
You then have to repeat these steps on your database server where you need to create another service file for the database with the appropriate port and service name.
I made an inventory system in netbeans using the language java and i connect it to derby as my database it is already working as a whole system but I want to have a client-server functionality what I mean is I want my program to have a server which holds the database(different computer) and different users with different computers which they can save/view data at the same time to the database means they are connected in one network.
Working with multiple connections to a single database.
What code can I use or method or do I have to import something?
I did some research and the only thing that I found is socket which can be used to create a chat between server and a client.
But I only tried the IP 127.0.0.1 for the client since I am making running the server and the client in the same computer.
And also can I open the connection of the server in the client form and send data like SQLQuery so I can save it in the database of the server?
Where can I see examples for these? Please help thanks
Yes, Derby supports a client-server configuration of your application.
Deploying your application in the client-server configuration is straightforward:
Deploy the Derby Network Server, for example by running the startNetworkServer script that is included with the Derby distribution.
Ensure that derbyclient.jar is in your application's CLASSPATH, and that you register "org.apache.derby.jdbc.ClientDriver" with the JDBC DriverManager.
Change your JDBC Connection URL from jdbc:derby:<db details> to jdbc:derby://<host:port>/<db details>.
If this is your first time using Derby, I strongly recommend working your way through the Derby tutorial at https://db.apache.org/derby/docs/10.12/getstart/index.html
For more information about running the Derby Network Server to service database requests for your applications, read the Derby Admin Guide: https://db.apache.org/derby/docs/10.12/adminguide/index.html
Use the IP 0.0.0.0 or for all connections in the server. The the connection url should include the name of the server or the ip address of the server in the network. When you use ip 127.0.0.1 or localhost derby can only accept connections to the database in the same machine, in this case localhost. All of this can be done by your network application server
I have created a standalone application as a school project. The major problem we are encountering is that since it has Java as front-end and MySQL as back-end (compulsory), and we have created a database that solely belongs to one computer, we cannot run the same project on different machines because it won't have the required database, the tables, or the same username and password we used to connect to MySQL.
So my question is **How can I connect to MySQL server in different Machines? **
For database and Tables, I could run a sql file, but that will happen when I would be connected to the MySQL server. Also I am developing the project at my home computer, and I want to run the project on different computers who are connected to my computer by no means .
You a following options
Shared drive : Attach database stored on remote shared drive to a local SQL Server read here
Connect to remote SQL Server instance from local computer - better if they are in same LAN - Steps here
If you want to package DB with app where you have predefined data and you dont want to save transactional data - use inMemory DB. They will load when you application starts up.
Already your application can work using different machine.
You have only to change the localhost string in your connection string with the IP of the MySQL machine :
DriverManager.getConnection("jdbc:mysql://localhost/database?"+ "user=sqluser&password=sqluserpw");
And make sure that remote access are enabled in your MySQL configuration. You can find more about it here.
Instead of using localhost in your database connection string, you should use the ip or aname of the database host
e.g.
jdbc:mysql://IP:3306/db?user=user&password=password
jdbc:mysql://A-NAME/db?user=user&password=password
Just make sure that port 3306 is open and that you have access on the system the databasse is hosted on
I'm using a scalable app on openshift and I'm trying to access its database from a local app but I am having trouble setting up the connection. I need to access my MySql 5.5 database.
IDE: Netbeans 7.4
I am using the environment variable values for the connection. I have no trouble connecting to it using my JBoss app in Openshift. Any help would be much appreciated. Sorry for the incomplete info. I am new here in SOF.
If you have the rhc client installed (see Client Tools Installation Guide if not), you can have it port forward to the server via rhc port-froward. Use localhost as the Host and port number it tells you, with the user name and password supplied from the application page.
Note this will only work when running locally, and the the rhc command will have to be running as well
See also 10.13. Port Forwarding