標籤: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.NullPointerExceptionat 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;
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/47/B5/wKioL1P-pfDQcUvaAAD_qrRXUis400.jpg" title="1.png" alt="wKioL1P-pfDQcUvaAAD_qrRXUis400.jpg" />
可以看到只有本地串連許可權。
開啟MySQL的遠端連線主要有三種方法:
一、授權使用固定IP
執行命令:
grant select,update,insert,delete on *.* to [email protected] identified by "root";
查詢結果:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/47/B6/wKioL1P-pvGiUzHSAAEhUgUEFgY678.jpg" title="2.png" alt="wKioL1P-pvGiUzHSAAEhUgUEFgY678.jpg" />
可以看到在user表中已經添加了root使用者的IP串連許可權。Host欄位表示登入的主機,其值可以用IP,也可用主機名稱。
二、授權法
可以將Host欄位的值改為%,表示在任何用戶端機器上能以root使用者登入到mysql伺服器,建議在開發時設為%。將許可權改為ALL PRIVILEGES,執行命令:
grant all privileges on *.* to [email protected]‘%‘ identified by "root";
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/47/B6/wKioL1P-qMyQKWzQAAFftVSPWSs628.jpg" title="3.png" alt="wKioL1P-qMyQKWzQAAFftVSPWSs628.jpg" />
可以看到已經添加了對所有客戶機都能訪問的root賬戶。
三、直接改表
執行命令:
use mysql;update user set Host = "%" where User = "root";
這樣,所以客戶機都能遠端存取本機MySQL了。
本文出自 “Forever_Love_ING” 部落格,請務必保留此出處http://dwf07223.blog.51cto.com/8712758/1546087
MySql實現遠端連線