I have a remote mysql database server setup on a machine myuniversity.edu and server is named 'localhost'. On it I have the database named 'MyDatabase'.
I want to connect it through Java.
The connection urls that I have tried are:
jdbc:mysql://myuniversity.edu/localhost
jdbc:mysql://myuniversity.edu/localhost/MyDatabase
jdbc:mysql://myuniversity.edu:3306/MyDatabase
but I get the `Connection refused: connect` exception.
Could someone please tell what the connection url should be in this case?
Not really sure if your machine name is myuniversity.edu, you can instead try the IP Address with the connection string, Localhost is the name for loopback network interface and accessible on that machine only. Also make sure if your default port for mysql (may be 3306) is open. With IP address your connection string would look like:
jdbc:mysql://192.168.0.123/MyDatabase
With IP and port it would be:
jdbc:mysql://192.168.0.123:3306/MyDatabase
(You need to replace your IP in the above string)
I'ts impossible to connect remotely without (IP) address
try this approach
if you want to connect it via internet :
OPEN CMD on your computer
in CMD write ping myuniversity.edu (for example ping google.com)
then you will get an ip address of the website and you can copy the ip
then try this approach :
Connection con;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://THE IP ADDRESS :3306/DatabaseName");
System.out.println("CONNECTED");
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
Ok so here's what I did to fix the issue:
In my.cnf file, I changed the bind-address from '127.0.0.1' to the
'host ipaddress'. This allows connecting to the remote mysql server
but would not allow access for any remote host trying to connect to
it.
To fix that, I added an entry in user table with host '%'. This allows remote hosts to connect to the database.
Now I can connect to the database with jdbc:mysql://serverIpAddress:3306/MyDatabase
Related
public Connection conn() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException
{
Connection all_connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
all_connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/dbname","username","password");
return all_connection;
}
Above code is my connection code. Its working fine. If i replace localhost by my ip address, its not working. Actually i want to replace localhost by ip address. Replacing localhost by 127.0.0.1 works fine. Why replacing ip address is not working????
This is probably an issue with your database user. (I can't tell for sure because you didn't post the actual error)
You'll need to verify that the user you're logging in as is allowed to access is allowed to access the database from an external IP. It seems likely that you're using the root user, which is allowed access from localhost, 127.0.0.1, and ::1, but not from external IPs.
If you are, make sure you set up a new DB user with only the required access, and allow access from the specific IP, or % (any host)
I have faced same issue while I am new to this.
IP address means other DB server's IP ?
If yes then check firewall that is it allow Incoming connection for 3306 port ?
If it is not then ad new rule in that server machine with 3306 connection post allow.
and which OS in server machine ?
I have done like following in postgresql :
jdbc:postgresql://192.168.1.9:5432/dbName
In your case it would be :
jdbc:mysql://192.168.1.9:3306/dbName
I suggest you to first of all check that machine's firewall port rules then go for next.
And be sure that you have written true user name and password.
First check whether your machine is configured for the IP address you are trying.
Try to ping it from other machine.
Try to nslookup for the same IP address.
If both the results are positive, means that your machine is configured for the IP address,
If any one fails, try to configure your ip, and then try again.
Connection object = DriverManager.getConnection("jdbc:mysql://127.xx.xx.x:3306/dbname","username","password");
use above code after configuration.
By default in some database server remote access is disabled. You have to enable remote access.
If you are using mySQL here is the solution.
1.Go to my sql bin folder or add it to PATH
2.Login to root by mysql -uroot -proot (or whatever the root password is.)
3.On success you will get mysql>
4.Provide grant access all for that user.
query to enable access.
GRANT ALL PRIVILEGES ON *.* TO 'username'#'IP' IDENTIFIED BY 'password';
Here IP is IP address for which you want to allow remote access, if we put % any IP address can access remotely.
I am new java programmer, I try to setup jdbc connnectivity with wamp mysql server.
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection( "jdbc:mysql://182.73.151.36:3306/sbtsystem?user=sbtuser&password=Pm#dmin&useUnicode=true&characterEncoding=UTF-8");
182.73.151.36 this is server IP address. I can't understand how to connect java with wamp mysql server
Try This:
First run these commands
ping 182.73.151.36
telnet 182.73.151.36 3306
And make 182.73.151.36 mysql eligible for remote connection
After that change your connection line and run this program
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://182.73.151.36:3306/sbtsystem","sbtuser","Pm#dmin");
You need to first download the jdbc driver files for mysql
(download from here)
The code looks fine. You need to check the network configurations. Try the following commands to see if the server is accessible.
ping 182.73.151.36
telnet 182.73.151.36 3306
supposing your network configuration is correct,
I guess your mysql server on 182.73.151.36 is not configured for remote connection,
I am trying to connect to MySQL from JDBC via localhost. But the connection fails. In the exception, I see that JDBC is trying to connect to 127.0.0.1
String connectionString = "";
try {
loadProperties();
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connectionString = "jdbc:mysql://" + properties.getProperty("host") + "/" + properties.getProperty
("database") + "?user=" + properties.getProperty("user") + "&password=" + properties
.getProperty
("password");
connect = DriverManager
.getConnection(connectionString);
logger.debug("Connected to " + properties.getProperty("host"));
} catch (Exception e) {
logger.error("Database Connection failed with connection string - " + connectionString,e);
}
From the log:
Database Connection failed with connection string - jdbc:mysql://localhost/testdb?user=testuser&password=testpass
java.sql.SQLException: Access denied for user 'testuser'#'127.0.0.1' (using password: YES)
Why is it replacing localhost with 127.0.0.1? I have configured login only for localhost.
I stumbled across this question when encountering the same issue.
To answer the question "Why is it replacing localhost with 127.0.0.1?":
From the MySQL docs, using localhost in your connection URL implies that you want to connect to a socket. Using 127.0.0.1 implies that you want to connect through TCP/IP.
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. ... To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1
According to this answer, it appears that by default JDBC only supports TCP/IP connections, at least for some Java versions. Original source: http://lists.mysql.com/java/8749:
Java itself doesn't support unix domain sockets
So, I'd guess that since JDBC only connects through TCP/IP, it converts localhost to 127.0.0.1 internally.
To solve the problem in my case:
I granted permission in MySQL for user#127.0.0.1
I changed localhost to 127.0.0.1 in my connection URL.
The IP address 127.0.0.1 is a special purpose address reserved for use on each computer. 127.0.0.1 is conventionally a computer's loopback address.
Network software and utilities can use 127.0.0.1 to access a local computer's TCP/IP network resources. Messages sent to loopback IP addresses like 127.0.0.1 do not reach outside to the local area network (LAN) but instead are automatically re-routed by the computer's own network adapter back to the receiving end of the TCP/IP stack. In simple words, localhost can also be referred as 127.0.0.1. There is a problem with MySql access privileges. This link would help you resolve it
I have ubuntu server running on virtual machine locally and I need to connect to mysql database there.
Database is in place and jdbc driver too. The only problem is that only way at the moment for my connection can be something like http://local/phpmyadmin/index.php?db=sandbox and that can't be used in jdbc connection string.
Hope, that somebody can advise a solution.
String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url,"username", "pwd");
Replace localhost with the IP of your VM. You have to use something other than NAT for networking in your VM (like Host-only, internal or bridged assuming VirtualBox).
You probably have to configure your Ubuntu firewall to let the connection through.
Also, you have to set mysql up to accept connections from the outside.
In the file /etc/mysql/my.cnf edit bind-address to your ip:
bind-address = your-vms-ip
I am trying to connect to MySQL database over a network. I have installed MySQL, and the service is running on the default port. I have also install the SQL connector to the jar file and added java JDK to the server machine. I am able to connect to my local database using the code:
private String dbUrl = "jdbc:mysql://localhost/DatabaseName";
private String dbClass = "com.mysql.jdbc.Driver";
But when I try and connect to it over the network, with the IP address (eg: 192.168.1.45):
private String dbUrl = "jdbc:mysql://192.168.1.45/DatabaseName";
private String dbClass = "com.mysql.jdbc.Driver";
I get the connection error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Does anyone know what this issue is? Do I need to add a different address?
I have added the default port with the address but cannot get it to work.
Thanks for any help.
By default, MySQL doesnt allow remote access and only allow local access.
You will have to modify your /etc/mysql/my.cnf config (on Linux) with:
bind-address = 192.168.1.45 // Your Server IP address
# skip-networking // This should be commented .
See the whole procedure here.
Check the my.cnf [mysqld] settings for the parameters port, bind-address, socket, to make sure these aren't causing problems.
Check the files /etc/hosts, /etc/hosts.deny to make sure everything is ok.
Check firewall applications
Check to make sure whatever directory mysqld's sockets are have the appropriate permissions.
Check to make sure that security settings within the mysql database (user table) permit access from your remote host.
Make sure you can telnet OK to localhost 3306, 127.0.0.1 3306, and whatever other IP address your machine is configured to (use ifconfig to find out).
You can test the server setup using the MySQL Workbench or mysql client which will narrow down the problem. It's also sometimes useful to just see if the server's there:
telnet host 3306
It'll tell you the version number of the server and some other binary junk. Enough to know your host is listening.