I have installed MySQL to my local computer. I can access MySQL from my Java Application which is running on my computer.
My connection string is private String url = "jdbc:mysql://localhost:3306/mydatabase" which allows me to connect successfully. But when I deploy my application to other computers on my LAN and try to connect to my MySQL databases from the other computers I can't access my database.
As others have mentioned in the comments, you issue is your connection string: private String url = "jdbc:mysql://localhost:3306/mydatabase"
In order for you to be able to connect to your database from other machines on your LAN you will need to change localhost to your IP address. For example:
private String url = "jdbc:mysql://192.168.0.10:3306/mydatabase"
Providing that the other machines can see 192.168.0.10 they will be able to connect (with the right credentials of course!)
Related
I am trying to create a Java application that connects to a MySQL database and I am using AWS to host it. So I created the AWS RDS instance and I got it to connect to the MySQL workbench just fine. My problem arises when I am trying to use JDBC to connect to it.
I have security groups that allow traffic from anywhere, but I also tried just allowing my IP.
I had it working when I was using a localhost but now I'm trying to move it to a server so keeping it localhost isn't an option (used localhost to test the application and such)
I made sure that my user was a remote user by doing
SELECT * from mysql.user;
And made sure that the host was a '%' and that it had all the privileges
So my code I'm trying to connect with in Java is
String connectionURL = "jdbc:mysql:/{hostname}:{port}/{database name}/?autoReconnect=true&useSSL=false";
String username = "username";
String password = "password";
con = DriverManager.getConnection(connectionURL, username, password);
Of course I have the actual host, port, and such in there just changed it for posting online.
When I try running it on Eclipse it says this java.lang.Exception: Database not found
I looked up some tutorials on AWS docs to make sure it lined up, which it all did.
Anyone have any idea why it might be connecting to MySQL workbench and not the JDBC?
I'm working on a personal project that includes a Java app connecting to a XAMPP SQL database. The app connects to the database perfectly on the same PC as the database is being hosted on. The program uses a singleton class.
Code:
private Connector() {
String host = "jdbc:mysql://localhost/my_database";
String user = "root";
String pass = "";
try {
connection = DriverManager.getConnection(host, user, pass);
System.out.println("Connection success!");
} catch (SQLException ex) {
System.out.println("Connection failed!");
}
}
I wanted to take a step further and be able to put the app on my laptop and connect to the database on my PC. I went into XAMPP and changed the "httpd-xampp.conf" file so that it allows local connections, and when I went on my laptop I tried opening phpMyAdmin from the browser and it worked!
Now I put the app on my laptop but instead of the host being:
String host = "jdbc:mysql://localhost/my_database";
I found the local IP of my PC and on my laptop changed the host to be:
String host = "jdbc:mysql://192.168.1.8/my_database";
The connection failed so I tried setting up a different user that allows any host to connect and has full access just in case. Still the same error happens.
I checked my firewall, but there was no entry that disables the connection, but anyways I was able to connect to phpMyAdmin through the browser through my laptop.
Here is the part I changed in the Xampp config file:
<Directory "N:/xampp/phpMyAdmin">
AllowOverride AuthConfig
Require all granted
ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
</Directory>
You could try port forwarding the application / port on the PC just to be sure it's not the firewall as other than that, I don't see what would prevent the connection.
Here is a link on port forwarding on Windows if you need it.
Hope this helps
I'm writing a program to push stuff into a remote database. My database is stored on a redhat server, while my program is written on a windows machine.
I dont want to give out my server addresses but lets say my Linux server is xx.xx.xx.xx8
MySQL Workbench in windows says that my mysql server host is 127.0.0.1:3306
I know there are a million similar questions but each one is pretty unique to the situation.
I've been using http://www.vogella.com/tutorials/MySQLJava/article.html#javaconnection as a guide but this looks like a local connection.
I have also been referencing this but it confuses me.
Here's a mock up of some code i think may work:
Connection connection = null;
String dburl = "jdbc:mysql://127.0.0.1:3306/Db_Name";
String userName = "user";
String passWord = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(dburl, userName, passWord);
Statement st = connection.createStatement();
String query = "INSERT INTO Example (`TestColumn`) VALUES('hello')";
int rsI = st.executeUpdate(query);
System.out.println("Hi");
}catch (Exception e) {
System.out.println(e);
} finally {
if (connection != null) {
try {
connection.close();
System.out.println("Database connection terminated");
} catch (Exception e) { /* ignore close errors */ }
}
}
that is based on the link i provided. All i changed was the address. I have no idea where the server address xx.xx.xx.xx8 should go.
In order to connect to the remote database you need to know the external ( or called public ) ip address of the server.
String dburl = "jdbc:mysql://85.65.85.222:3306/Db_Name"
Even if your database on server says it listens on localhost:3306, you can still connect to it using public ip with correct port. (providing you have sufficient access rights to the server)
Sometimes there may be situation that even if you know the remote server IP address you still won't be able to connect to remote database directly perhaps due to blocked port or other ACL issues ( and this is ofc applicable if you have no control over this). You could however create ssh tunnel to that server and create a port forward.
JSch jsch = new JSch();
Session session = jsch.getSession(user, host);
session.setPassword(password);
session.connect(timeout);
session.setPortForwardingL(listenPort, destHost, destPort);
example ssh connnection could look something like this:
ssh user#85.65.85.222 -Llocalhost:5050:127.0.0.1:3306 <- this will create a ssh tunnel with port forwarding, meaning on your local machine where you have your code and whole environment you will still be able to connect to remote database using
String dburl = "jdbc:mysql://localhost:5050/dbname"
Just to clarify for anyone looking at this later...
localhost is always the machine you are sitting at - always. There is no way you will ever connect to a remote box using localhost. Also, the IP address 127.0.0.1 is equivalent to localhost, so don't use that either.
You may see that the database binds to localhost or 127.0.0.1 on the database server. That is ok. Use the IP address of the server running the database in your database connection URL
mysql:jdbc://<IPOfServer>:<PortOfServer>/<DbName>).
You can find the IP address of the server by running the following command on the database server...
Windows: ipconfig
Linux: ifconfig
Good luck!
You can see what documentation says:
The method DriverManager.getConnection establishes a database
connection. This method requires a database URL, which varies
depending on your DBMS. The following are some examples of database
URLs:
MySQL: jdbc:mysql://localhost:3306/, where localhost is the name of
the server hosting your database, and 3306 is the port number
So, for MySQL Connector/J Database URL
The following is the database connection URL syntax for MySQL Connector/J:
jdbc:mysql://[host][,failoverhost...]
[:port]/[database]
[?propertyName1][=propertyValue1]
[&propertyName2][=propertyValue2]...
Where:
host:port is the host name and port number of the computer hosting your database. If not specified, the default values of host and port are 127.0.0.1 and 3306, respectively. So, every operation you do will be applied on this post
database is the name of the database to connect to. If not specified, a connection is made with no default database.
failover is the name of a standby database (MySQL Connector/J supports failover).
propertyName=propertyValue represents an optional, ampersand-separated list of properties. These attributes enable you to instruct MySQL Connector/J to perform various tasks.
Update: if you have a remote databases (your remote server) that you want to access to, you should have for example:
String dburl = "jdbc:mysql://<REMOTE_HOST>:3306/Db_Name"; //replace with your remote host
... // here all the code related to your remote host
Note: if you remote server has in its configuration "localhost" you have to figure out its public ip address by executing ifconfig. Then use its ip address on your string. As important note your remote server must be configured to allow remote connections.
if you connect to a server (ie. with the ip xx.xx.xx.xx8) you specify through the port (which is usually 3306) that you want to connect to the databaseserver on that machine. so simply use:
String dburl = "jdbc:mysql://xx.xx.xx.xx8:3306/Db_Name";
as your connectionstring.
If you dont know the port, mysql is running, have a look in your /etc/mysql/my.cnf file. There should be the port specified.
Also have a look at your firewallrules if you can't connect and make sure you enabled remoteconnections (here is a good tutorial to enable it: http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html).
If I have a mysql database on my own pc, I would use the following cnxn string to connect it to my java program :
String url = "jdbc:mysql://localhost:port/dbname";
But if my database is made over phpMyAdmin, what would be the connection string ?
one other note, the phpmyadmin database is not on my local pc now, it is on another one but both pc are in the same network.
Thanks for any help ..
phpMyadmin is administration web application developed on PHP platform. phpMyadmin connects to MySQL database that might be on other machine.
you need to check config.inc.php file of phpMyadmin to check the host and port of mysql database.
Once you get the IP and port of the mysql database you can connect using connect string like
jdbc:mysql://<ipaddress>:<port>/<database_created_through_phpMyAdmin>
Replace localhost with the IP of the machine that hosts your database:
String url = "jdbc:mysql://<ip_goes_here>:port/dbname";
As Rutesh mentions, this may or may not be the same machine on which phpMyAdmin runs.
I've been recently trying to connect to a hosted MySQL using Java but can't get it to work. I can connect to a local MySQL with localhost using:
connect = DriverManager.getConnection("jdbc:mysql://localhost/lego?"
+ "user=******&password=*******");
(Replacing the astrisks withmy username and password)
I can connect to the hosted MySQL database fine with PHP using:
mysql_connect('mysql.hosts.co.uk','******','**********');
mysql_select_db('test');
My problem is, I cannot connect via Java. I have an Exception which is caught if the connection doesn't work and this is always printed out.
Any ideas why it isn't working? Am I doing something wrong?
Thanks for your time,
InfinitiFizz
since it works in php (i guess you didn't try to connect from a local place with php???) it shouldn't be a port problem... but you should check that port 3306 is open... and ask the hosts company about that.
Have you noticed that in the DriverManager
http://java.sun.com/javase/6/docs/api/java/sql/DriverManager.html
you have:
getConnection(String url)
but also:
getConnection(String url, String user, String password)
Perhaps it would work better...
My guess is that you need to select a non-standard port, since I'd imagine the hosting server is serving lots of MySQL instances and they can't all use the normal one. I don't see selection of a port here.
If that's not it, perhaps there is a firewall issue somewhere along the way that's blocking the port or connection.