MySQL实现远程连接
安装完MySQL之后就没去配置了,然后每次都是使用local连接,今天做测试使用了IP地址,然后就又掉了链子,以前也遇到过,就是不长记性!!!!!
错误日志:
1 java.sql.SQLException: null, message from server: "Host 'hadoop' is not allowed to connect to this MySQL server" 2 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 3 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) 4 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) 5 at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1128) 6 at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336) 7 at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369) 8 at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153) 9 at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792) 10 at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) 11 at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source) 12 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 13 at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 14 at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 15 at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381) 16 at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305) 17 at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:135) 18 at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182) 19 at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171) 20 at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137) 21 at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014) 22 at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32) 23 at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810) 24 at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547) 25 java.lang.NullPointerException 26 at com.asinfo.dao.impl.SuperDao.update(SuperDao.java:113) 27 at com.asinfo.dao.impl.FileDaoImpl.insert(FileDaoImpl.java:75) 28 at com.asinfo.service.impl.FoundFileServiceImpl.foundFile(FoundFileServiceImpl.java:104) 29 at com.asinfo.service.impl.FoundFileServiceImpl.foundFile(FoundFileServiceImpl.java:45) 30 at com.asinfo.main.Main.main(Main.java:16)
解决办法:开启MySQL的远程连接。
首先查询数据库用户:
use mysql; select * from user;
可以看到只有本地连接权限。
开启MySQL的远程连接主要有三种方法:
一、授权使用固定IP
执行命令:
grant select,update,insert,delete on *.* to root@10.254.0.230 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了。
博客地址: http://www.cnblogs.com/dwf07223,本文以学习、研究和分享为主,欢迎转载,转载请务必保留此出处。若本博文中有不妥或者错误处请不吝赐教。 |