CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    [RESOLVED] MySQL database connecting with VC++ through LAN

    Hi,
    I was created the mysql database in my Pc. This database connecting with vc++ code Using MySQL C++ Connection Example in Visual Studio. I done this work in single PC. It's working good. i viewed the table values using vc++ application.

    But now want to connecting the database through lan connection. I refered the below link but i'm not clear.Step # 5 Grant access to remote IP address

    For your reference, (I installed)
    PC1 - visual studio 2003, mysql 5.6 for win32 & mysql connector (Ver : mysql-connector-c++-1.0.5-win32), OS : XP SP2, IP : 192.168.100.123

    PC2 - mysql 5.6, OS: XP SP3, IP: 192,168,100.9

    Table created in PC2, database name "test". server = 'localhost', user = 'root', passwd = "".

    I like to connect PC1 & PC2 through Lan & access the table using vc++ code.
    How can i view PC2's database table values in PC1?

    Pls clear me.
    Regards,

    SaraswathiSrinath

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: MySQL database connecting with VC++ through LAN

    Code:
    // Link to the Connector/C++ library
    #pragma comment(lib, "mysqlcppconn.lib")
    
    // Connection details
    string server   = "localhost";
    string username = "root";
    string password = "123"; //
    Highlighted must be the host name/address where the database runs. If you connect to localhost, what other result you expect but connecting to localhost?
    Best regards,
    Igor

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: MySQL database connecting with VC++ through LAN

    It's time to update those XP service packs.

  4. #4
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: MySQL database connecting with VC++ through LAN

    Quote Originally Posted by Igor Vartanov View Post
    Highlighted must be the host name/address where the database runs. If you connect to localhost, what other result you expect but connecting to localhost?
    Hi Mr.Igor Vartanov,
    I used like below in my vc++ code. If i run, i will get error.
    Code:
    // Connection details
    string server   = "tcp:\\127.0.0.1:3306"; 
    string username = "root";
    string password = "";
    Error message: Unknown MySQL server host 'tcp:\\127.0.0.1:3306' (0)
    Press any key to continue . . .
    (or)
    Code:
    // Connection details
    string server   = "localhost";
    string username = "root";
    string password = "";
    Error message: Can't connect to MySQL server on '127.0.0.1' (10061)
    Press any key to continue . . .

    How can i solve this. If the connection getting no issue, then i will see the database which i was created in PC2.
    Just i start mysql in PC2. Any other settings need for PC2?
    Regards,

    SaraswathiSrinath

  5. #5
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: MySQL database connecting with VC++ through LAN

    Quote Originally Posted by Arjay View Post
    It's time to update those XP service packs.
    Is my problem solve ? If i update XP SP3 in PC1
    Regards,

    SaraswathiSrinath

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: MySQL database connecting with VC++ through LAN

    Quote Originally Posted by saraswathisrinath View Post
    Hi Mr.Igor Vartanov,
    I used like below in my vc++ code. If i run, i will get error.
    Code:
    // Connection details
    string server   = "tcp:\\127.0.0.1:3306"; 
    string username = "root";
    string password = "";
    Error message: Unknown MySQL server host 'tcp:\\127.0.0.1:3306' (0)
    Press any key to continue . . .
    Have a look at the http://www.connectionstrings.com/mysql/
    Victor Nijegorodov

  7. #7
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: MySQL database connecting with VC++ through LAN

    Sorry Mr.Victor, I'm not clear. Shall i install the MySQL Connector/C++ in PC2.
    I'm begginer. Pls tell me what will i do for solve this problem.

    But i wrote the below command in command prompt and get the information
    Code:
    C:\>mysqladmin -h localhost version
    
    mysqladmin  Ver 8.42 Distrib 5.6.14, for Win32 on x86
    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners. 
    Server version          5.6.14
    Protocol version        10
    Connection              localhost via TCP/IP
    TCP port                3306
    Uptime:                 40 min 49 sec
     
    Threads: 1  Questions: 20  Slow queries: 0  Opens: 68  Flush tables: 1  Open tab
    les: 61  Queries per second avg: 0.008
    Regards,

    SaraswathiSrinath

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: MySQL database connecting with VC++ through LAN

    If you want to connect to the server being on the PC2 then try
    Code:
    string server  = "192,168,100.9";
    If the server is on the PC1 / then set the address of PC1:
    Code:
    string server  = "192.168.100.123";
    Victor Nijegorodov

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: MySQL database connecting with VC++ through LAN

    Quote Originally Posted by saraswathisrinath View Post
    Error message: Unknown MySQL server host 'tcp:\\127.0.0.1:3306' (0)
    Press any key to continue . . .
    As far as I understand, the correct syntax would be
    Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;
    Pwd=myPassword;Protocol=tcp
    Port 3306 is implied if not specified.

    (or)
    Code:
    // Connection details
    string server   = "localhost";
    string username = "root";
    string password = "";
    Error message: Can't connect to MySQL server on '127.0.0.1' (10061)
    Press any key to continue . . .

    How can i solve this.
    Are you sure you run this on the host where MySQL database is running? The error message makes me think you run this on a different host.

    Also you need to make sure the database port, wherever it runs, appears not blocked by firewall.
    Last edited by Igor Vartanov; December 3rd, 2013 at 05:50 AM.
    Best regards,
    Igor

  10. #10
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: MySQL database connecting with VC++ through LAN

    [QUOTE=VictorN;2138523]If you want to connect to the server being on the PC2 then try
    Code:
    string server  = "192,168,100.9";
    Getting Error : Can't connect to mysql server on 192.168.100.9(10060).

    Quote Originally Posted by Igor Vartanov View Post
    As far as I understand, the correct syntax would be

    Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;
    Pwd=myPassword;Protocol=tcp
    Port 3306 is implied if not specified.
    I didn't use the code like above.
    My Code is
    Code:
    // Standard includes
    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    // Connector Includes
    //#import "C:\\Program Files\\MySQL\\MySQL Connector C++ 1.0.5\\lib\\opt\\mysqlcppconn.dll"
    #include "C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/driver.h"
    #include "C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h"
    #include "C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/resultset.h"
    #include "C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/statement.h" 
    
    // Link to the Connector/C++ library
    #pragma comment(lib, "C:\\Program Files\\MySQL\\MySQL Connector C++ 1.0.5\\lib\\opt\\mysqlcppconn.lib")
    
    // Connection details
    string server ="192.168.100.9"; // "localhost";
    string username = "root";
    string password = ""; // 
    int main(){
    
    sql::Driver *driver; // MySQL Driver Object
    sql::Connection *connection; // MySQL connection Object
    sql::Statement *statement; // Statement which holds SQL commands
    sql::ResultSet *resultSet; // ResultSet to hold the results
    //Here is the connection
    try{
    driver = get_driver_instance();
    connection = driver->connect(server, username, password);
    statement = connection->createStatement();
    statement->execute("USE test1");// table test1 created in PC2
    resultSet = statement->executeQuery("select id,name from test1");
    while (resultSet->next())
    {
    // Iterating the result set 
    cout << resultSet->getString(1) << " "; 
    cout << resultSet->getString(2) << endl; 
    }
    }
    catch (sql::SQLException e){
    cout << "Error message: " << e.what() << endl;
    system("pause");
    exit(1);
    }
    //Clear resources
    delete resultSet;
    delete statement;
    delete connection;
    system("pause");
    return 0;
    }
    This code running in PC1 and Database named "test1" created in PC2.
    I like to view the table test1 values from PC1 using PC2.
    do u find the mistake now?
    Last edited by saraswathisrinath; December 3rd, 2013 at 07:08 AM. Reason: wrongly used tag
    Regards,

    SaraswathiSrinath

  11. #11
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: MySQL database connecting with VC++ through LAN

    1. Please, use tags correctly (there must be a slash, not a backslash in closing tag.)
    Please, edit your post to correct this problem.

    2.
    Quote Originally Posted by PMsaraswathisrinath
    Code:
    string server  = "192.168.100.9";
    Getting Error : Can't connect to mysql server on 192.168.100.9(10060).
    Quote Originally Posted by PMsaraswathisrinath
    My Code is
    [code]
    Code:
    // Connection details
    string server ="192.168.100.9"; // "localhost";
    string username = "root";
    string password = ""; // 
    int main(){
    ...
    This code running in PC1 and Database named "test1" created in PC2.
    Sorry, I cannot understand, does the code using server address "192.168.100.9" run or not (you referred to the Error 10060)
    Victor Nijegorodov

  12. #12
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: MySQL database connecting with VC++ through LAN

    Quote Originally Posted by VictorN View Post
    does the code using server address "192.168.100.9" run or not (you referred to the Error 10060)
    I found the solution. I configured the lan and view the mysql table value in client PC - PC1.

    Client side code[PC1] :
    Code:
    // Connection details
    string server ="192.168.100.9"; 
    string username = "sara";
    string password = "123"; //
    Server Side Code[PC2]
    Code:
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'sara'@'192.168.100.9' IDENTIFIED BY '123';
    Query OK, 0 rows affected (0.00 sec)
     
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)
     
    
    C:\Program Files\MySQL\MySQL Server 5.6\bin>mysql -u root -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
     
    mysql> select user,host from mysql.user;
    +------+-----------------+
    | user | host            |
    +------+-----------------+
    | sara | 192.168.100.9 |
    | root | ::1               |
    |      | localhost       |
    | root | localhost     |
    +------+-----------------+
    control panel -> windows firewall - > exception->addport ->
    name : mysql
    Port number: 3306
    select TCP
    click ok


    Solved

    Thank you Mr.Victor & Mr.Igor.
    Regards,

    SaraswathiSrinath

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured