简体   繁体   中英

C++: can't connect to remote mysql database

I writing a c++ program that should connect to a mysql database. it works successfully when I use the local database, but I get an error "Can't connect to mysql database on '192.168.0.111' (111)" when I try to connect to a database on another computer. this is the function that test my connection:

void addb()
{
string mainServer="192.168.0.111";
string mainDbUser="root";
string mainDbPass="111";
MYSQL *connect; //database connection variable

connect=mysql_init(NULL);
if(!connect)
    cout<<"Couldn't initiate connector\n";

if (mysql_real_connect(connect, mainServer.c_str(), mainDbUser.c_str(), mainDbPass.c_str(), "main" ,0,NULL,0))
{
    cout<<" done\n";
}
else
{
    cout<<mysql_error(connect)<<endl;
}
mysql_close (connect);
}

both computers are running Linux Mint OS. I tried disabling my firewall on the second computer but i got the same problem. note: I can access the database using phpmyadmin with my web browser.

Make sure the firewall allows MySQL port on the DB machine.

Make sure in the my.cnf the database is loaded with you have the proper network configuration (to listen to the public IP).

From the error message - I would suspect it is a firewall issue - so make sure the DB machine firewall allows incoming communication (and specifically - that SELinux enables it in addition to the firewall) and that the sending machine allows outgoing communication to this machine.

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