今天在进行jdbc连接远程服务器MYSQL的时候,连接不上,出现了错误
代码如下:
package jdbc;
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class jdbc01 {
public static void main(String[] args) throws SQLException {
Driver driver = new Driver();
String url = "jdbc:mysql://47.118.69.23:3306/ss";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","123456");
Connection connect = driver.connect(url, properties);
String sql = "insert into actor values(null,'刘德华','男','1970-11-11','110')";
Statement statement = connect.createStatement();
int rows = statement.executeUpdate(sql);
System.out.println(rows > 0 ?"成功":"失败");
statement.close();
connect.close();
}
}
错误如下:
Exception in thread "main" 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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2252)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
at jdbc.jdbc01.main(jdbc01.java:21)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:75)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:214)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
... 13 more
Process finished with exit code 1
经过翻译,发现是通信链路故障, 那么先尝试第一步:尝试连接到本地的mysql,这样可以排除是导入jar包,字符编码等的问题
String url = "jdbc:mysql://47.118.69.23:3306/ss";
String url = "jdbc:mysql://localhost/ss";
运行 成功! 说明是connect的问题,这时候在ss后面加?之类的解决方法就没用了,因为那样改变的是jar版本和字符集编码的问题. 接下来登录云服务器上的mysql,让mysql数据库允许被远程连接访问
命令行登录mysql后:
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
如果出现 不用管它
接下来刷新一下,不然不会生效。
mysql>flush privileges;
注意,用局域网内的另一台电脑连接你的mysql如果出现 bad handshake字样,证明你俩的数据库版本不同,连接失败。
确认数据库允许被远程访问以后,再次尝试,还是不行 接下来按照这些我在网上搜索到的常见问题进行排查: 再检查url的问题 1.jdbc依赖以及代码是否正常 2.连接数据库的url是否正确,特别是ip地址,因为是服务器,注意内网、外网的ip 3.检查云的数据库的黑名单和白名单设置. 4.检查连接的数据库用户名和密码. 5.查看mysql是否配置允许远程访问(上述已经提到). 6.是否关闭防火墙 ,或者 3306端口是否被开放允许其它IP访问 7.确保在云服务器后台中开放了对应的端口映射,第三方提供的云服务器一般对所有端口都是默认关闭不对外访问的,你需要在控制台手动开放3306端口,开放的时候可以指定给一个具体的IP,或者指定任意的IP,具体的操作在后台界面应该有提示.
进行排查以后,发现我的root权限在之前被我改了,所以连接不到… 还是挺麻烦的,但是只要找到问题就好办了.
|