I am trying to using the MySQL c++ connector to connect MySQL database on the local machine.
I installed the MySQL c++ connector 7.1.1.9 and boost. In the c++ IDE. I included header path and the connector library file libmysqlcppconn-static.a
into the project. The project compiles fine.
But when I try to run the example to just connect a local database.
try{
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
driver = sql::mysql::get_mysql_driver_instance();
// I tried the user root here, but got same exception
//con = driver->connect("tcp://127.0.0.1:3306", "root", "password");
con = driver->connect("tcp://127.0.0.1:3306", "user", "password");
delete con;
}
catch(sql::SQLException &e)
{
// Edit1: fix code based on Macxx's comments
// a access denied exception is thrown from here.
std::cerr << e.what() << '\n';
}
An exception
mySQL exceptions: Access denied for user 'root'@'localhost' (using password: NO)
is thrown. I checked that the MySQL is installed correctly by accessing it in the terminal:
$ mysql -u root -p
Enter password: // <---entered "password" here, so I am sure the password is correct
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
It seems the connector is ignoring the second parameter. I also tried to use the structure ConnectOptionsMap
to set ['password'] to "password". Still not working properly.
Any suggestion to fix the exception?
A little something about after the connection is granted: I had a very difficult time using the binary libraries (libmysqlcppconn.so / libmysqlcppconn-static.a) downloaded from the Oracle MySQL official website.
If I execute
con->setSchema("AN_EXISTING_DB")
The app throws an exception: MySQL server has gone away
;
If I use :
sql::Statement *st = con->createStatement();
st->execute("use AN_EXISTING_DB");`
The app crashes due to bad allocation.
Eventually, I solved this problem by using the packages from apt-get (They are early version of the sqlcppconn).
sudo apt-get install libmysqlcppconn-dev
Then I changed the project file to link the libraries in /usr/lib/libmysqlcppconn.so
and /usr/lib/x86_64-linux-gnu/libmysqlclient.so
.
[environment]
Ubuntu 16.04.2
MySQL 5.7
MySQL connector version: 7.1.1.9
MySQL connector version: 7.1.1.7
boost 1.58.0
g++ 5.4.0
Thanks in Advance
Rong