MySql实现远程连接
安装完MySQL之后就没去配置了,然后每次都是使用local连接,今天做测试使用了IP地址,然后就又掉了链子,以前也遇到过,就是不长记性!!!!!
错误日志:
java.sql.SQLException: null, message from server: "Host ‘duanwf‘ is not allowed to connect to this MySQL server" at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1128) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305) at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:135) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137) at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014) at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32) at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810) at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547) java.lang.NullPointerException at com.asinfo.dao.impl.SuperDao.update(SuperDao.java:113) at com.asinfo.dao.impl.FileDaoImpl.insert(FileDaoImpl.java:75) at com.asinfo.service.impl.FoundFileServiceImpl.foundFile(FoundFileServiceImpl.java:104) at com.asinfo.service.impl.FoundFileServiceImpl.foundFile(FoundFileServiceImpl.java:45) at com.asinfo.main.Main.main(Main.java:16)
解决办法:开启MySQL的远程连接。
首先查询数据库用户:
use mysql; select * from user;
可以看到只有本地连接权限。
开启MySQL的远程连接主要有三种方法:
一、授权使用固定IP
执行命令:
grant select,update,insert,delete on *.* to [email protected] identified by "root";
查询结果:
可以看到在user表中已经添加了root用户的IP连接权限。Host字段表示登录的主机,其值可以用IP,也可用主机名。
二、授权法
可以将Host字段的值改为%,表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。将权限改为ALL PRIVILEGES,执行命令:
grant all privileges on *.* to root@‘%‘ identified by "root";
可以看到已经添加了对所有客户机都能访问的root账户。
三、直接改表
执行命令:
use mysql; update user set Host = "%" where User = "root";
这样,所以客户机都能远程访问本机MySQL了。
本文出自 “Forever_Love_ING” 博客,请务必保留此出处http://dwf07223.blog.51cto.com/8712758/1546087
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。