How to access & manipulate my MySQL database which is at different machine - java

I've made a Mysql db that i can easily manipulate with Java,
However I cant access it from other machines since its running off localhost only right? How would i go about making it so i would be able to manipulate the data from another machine. Or am i going about this all wrong? I've tried playing around with the remote access on MySQL workbench but I'm just not understanding it.
Here is how I'm accessing it currently.
Connection myConn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sampledatabase"
);

Of course you could access it remotely!
Firstly, you must understand what's a MySQL user and the authentication mechanism. MySQL treat 'UserName'#'Host' as a real user who can access to the MySQL server. When you access it locally the actual user you used is 'Me'#'localhost' (Assume the username here is 'Me'). And inside MySQL server there must be a user named 'Me'#'localhost' exist(or 'Me'#'%'). You must provide the right password for 'Me'#'localhost' as well.
When you need to access it remotely, you must replace localhost with the remote ip address so that your app could create the TCP connection correctly. Assume that the ip address of MySQL server is 172.0.1.5 and the ip address of your app is 172.0.1.10. The actual user you used now is 'Me'#'172.0.1.10'. You must make sure that the user 'Me'#'172.0.1.10' does exist in MySQL server. Otherwise you will encounter a auth failure.
To check all users:
select * from mysql.user; // if you have the privilige to perform it
Add users: Create User Syntax.

Yes, you can access the database, which is installed on other machine. You need to understand the url, which you provided
jdbc:mysql://localhost:3306/sampledatabase
Here localhost refers to hostname of the database server, mean same machine or local machine. If you are trying to access some other database server, which resides on other machine, then you need to replace localhost with remote-machine-hostname or it's ip-address.

Related

How would my MariaDB database be accessed by other people using JDBC?

For a university project, we were taught to use JDBC to connect to a MariaDB database. The database was created on localhost:3306.
This is what we used:
Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/dbname", username, password);
I am now doing an online group project using GitHub. How would my group partners gain access to this database if it is not on their local machine? I read somewhere about SQLdump but I couldn't seem to get it working. If I successfully did an SQLdump, could I just include the file in github and it would work for them?
Otherwise, would I need to put it on a public server?
First you need to decide whether you want to share the data or the database:
share data
This will put copies of data to your group members. Every one of you has his/her own copy in a locally installed MariaDB database, and every one of you can access it using localhost in the connection string.
https://mariadb.com/kb/en/backup-and-restore-overview/
The sql dump file can be easily shared with others. If you find the backup/restore stuff is getting tedious and merging the data is a requirement, check for https://www.liquibase.org/, or even consider going for the shared database.
shared database
Ensure your MariaDB server not only binds to localhost (127.0.0.1) but also to your machine's public IP.
open your machine's firewall to allow access to this IP. Maybe you have to look at more firewalls to be opened (e.g. your router).
create user accounts that are allowed to connect from their respective host or from any host (https://mariadb.com/kb/en/create-user/#account-names)
You can do all that from your local development machine. If you find the network or uptime requirements are not comfortable, switch to a dedicated server. This may be a cloud solution or on-premise, whatever is comfortable for you.

Is there a way to automatically discover an SQL database on your network?

I am setting up a MySQL database at my work and am also building an interface so people can interact with it.
The company already has a server that they use to host shared files and this will be used to run the database. I would like to know if it would be safe to hardcode the IP address of the server as it will be turned off and on in the evening and mornings.
If the IP is likely to change then is there a way I could find the IP address of the server automatically in the program, without the user having to do anything extra manually.
First you can use DNS and connect on and FQDN.
You can use a network scanner (for instance nmap) to search for open port 3306

Using Jaybird to connect to a Firebird database located in a computer in a local network

I'm creating a java application that connects to a Firebird database and retrieve some data. The program is done, and it runs well in my computer. The problem is that, in the computer I need to run the application, the database is in another computer in the local network and I have no idea of which path should I put into the connection string of jaybird to get it running. Let me explain a bit:
I need to run my application in a computer X, but the actual database is not in a drive in the computer, but in a computer in the local network. Something like this (this is an example, and its reproduced at my home, not at the actual site I need to get it to work):
So the path that it gives me for the database is something like:
I know that if my database is in my drive ( for example, in C:\Users\john\Desktop ), my connection to the database using Jaybird should be something like this:
static final String FB_DB_URL = "jdbc:firebirdsql://localhost:3050/C:\\Users\\john\\Desktop\\TEST1.FDB";
FB_connection = DriverManager.getConnection( FB_DB_URL , FB_USER , FB_PASS);
Actually, that is how I managed to make it work in my computer. But in this case I have no clue of how should the path be. I tried something like:
static final String FB_DB_URL = "jdbc:firebirdsql://192.168.0.101:3050/\\\\DESKTOP-1UFA09I\\Documents\\TEST1.FDB";
With ‘192.168.0.101’ being the ipv4 address of the computer where the database is. But this gave me an exception saying that the application was unable to find the database. I think this is the closest I was to find the solution, but I had no luck.
Some extra information:
Both computers had the firewall off, and could send ping to each other, so they are, in fact, connected.
I'm using Firebird 2.5 and jaybird 2.2.1.
I also tried the following with no luck:
static final String FB_DB_URL = "jdbc:firebirdsql://192.168.0.101:3050/C:\\users\\john2\\Documents\\TEST1.FDB";
The folder is shared (or at least, that’s what I think) because I can get access to it via windows, and the database file gives read/write permissions to everyone.
I'm sure that Firebird is running in both PCs.
Any help in the matter would be greatly appreciated. I'm sure the problem is in the path, because, as I said, the application works well in with the database in my computer. Also, if you could give me a solution that doesn’t require the IP of the computer where the DB is stored that would be awesome, but I think I can get that information anyways.
EDIT
For documenting purposes the solution to the problem was the following:
the right way of making the path to the database is:
"jdbc:firebirdsql://192.168.0.101:3050/C:\\folder1\\folder2\\TEST1.FDB";
the exception occurred because, apparently, jaybird has some permission issues with the users folder in the server. As my DB was located in the documents folder, it was causing some problems when jaybird tried to get access to it. the solution is to move the DB to another folder in C://
To connect to a remote database you need to know the following things:
The hostname or IP address of the Firebird server
The port number of the Firebird server (default 3050)
User name and password
Alias of the database or the full path of the database
You seem to have the first three items covered, so the problem is with the fourth. You think the database is located in C:\users\john2\Documents\TEST1.FDB. If that is the case, then the JDBC url is:
jdbc:firebirdsql://192.168.0.101:3050/C:\\users\\john2\\Documents\\TEST1.FDB
However, the user running the Firebird server process by default has no access to user folders for security reasons.
I suggest that you move the database to a location outside of the user folder, and make sure the account running the Firebird server service has read and write access (NETWORK SERVICE or LOCAL SYSTEM, I forgot which and I currently don't have access to Windows machine) .
Other notes:
Locations of a Firebird database should not be shared, because accessing a database remotely should be done through a Firebird server
You should really update to Jaybird 2.2.11; 2.2.1 is almost 4 years old and a lot has been fixed since
If you are really using Firebird 2.5(.0), then upgrade to 2.5.6, or consider upgrading to 3.0
It looks like you want to connect to the Firebird server in a client/server mode and pass the filename of the database to it. The "host:port" part connects to the server, what follows the next "/" is the name of the database file.
If you use 192.168.0.101:3050 you have to make sure the server runs on the other machine having the IP 192.168.0.101. Since the server is already on the other machine, you need to give it local name of the database name on that machine, like in
static final String FB_DB_URL = "jdbc:firebirdsql://192.168.0.101:3050/C:\\users\\john2\\Documents\\TEST1.FDB";
To not use the IP you'd need to give that machine a name that can be DNS resolved in the local network, which is not difficult, an entry in the hosts name would suffice. The port 3050 is actually the default port so you can omit it.
Since this doesn't work I guess the server is not running on the target machine, only the database file is there. In this case you should connect to you local server but let it know the network reachable database file name, like in
static final String FB_DB_URL = "jdbc:firebirdsql://localhost/\\\\DESKTOP-1UFA09I\\Documents\\TEST1.FDB";
It should allow the local Firebird server to open the \\DESKTOP-1UFA09I\Documents\TEST1.FDB file over the local network - of course if the network share DESKTOP-1UFA09I points to the right location and the account under which the local Firebird server has sufficient access rights on the other machine.

Creating Database in MySQL in Java netbeans in different machines

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

Problem connecting to remote mysql database

I am trying to connect to a mysql db on a shared server. I am using a java application to make the connection. Problem doesn't happen when I connect to localhost db.
URL = "jdbc:mysql://SHARED HOST IP:3306/DBNAME";
USER = "dbUSER";
PASS = "dbPASS";
Connection conn = DriverManager.getConnection(URL, USER, PASS);
java.sql.SQLException: Access denied for user 'DBUSER'#'mycomputersIP???' (using password: YES)
It is strange that it says denied for dbuser# mycomputersip instead of dbuser#sharedhostIP
Is there a setting on my wireless router that is screwing things up?
your database is not configured for remote access. basically its saying your user located at your ip doesn't have permission to access the database, as opposed to your user located at the web server.
if you are using a shared hosting package, you will either need to turn this on in your admin area, or it may not be supported by your host. some hosts additionally require that externally accessible databases may not be on the web server, so your connection string would likely change as well.
There are no settings screwed up on your router. If you are trying to connect from mycomputersIP (i.e. the program opening the JDBC connection is on your computer), then the database will need to grant permission to 'DBUSER'#'mycomputersIP'. (In fact, that is the point of that portion of the grant string.)
Just as VeeArr said, 'DBUSER'#'mycomputersIP???' is standard. For some reason your host exposes your database but isn't allowing you to connect. There's a way in phpMyAdmin to do it under Privileges if you manage the whole database or there's an option in your control panel. If not, then you need to contact your host for guidence
The Same problem I have got earlier, Its very simple, The access has been denied by your ISP to access DB,
Raise the request to grant access, it will work.

Categories

Resources