Results 1 to 7 of 7

Thread: Connect to remote MySQL database using SSH

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2015
    Location
    Russia, Moscow
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question Connect to remote MySQL database using SSH

    Hello!
    I need your help with Qt database and SSH. I want to establish a connection with remore MySQL database using SSH. For SSH I use libssh and the folloing code:

    Qt Code:
    1. Settings settings; const Settings::SSH &ssh{settings.loadSSH()}; ssh_key key;
    2. int code = ssh_pki_import_privkey_file("key.ossh", ssh.password.toStdString().c_str(), NULL, NULL, &key);
    3. if(code != SSH_OK)
    4. {
    5. _logger->logOnce("Bad private key!");
    6. return false;
    7. }
    8.  
    9. ssh_options_set(_ssh, SSH_OPTIONS_HOST, ssh.host.toStdString().c_str());
    10. code = ssh_connect(_ssh);
    11. if(code != SSH_OK)
    12. {
    13. _logger->logSshErrorOnce("Unable to connect to host!", _ssh);
    14. return false;
    15. }
    16. code = ssh_userauth_publickey(_ssh, ssh.login.toStdString().c_str(), key);
    17. if(code != SSH_AUTH_SUCCESS)
    18. {
    19. _logger->logSshErrorOnce("Unable to authorize!", _ssh);
    20. return false;
    21. }
    22. code = ssh_channel_open_forward(_channel, ssh.host.toStdString().c_str(), 22, "localhost", 3307);
    23. if(code != SSH_OK)
    24. {
    25. _logger->logSshErrorOnce("Unable to create ssh-channel!", _ssh);
    26. return false;
    27. }
    To copy to clipboard, switch view to plain text mode 
    It works and there are no errors. BUT, what I should do next to transfer data from my Qt database to ssh tunnel? May be I need to use something in addition (classes, libs...)?
    If I write after the code above this:

    Qt Code:
    1. const Settings::Database &db{settings.loadDatabase()};
    2. _db = QSqlDatabase::addDatabase("QMYSQL", "my_database");
    3. _db.setPort(3307);
    4. _db.setHostName("localhost");
    5. _db.setDatabaseName("test_db");
    6. _db.setUserName(db.login);
    7. _db.setPassword(db.password);
    8.  
    9. _db.open();
    To copy to clipboard, switch view to plain text mode 

    then Qt says that there is a connection error...
    How to correctly make the connection? Help please.

    PS I dont want to use QProcess and run external tunneler like plink.exe.
    Last edited by _Dron_; 1st February 2016 at 07:56.

Similar Threads

  1. How to Connect to MySQL database
    By johnL in forum Newbie
    Replies: 12
    Last Post: 16th May 2015, 16:43
  2. Replies: 2
    Last Post: 16th April 2012, 13:42
  3. Replies: 3
    Last Post: 8th March 2011, 07:57
  4. Questions about connect to remote database
    By stmk in forum Qt Programming
    Replies: 3
    Last Post: 11th November 2010, 10:02
  5. Replies: 2
    Last Post: 15th April 2010, 14:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.