mysql c++ connector exceptions: Access denied for user 'root'@'localhost' (using password: NO) && MySQL server has gone away -
i trying using mysql c++ connector connect mysql database on local machine.
i installed mysql c++ connector 7.1.1.9 , boost. in c++ ide. included header path , connector library file libmysqlcppconn-static.a
project. project compiles fine.
but when try run example connect local database.
try{ sql::mysql::mysql_driver *driver; sql::connection *con; driver = sql::mysql::get_mysql_driver_instance(); // tried user root here, 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 // access denied exception thrown here. std::cerr << e.what() << '\n'; }
an exception
mysql exceptions: access denied user 'root'@'localhost' (using password: no)
thrown. checked mysql installed correctly accessing in terminal:
$ mysql -u root -p enter password: // <---entered "password" here, sure password correct welcome mysql monitor. commands end ; or \g. mysql connection id 6 server version: 5.7.18-0ubuntu0.16.04.1 (ubuntu) copyright (c) 2000, 2017, oracle and/or affiliates. rights reserved. oracle registered trademark of oracle corporation and/or affiliates. other names may trademarks of respective owners. type 'help;' or '\h' help. type '\c' clear current input statement. mysql>
it seems connector ignoring second parameter. tried use structure connectoptionsmap
set ['password'] "password". still not working properly.
suggestion fix exception?
a little after connection granted: had difficult time using binary libraries (libmysqlcppconn.so / libmysqlcppconn-static.a) downloaded oracle mysql official website. if execute
con->setschema("an_existing_db")
app throws exception: mysql server has gone away
;
if use :
sql::statement *st = con->createstatement(); st->execute("use an_existing_db");`
the app crashes due bad allocation.
eventually, solved problem using packages apt-get (they version of sqlcppconn).
sudo apt-get install libmysqlcppconn-dev
changed project file link libraries in /usr/lib/libmysqlcppconn.so
, /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
in /etc/mysql/my.cnf, there bind-address line. please set to
bind-address = 0.0.0.0
then restart mysql service.
next try execute program.
also while creating mysql database, user make sure permissions proper, can refer below instructions (valid on linux / ubuntu)
create user 'user_user'@'localhost' identified 'user_password'; create user 'user_user'@'%' identified 'user_password'; grant on *.* 'user_user'@'localhost'; grant on *.* 'user_user'@'%'; create database user_database; grant usage on *.* user_user@'localhost' identified 'user_password'; grant usage on *.* user_user@'%' identified 'user_password'; grant privileges on user_database.* user_user@'localhost' ; grant privileges on user_database.* user_user@'%' ;
and if writing code on qt, below code works 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 database."); }
Comments
Post a Comment