I am using mysql-connector-c++-8.0.33 and C++ code to connect to database.
Here is the code,
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::ConnectOptionsMap connection_properties;
driver = get_driver_instance();
connection_properties["hostName"] = "localhost";
connection_properties["userName"] = "correctusername";
connection_properties["password"] = "correctpassword";
connection_properties["schema"] = "testDB";
connection_properties["readDefaultFile"] = "/etc/my.cnf";
con = driver->connect(connection_properties);
delete con;
}
catch(sql::SQLException &e)
{
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
In this case, when I provide "readDefaultFile" property with the file path of my.cnf that has all the data required such as socket, username, group etc, I get the below exception:
# ERR: Unable to connect to localhost (MySQL error code: 2003, SQLState: HY000 )
Instead, when I provide the "socket" property as below, the database connects.
connection_properties["socket"] = "/path/mysql.sock";
Could you please suggest if I am missing something here while using readDefaultFile? The same my.cnf file works and database connects when I use mysql++ but is not connecting using mysql-connector. Requirement is that the application should use the "readDefaultFile" property and avoid using "socket" property.
Many Thanks
2
Answers
Two methods
ln
soft.Make sure
/etc/my.cnf
has[client]
section.