skip to Main Content

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


  1. Two methods

    1. The ip address is used in the code to create the corresponding user in the database.
    2. Connect the path of sock with ln soft.
    Login or Signup to reply.
  2. Make sure /etc/my.cnf has [client] section.

    [client]
    socket=/path/mysql.sock
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search