简体   繁体   中英

Can't connect to MySQL from C++

From some application, I can't connect to a remote database server:

  MYSQL conn;  

  if(!mysql_init(&conn))
      std::wcout << "Error: can't create MySQL-descriptor" << endl;   

  if(!mysql_real_connect(&conn,"ip","user","password","dbname",0,0,0))
       std::wcout << "Error: can't connect to MySQL server" << endl;     

  if(mysql_query(&conn, "SELECT VERSION()") != 0)
       std::wcout << "Error: can't execute SQL-query" << endl;

I opened port 3306 on the remote server:

 iptables -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT 
 iptables save
 iptables restart

Error:

Failed to connect to database:Error: Can't connect to MySQL server on 'ip_..' (10060)

What could be the reason?

You should display result of mysql_error after every failed test (following a successful mysql_init ), eg:

 if(!mysql_real_connect(&conn,"ip","user","password","dbname",0,0,0))
     std::wcout << "Error: can't connect to MySQL server:"
                << mysql_error (&conn) << endl; 

otherwise your user won't be able to understand what is happening to him.

You'll probably find out what is happening. If not, look into the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM