Keine Verbindung zur MySQL-Datenbank



  • Hallo,

    gestern habe ich nach dem Studium von mehreren Tutorials und Videos angefangen, eine Verbindung zu meiner MySQL-DB herzustellen.
    Dabei bin ich nach den Vorgaben der MSDN-Seite vorgegangen und habe auch das dortige Example1 verwendet.

    https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-apps-windows-visual-studio.html
    https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-complete-example-1.html

    Kompilierung OK, aber die Verbindung wird nicht korrekt hergestellt mit der Fehlermeldung 2003 und HY000. Der DB-User und die DB existieren, ob mit und ohne Passwort, keine Verbindung und Timeout. Auch die Verbindung zu einer lokalen DB klappt nicht.

    Vielleicht kann mir jemand auf die Sprünge helfen oder hat eine funktionierende Variante für C++ und MySQL.

    mfg Christian 'eXc'



  • Kann sein das dies eher ein MySQL Thema ist, hat aber auch was mit C++ zutun. Wenn falsch, dann bitte verschieben, Danke!



  • Kannst du dich denn per Commandline mit der MySQl-DB verbinden?
    Schau auch mal die Antworten in Can't connect to MySQL server on 'localhost' (10061) after Installation



  • eXc schrieb:

    hat aber auch was mit C++ zutun

    Eher nicht. Du zeigst ja noch nicht einmal Code.



  • Ja, ich kann mich per Konsole auf die DB verbinden, wird ja auch aktiv genutzt inkl. User.

    /* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.
    
    There are special exceptions to the terms and conditions of the GPL
    as it is applied to this software. View the full text of the
    exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
    software distribution.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    */
    
    /* Standard C++ includes */
    #include "stdafx.h"
    #include <stdlib.h>
    #include <iostream>
    
    /*
    Include directly the different
    headers from cppconn/ and mysql_driver.h + mysql_util.h
    (and mysql_connection.h). This will reduce your build time!
    */
    #include "mysql_connection.h"
    
    #include <cppconn/driver.h>
    #include <cppconn/exception.h>
    #include <cppconn/resultset.h>
    #include <cppconn/statement.h>
    
    using namespace std;
    
    int main(void)
    {
    	cout << endl;
    	cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
    
    		try {
    		sql::Driver *driver;
    		sql::Connection *con;
    		sql::Statement *stmt;
    		sql::ResultSet *res;
    
    		/* Create a connection */
    		driver = get_driver_instance();
    		con = driver->connect("tcp://5.230.97.XXX:3306", "tester", "");
    		/* Connect to the MySQL test database */
    		con->setSchema("tester");
    
    		stmt = con->createStatement();
    		res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
    		while (res->next()) {
    			cout << "\t... MySQL replies: ";
    			/* Access column data by alias or column name */
    			cout << res->getString("_message") << endl;
    			cout << "\t... MySQL says it again: ";
    			/* Access column data by numeric offset, 1 is the first column */
    			cout << res->getString(1) << endl;
    		}
    		delete res;
    		delete stmt;
    		delete con;
    
    	}
    	catch (sql::SQLException &e) {
    		cout << "# ERR: SQLException in " << __FILE__;
    		cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
    		cout << "# ERR: " << e.what();
    		cout << " (MySQL error code: " << e.getErrorCode();
    		cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    	}
    
    	cout << endl;
    
    	return EXIT_SUCCESS;
    }
    

    Die Antworten konnten mir leider nicht helfen 😞



  • tcp://5.230.97.XXX:3306", "tester", ""

    Sollte da nicht eine IP-Adresse stehen?



  • Normalerweise steht da auch die ganze IP-Adresse. Hat denn niemand vielleicht ein funktionierendes Script das er mir zum testen zur Verfügung stellen kann? Wäre super.

    mfg Christian 'eXc'



  • Du benutzt doch schon ein Beispiel von Oracle. Was erwartest du noch? Insbesondere in einem C++ Forum?



  • Das mir eventuell jemand sagen kann, warum das nicht funktioniert. Vielleicht gibt es ja auch Beispiele die leichter umzusezten sind. Normale Datenübertragung von Files per cURL oder FTP ist doch auch nicht so schwer.

    Aber anders, kann ich ausser der Fehlermeldung vielleicht noch irgendwas einbauen das mir die Fehlerursache näher erläutert? Bin ja immer willig zu lernen, nur wenn man mehrere Sachen probiert und nicht weiß wo der Fehler liegen könnte (natürlich auch aufgrund von mangeldem Grundwissen) bringt das mich nicht weiter.
    Ich habe bis jetzt immer versucht durch meine Projekte näher an bestimmte Themen zu kommen und nun versuche ich das genauso. Bis auf die Verbindung zur DB funktioniert ja jetzt auch alles.

    mfg Christian 'eXc'



  • Ich wünschte ich könnte helfen, aber habe bisher immer nur direkt die C Api benutzt. Kannst du ja auch mal ausprobieren. Ich meine wenn nichts hilft, schadet das ja nicht unbedingt.
    (EDIT, bzw aktuell benutze ich SOCI)



  • Habe dieses Problem lösen können, es lag an mir selber und der Rechtevergabe der Datenbank.

    Trotzdem Danke für die Hilfe!


Anmelden zum Antworten