简体   繁体   English

C ++:无法连接到远程mysql数据库

[英]C++: can't connect to remote mysql database

I writing a c++ program that should connect to a mysql database. 我编写了一个应该连接到mysql数据库的c ++程序。 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. 当我使用本地数据库时,它可以成功工作,但是当我尝试连接到另一台计算机上的数据库时,出现错误“无法连接到192.168.0.111(111)上的mysql数据库”。 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. 两台计算机都运行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. 注意:我可以通过Web浏览器使用phpmyadmin访问数据库。

Make sure the firewall allows MySQL port on the DB machine. 确保防火墙允许数据库计算机上的MySQL端口。

Make sure in the my.cnf the database is loaded with you have the proper network configuration (to listen to the public IP). 确保在my.cnf数据库中已加载了正确的网络配置(以侦听公共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. 从错误消息中-我会怀疑这是防火墙问题-因此,请确保DB计算机防火墙允许传入通信(特别是-SELinux除了防火墙之外还启用了它),并且发送机器允许与该计算机进行传出通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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