MySQL Verbindung mit localhost als IP funktioniert, aber mit normaler IP nicht

1c0re

Neues Mitglied
Hallo,
Ich habe ein kleines Datenbank-Tool geschrieben, nun wollte ich, dass man in einer .txt-Datei die Verbindungsdaten angeben kann.
Vorher habe ich zum testen die Adresse localhost oder 127.0.0.1 genutzt, bis da hat auch alles noch funktioniert, jetzt habe ich es mit meiner lokalen IP und meiner IP von außen versucht und beides schlägt fehl. Was ist das? ???:L

Der Code zum Verbinden
Java:
    private void initDatabaseStuff() {
		MysqlDataSource mds = new MysqlDataSource();
        String server = null;
        String portStr;
        Integer port = null;
        String db = null;
        String uid = null;
        String pass = null;
        try {
        	FileReader f = new FileReader("database.txt");
            BufferedReader r = new BufferedReader(f);
			server = r.readLine();
			portStr = r.readLine();
			port = new Integer(portStr);
			db = r.readLine();
			uid = r.readLine();
			pass = r.readLine();
			f.close();
			r.close();
		} catch (Exception e1) {
			javax.swing.JOptionPane.showMessageDialog(getParent(), "Die Konfigurationsdatei konnte nicht geöffnet werden");
			e1.printStackTrace();
		}
        mds.setServerName(server);
        mds.setPort(port);
        mds.setDatabaseName(db);
        mds.setUser(uid);
        mds.setPassword(pass);
        try {
            con = mds.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
 
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                try {
                    if (!con.isClosed()) {
                        con.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

Eclipse spuckt immer einen der folgenden Fehler aus
Code:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: java.sql.SQLException: Must specify port after ':' in connection string
	at com.Bestand.initDatabaseStuff(Bestand.java:119)
	at com.Bestand.<init>(Bestand.java:82)
	at com.Bestand.main(Bestand.java:163)
	at com.PulverMain$2.actionPerformed(PulverMain.java:49)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$400(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.sql.SQLException: Must specify port after ':' in connection string
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
	at com.mysql.jdbc.NonRegisteringDriver.parseHostPortPair(NonRegisteringDriver.java:219)
	at com.mysql.jdbc.NonRegisteringDriver.parseURL(NonRegisteringDriver.java:707)
	at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:430)
	at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:141)
	at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:111)
	at com.Bestand.initDatabaseStuff(Bestand.java:117)
	... 39 more
oder
Code:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: java.sql.SQLException: null,  message from server: "Host 'Chrissi_PC.fritz.box' is not allowed to connect to this MySQL server"
	at com.Bestand.initDatabaseStuff(Bestand.java:119)
	at com.Bestand.<init>(Bestand.java:82)
	at com.Bestand.main(Bestand.java:163)
	at com.PulverMain$2.actionPerformed(PulverMain.java:49)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$400(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.sql.SQLException: null,  message from server: "Host 'Chrissi_PC.fritz.box' is not allowed to connect to this MySQL server"
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1104)
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2412)
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2445)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2230)
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
	at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:443)
	at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:141)
	at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:111)
	at com.Bestand.initDatabaseStuff(Bestand.java:117)
	... 39 more
beim 2. liegt es an der Datenbank, das habe ich schon gesehen, aber was muss ich umstellen um es zu beheben?
Hoffentlich kann mir jemand von euch helfen
 
Code:
java.sql.SQLException: Must specify port after ':' in connection string
Das sieht nach einem Fehler in der jdbc-Url aus. Zeig mal, wie Du die zusammenbaust.
Code:
java.sql.SQLException: null,  message from server: "Host 'Chrissi_PC.fritz.box' is not allowed to connect to this MySQL server"
In MySQL kann man sehr feingranular konfigurieren, welcher User auf welche Datenbank(en) sich von wo aus verbinden kann. Z.B. kann der root-User sich aus Sicherheitsgründen nur von localhost aus verbinden. Wenn du Deine Netzwerk-IP verwendest, ist die Quell-IP nicht localhost und die o.g. Fehlermeldung würde kommen. Berechtigungen kannst Du mittels des GRANT-Befehls vergeben. Infos gibt's hier: MySQL :: MySQL 5.1 Referenzhandbuch :: 13.5.1.3 GRANT und REVOKE
 
Code:
java.sql.SQLException: Must specify port after ':' in connection string
Das sieht nach einem Fehler in der jdbc-Url aus. Zeig mal, wie Du die zusammenbaust.
da habe ich einfach jdbc://1.2.3.4 eingegeben (ist nur ein beispielwert, weil ich die genaue ip nicht im kopf habe, aber nach dem aufbau)
Code:
java.sql.SQLException: null,  message from server: "Host 'Chrissi_PC.fritz.box' is not allowed to connect to this MySQL server"
In MySQL kann man sehr feingranular konfigurieren, welcher User auf welche Datenbank(en) sich von wo aus verbinden kann. Z.B. kann der root-User sich aus Sicherheitsgründen nur von localhost aus verbinden. Wenn du Deine Netzwerk-IP verwendest, ist die Quell-IP nicht localhost und die o.g. Fehlermeldung würde kommen. Berechtigungen kannst Du mittels des GRANT-Befehls vergeben. Infos gibt's hier: MySQL :: MySQL 5.1 Referenzhandbuch :: 13.5.1.3 GRANT und REVOKE
das wusste ich nicht, habe natürlich immer mit root versucht, ich probiers gleich mal aus

EDIT: Vielen Dank, es hat mit nem anderen user mit entsprechenden rechten geklappt :toll:
 
Zuletzt bearbeitet:

Zurück
Oben