Remote Connection to MySQL Server [duplicate] - java

This question already has answers here:
Enable remote MySQL connection: ERROR 1045 (28000): Access denied for user
(13 answers)
Closed 8 years ago.
hello there I am new to MySQL Server and I want to connect to MySQL DataBase remotely from my computer other than database placed on! My IP on machine on which I have MySQL server is:
39.32.58.13
and I am using this code to connect:
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
private void Connect(){
try{
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://39.32.222.77:3306/test","myname", "mypass");
}
}
But it gives me an error all the time:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Is it me doing something wrong because I'm doing it for the first time and need instructions on how to do it ! so please dont mind my mistakes thanks in advance

You need to grant permission for using mysql fro remote location.
GRANT ALL PRIVILEGES ON *.* TO 'user'#'%.example.com'
IDENTIFIED BY PASSWORD 'Password'
WITH GRANT OPTION;
FLUSH PRIVILEGES;
where user can be root or any other user
see this for more info on grant

First,check if the remote mysql database is running.
Second,check if your mysql accound has rights to access the database
hope to help you !

You have to put this as root:
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'IP' IDENTIFIED BY 'PASSWORD';
where IP is the IP you want to allow acess and USERNAME is the user you use to connect
If you want to allow access from any IP just put % instead of your IP
and then you only have to put
FLUSH PRIVILEGES
or restart mysql server and that's it.
Here's Answer
Another Tutorial

Related

Connecting to MySql using Java - SSL Connection

I have been trying to to connect to MySql data base which uses ssl connection with java and having trouble, if any one can help me will be of great help.
Manual Connection to MySql:
We use MySQL Workbench , parameters - Hostname - "test-db1-ro-xxxxxx.net" , Port - 3306 , User Name , Password.
There is a SSL CA File - mysql-ssl-ca-cert.pem , the manual connection is successful.
I tried the same in java (in windows system)
My code:
String url = "jdbc:mysql://test-db1-ro.apdev.XXXXXX.net:3306/database";
String driver = "com.mysql.jdbc.Driver";
String userName = "XXXXXXXX";
String password = "XXXXXXXX";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url,userName,password);
when i run the code i get an error as "Access Denied"
I am thinking it is due to the SSL Connection.
I am not able to know how to use the .pem file to make a secure connection to database in this case.
Can some one help, have done various things but always get the same access denied error.
Full StackTrace:
java.sql.SQLException: Access denied for user 'xxxxxxxx' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:885)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3421)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1247)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at TestDBConnection.main(TestDBConnection.java:34)
You have trouble authenticating.
On the MySql terminal:
GRANT ALL ON *.* TO 'user'#'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
If problem still persists:
GRANT ALL ON *.* TO 'user'#'%' WITH GRANT OPTION;
This command grants you superuser privileges.
FLUSH PRIVILEGES;
To make sure your privileges are loaded.
NB: Do not use this account on websites with public access.

Error in creating the connection. java.sql.sqlException

i am trying to create a database connection , but database is not in my localhost, so i provided the IP instead of localhost in
Connection con= DriverManager.getConnection(conStr,user,pass);
where
constr="jdbc:mysql://10.0.0.1/Sigma"
but its giving
java.sql.SQLException: Access denied for user 'root'#'15.3.0.4' (using password: NO)
but i login with the same user name and password
That's probably because the machine where you're running that code is has not that 15.3.0.4 IP, although you can connect from your machine which potentially has that IP.
You can try to create a 'root'#'%' user who will be granted to access regardless where you are connecting from.

Unable to connect to mysql jdbc database

I am frustrated seeing this error and not knowing the solution. I am trying to connect to a mysql db with a server url but it is giving my mysqlException(stacktrace below). The code works fine till here:
String dbUrl = "jdbc:mysql://server_url/db_name";
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
String user = "user";
String password = "password";
conn = DriverManager.getConnection(dbUrl,user,password);
This is the error I'm getting
java.sql.SQLException: null, message from server: "Host '172.23.251.154' is not allowed to connect to this MySQL server"
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1070)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
Is this because I'm using a different version of mysql-connector jar?
Please help me.
From Chapter 6. SQL Questions: How Do I Enable TCP Connections to MySQL?,
By default, MySQL won't allow ANY users access to any of the databases if they connect over a TCP connection. In order to permit the connection, you must create a entry in the user table of the mysql database (make sure you select the PASSWORD function to encrypt your password). In particular, the Host field needs to indicate which host(s) are permitted to connect. If you specify % (which I would not recommend), then a user would be able to connect from any host.
What about firewall on port 3306?
May be the user you are trying to access doesn't have enough privileges to access the database from the given server.
So try providing GRANTS to the user.
GRANT ALL PRIVILEGES ON database_name TO 'user'#'hostname' IDENTIFIED BY PASSWORD <password>;
FLUSH PRIVILEGES;
here hostname can be your host address "172.23.251.154". Dont forget to flush privileges and then try connecting the server.
If your MySql server is located on your host provider there is option in your control panel where you can set ip addresses from which you can connect to mysql server in your case 172.23.251.154 if this is your ip address

Message from Server: "Host '192.168.169.98' is not allowed to connect to this MySQL server [duplicate]

This question already has answers here:
Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
(29 answers)
Closed 9 years ago.
Using JDBC, I am currently trying to connect to a server hosted on the same network using MAMP.
Using this string: private static final String DB_CONNECTION = "jdbc:mysql://192.168.169.101:8889/";
I received this error: message from server: "Host '192.168.169.98' is not allowed to connect to this MySQL server"
I am wondering if this is a firewall error?
Comment out 'skip-networking' in your my.cnf file.
If skip-networking is commented out, then it's a user level permission which needs to be set in the mysql.user or mysql.database tables.
MySQL wants you to GRANT permission for users to connect to the server by IP address and credentials. I think it's a good idea to set up credentials for that particular database and user and GRANT only the permissions necessary to accomplish the task.
Don't use the root admin credentials for anything except administration.
http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

Connecting to a MySQL Database with Java

I want to connect to my MySQL database with Java.
I am using JDBC and I have the driver installed. (com.mysql.jdbc.Driver)
The only problem is that I keep getting an error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException The last
packet sent successfully to the server was 0 milliseconds ago.
Here is my code:
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql:/mydomain.com/mydatabase", "username", "password");
I am not positive how to compose the URL (and where I get my username and password) but I have done A LOT of research.
I am the only person with acess to my database and domain, so there's no use asking the admin.
I use phpMyAdmin to create the database(s) and manage them. Do I use my phpMyAdmin username and password or what?
By the way, my site is hosted on Yahoo! Small Business.
So my questions are:
How do I make the connection URL?
What is my username and password?
I would say you are missing a forward slash on your URL.
Connection connection = DriverManager.getConnection("jdbc:mysql://mydomain.com/mydatabase", "username", "password");
Or I have a feeling that there is something wrong with your access privileges. This same thing happened to me also and it was a problem of Firewall blocking the port on the server. So verify this is not the case.
How do I make the connection URL?
Are you missing a forward slash in your URL? I would've assumed it would be something like:
jdbc:mysql://server/database
Load the drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
connect with the data base
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/widget_corp","dbuser","dbpassword");
.....jdbc:mysql://this is use as it is
.....127.0.0.1 this is the address of localhost You can use the word "localhost" insted of "127.0.0.1"
.....dbuser is the data base user
.....dbpassword is the password of the dbuser
.....3306 is the default port used for database

Categories