1

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

r0ng
  • 1,669
  • 23
  • 33

2 Answers2

0

I used only postgres before, but I think you have to grant the rights to access the database. Look at some of these threads, I hope they will help you:

In postgres you have to change the config file (pg_hba.conf) for the database to allow remote access..


I think you want to print out thrown exceptions. Change the last line to

{
    std::cerr << e.what() << std::endl;
}
Macxx
  • 75
  • 4
  • you are right about the error message. What I mean was something like that. I was using Qt. So I stripped out all Qt code and forgot to fix that part. I will edit the original question. The content of `e.what();` in the original question was `Access denied for user 'root'@'localhost' (using password: NO)`. Thanks – r0ng Jul 29 '17 at 06:54
0

In /etc/mysql/my.cnf, there will be a bind-address line. Please set it to

bind-address = 0.0.0.0

Then restart mysql service.

Next try to execute your program.

Also while creating mysql database, user make sure permissions are proper, you can refer below instructions (valid on linux / ubuntu)

CREATE USER 'user_user'@'localhost' IDENTIFIED BY 'user_password';
CREATE USER 'user_user'@'%' IDENTIFIED BY 'user_password';
GRANT ALL ON *.* TO 'user_user'@'localhost';
GRANT ALL ON *.* TO 'user_user'@'%';

create database user_database;

grant usage on *.* to user_user@'localhost' identified by 'user_password';
grant usage on *.* to user_user@'%' identified by 'user_password';

grant all privileges on user_database.* to user_user@'localhost' ;
grant all privileges on user_database.* to user_user@'%' ;

And if you are writing code on Qt, below code works for me

    QSqlDatabase db;

    db = QSqlDatabase::addDatabase("QMYSQL");

    db.setHostName(DBHost);
    db.setDatabaseName(DBName);
    db.setUserName(DBUserName);
    db.setPassword(DBPassword);
    db.setPort(DBPort);


    if (!db.open())
    {
        QMessageBox::critical(this,"Error","Could not connect to database.");
    }
Austin
  • 1,305
  • 12
  • 30
  • Thanks for your comments, Kamal. The reason I am not using the Qt way to access database is when I run `ldd ~/Qt/5.9/gcc_64/plugins/sqldrivers/libqsqlmysql.so`. I found `libmysqlclient.so.18 => not found`. Which force me either find the missing library or rebuild the Qt mysqldriver. Then I had some problems to build the driver, which is another story. – r0ng Aug 02 '17 at 00:24
  • If you are on ubuntu, you can install libmysqlclient-dev package. It should get you libmysqlclient.so.x – Austin Aug 02 '17 at 05:23
  • Yes, you are right. But the version is different. Curently, it is libmysqlclient.so.20. Whereas, Qt 5.9.1 requires libmysqlclient.so.18. – r0ng Aug 02 '17 at 07:03
  • It might be solved by creating a symbolic link. But I really do not want to do that. – r0ng Aug 02 '17 at 07:06