远程mysql使用c ++登录ssh错误1045

时间:2017-03-21 14:16:49

标签: c++ mysql ssh

我很难通过ssh连接到mySQL ...

ssh隧道似乎设置正确。 mySQL凭据是正确的。 我已授予用户所有权限。 我已经在远程服务器上访问了%。

有谁知道为什么不接受我登录mySQL数据库?

我似乎无法弄清楚出了什么问题: - (

我的代码:

void connect2sql () {

// ESTABLISH SSH TUNNEL

    ssh_session my_ssh_session;
    int rc;
    int port = 3306;
    int verbosity = SSH_LOG_PROTOCOL;
    char *password;
    // Open session and set options
    my_ssh_session = ssh_new();
    if (my_ssh_session == NULL)
        exit(-1);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "www.mysite.com");
    ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "myadminuser");
    ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_CIPHERS_C_S,"aes128-ctr");

    //ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
    // Connect to server
    rc = ssh_connect(my_ssh_session);
    if (rc != SSH_OK)
    {
        fprintf(stderr, "Error: %s\n", ssh_get_error(my_ssh_session)); //HERE IS WHERE I GET THE ERROR 
        ssh_free(my_ssh_session);
        exit(-1);
    }
    // Verify the server's identity
    // For the source code of verify_knowhost(), check previous example

    // Authenticate ourselves
    //password = "pass";
    rc = ssh_userauth_password(my_ssh_session, NULL, "adminpassword");
    if (rc != SSH_AUTH_SUCCESS)
    {
        fprintf(stderr, "Error authenticating with password: %s\n",
            ssh_get_error(my_ssh_session));
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }


    // CREATE CHANNEL
    ssh_channel channel = ssh_channel_new(my_ssh_session); // Create a ssh channel
    if(channel_open_forward(channel, "www.mysite.com", port,"localhost", port) != SSH_OK) { // Forward connection failed
         fprintf(stderr,"Error: channel broken");
} else { // SUCCESS
          fprintf(stderr,"channel success");
}

// CONNECT TO SQL

try {
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;

  // Create a connection 
  driver = get_driver_instance();
  con = driver->connect("localhost", "mysqluser", "sqluserpassword");

  // Connect to the MySQL test database 
  con->setSchema("mysqldatabaseofinterest");

  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;
}
}

我得到的输出:

[2017/03/21 13:36:26.657666, 1] ssh_connect:  libssh 0.6.3 (c) 2003-2014 Aris Adamantiadis, Andreas Schneider, and libssh contributors. Distributed under the LGPL, please refer to COPYING file for information about your rights, using threading threads_noop
[2017/03/21 13:36:26.707427, 2] ssh_socket_connect:  Nonblocking connection socket: 4
[2017/03/21 13:36:26.707442, 2] ssh_connect:  Socket connecting, now waiting for the callbacks to work
[2017/03/21 13:36:26.741895, 1] socket_callback_connected:  Socket connection callback: 1 (0)
[2017/03/21 13:36:26.786613, 1] ssh_client_connection_callback:  SSH server banner: SSH-2.0-OpenSSH_5.3
[2017/03/21 13:36:26.786646, 1] ssh_analyze_banner:  Analyzing banner: SSH-2.0-OpenSSH_5.3
[2017/03/21 13:36:26.786670, 1] ssh_analyze_banner:  We are talking to an OpenSSH client version: 5.3 (50300)
[2017/03/21 13:36:26.964356, 2] ssh_packet_dh_reply:  Received SSH_KEXDH_REPLY
[2017/03/21 13:36:26.967261, 2] ssh_client_dh_reply:  SSH_MSG_NEWKEYS sent
[2017/03/21 13:36:26.967267, 2] ssh_packet_newkeys:  Received SSH_MSG_NEWKEYS
[2017/03/21 13:36:26.967349, 2] ssh_packet_newkeys:  Signature verified and valid
[2017/03/21 13:36:27.119643, 2] channel_open:  Creating a channel 43 with 64000 window and 32768 max packet
[2017/03/21 13:36:27.186642, 2] ssh_packet_channel_open_conf:  Received a CHANNEL_OPEN_CONFIRMATION for channel 43:0
[2017/03/21 13:36:27.186660, 2] ssh_packet_channel_open_conf:  Remote window : 2097152, maxpacket : 32768
channel success
# ERR: SQLException in delve-search-py.cpp(connect2sql) on line 508
# ERR: Access denied for user 'mysqluser'@'localhost' (using password: YES) (MySQL error code: 1045, SQLState: 28000 )

0 个答案:

没有答案