Connection attempt failed MySQL poco - c++

Another question on a Mysql connection failing:
I'm using the Poco library (1.5.2) and I would like to know why, when I try to open a MySQL connection, I got this message:
Connection attempt failed
Whereas, when I try a connection via console (mysql -u root -p ...), it works.
Maybe I forget an important step in the MySQL configuration ?
Here is my code :
#include <iostream>
#include <string>
#include <Poco/Data/MySQL/MySQLException.h>
#include <Poco/Data/MySQL/Connector.h>
#include <Poco/Data/SessionFactory.h>
using namespace std;
int main()
{
Poco::Data::MySQL::Connector::registerConnector();
try
{
string str = "host=localhost;user=root;password=mypassword;compress=true;auto-reconnect=true";
Poco::Data::Session test(Poco::Data::SessionFactory::instance().create(Poco::Data::MySQL::Connector::KEY, str ));
}
catch (Poco::Data::MySQL::ConnectionException& e)
{
cout << e.what() << endl;
return -1;
}
catch(Poco::Data::MySQL::StatementException& e)
{
cout << e.what() << endl;
return -1;
}
return 0;
}
Thank you !!

ok the problem was the "localhost" value for "host" doesn't work on my linux (I don't know why). For fixing the bug, I had to change my string to:
string str = "host=127.0.0.1;user=root;password=mypassword;compress=true;auto-reconnect=true";

Related

PostgreSQL unable to access

I can't access database from C++
every time it just throws error: FATAL: Ident authentication failed for user "testuser"
I had tried:
Reinstalling postgresql11 to postgresql12
Creating user
Creating database with owner testuser
Changing pg_hba.conf localhost from peer to md5
Changing the password of user multiple times
Rebooting computer
Restarting Service
here is the code:
#include "gtest/gtest.h"
#include <iostream>
#include <pqxx/pqxx>
using std::cin;
using std::cout;
using std::cerr;
using namespace std;
using namespace pqxx;
int main() {
try {
connection C("dbname = relay user = testuser password = 1234 hostaddr = 127.0.0.1 port = 5432");
if (C.is_open())
cout << "Opened database successfully: " << C.dbname() << endl;
else {
cout << "Can't open database" << endl;
return 1;
}
C.close();
} catch (const std::exception &e) {
cerr << e.what() << std::endl;
return 1;
}
}
TEST(postgreDB_test_trivial, trivial_test) {
main();
}
I allowed even firewall access for ports
I don't have any more ideas on how to resolve this.
The problem was that uninstalling Postgres11 and installing Postgres12 left pg_hba.conf with different setting after I restarted service so I needed to change everything to trust in pg_hba.conf to get it to work

Getting started with MongoDB C++ driver in Windows

Trying to set up a simple MongoDB database connection in Windows 7, using the C++ driver. I'm using Visual C++ compiler 19 for x86, 32-bit MongoDB 3.0.6, Boost 1_59_0, Mongo legacy 1.0.5 C++ driver.
Driver compiles OK using the command
scons --cpppath=d:\boost_1_59_0 --libpath=d:\boost_1_59_0\stage\lib --msvc-host-arch=x86 install
Program is
#include <cstdlib>
#include <iostream>
using namespace std;
#include <WinSock2.h>
#include <windows.h>
#include "mongo/client/dbclient.h"
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
Program compiles using
cl /EHsc /I"c:\mongo-cxx-driver-legacy-1.0.5\build\install\include" /I"d:\boost_1_59_0" /DSTATIC_LIBMONGOCLIENT mdb.cpp c:\mongo-cxx-driver-legacy-1.0.5\build\install\lib\libmongoclient-s.lib /link /LIBPATH:"D:\boost_1_59_0\stage\lib" ws2_32.lib
But when I run the program, get the error message
caught can't connect couldn't initialize connection to localhost, address is invalid
The server is running OK as I can access it through the shell, add records etc.
This is my first time programming MongoDB and I'm kind of stuck. Any suggestions?
OK, problem solved (thanks to stevepowell.ca/mongo-db-1.html). Here's the answer for anyone else who runs into this problem:
Windows needs to initialize the client before setting up the connection.
#include <cstdlib>
#include <iostream>
#include <WinSock2.h>
#include <windows.h>
#include <memory>
#include "mongo/client/dbclient.h"
using namespace mongo;
using namespace std;
void run() {
mongo::client::initialize(); // this line is new
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
I wish this had been in the tutorial!
Onwards and upwards.

connecting a c++ application to an Oracle database with sqlapi++

I wrote an example application in C++(on Eclipse Luna) using sqlapi++ libraries to connect to an Oracle database I created with sqldeveloper.
The program compiles without errors, but when I run it,nothing appears on the console.
(I am using Windows 7)
Here the Database information:
Database name:"DB Casa Editrice"
Host:localhost
SID:xe
Port:1521
And the code:
#include <iostream>
#include "SQLAPI.h"
using namespace std;
int main() {
SAConnection con; // create connection object
try {
con.Connect( "DB Casa Editrice", // database name
"system", // user name
"shruikan94", // password
SA_Oracle_Client );
cout << "We are connected!\n";
con.Disconnect();
cout << "Disconnected!\n";
}
catch( SAException &x )
{
// SAConnection::Rollback()
// can also throw an exception
// (if a network error for example),
// we will be ready
try {
// on error rollback changes
con.Rollback();
}
catch( SAException & )
{
}
// print error message
cout << "ERROR\n";
}
return 0;
}

SOCI C++ (SQL wrapper) FATAL: database "testDB" does not exist

Just another problem with Soci... I want to connect with my testDB which I've just created but the code below shows fatal error.
I did this:
On PostgreSQL:
1) CREATE USER robert WITH ENCRYPTED PASSWORD 'pass';
2) CREATE DATABASE testDB;
3) GRANT ALL PRIVILEGES ON DATABASE testDB TO robert;
My C++ code:
#include <iostream>
#include <soci.h>
#include <string>
using std::string;
using std::cout;
#include <postgresql/soci-postgresql.h>
bool connectToDatabase(string databaseName, string user, string password)
{
try
{
soci::session sql(soci::postgresql, "dbname=" +databaseName + " user="+user + " password="+password);
}
catch (soci::postgresql_soci_error const & e)
{
std::cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
return false;
}
catch (std::exception const & e)
{
std::cerr << "Some other error: " << e.what() << std::endl;
return false;
}
return true;
}
int main(int argc, char **argv)
{
cout << connectToDatabase("testDB", "robert", "pass");
return 0;
}
after compilation I got this:
Some other error: Cannot establish connection to the database.
FATAL: database "testDB" does not exist
What's wrong?
Connect to database "testdb" instead.
Case folding in PostgreSQL works by converting unquoted identifiers to lower case. So CREATE DATABASE testDB creates a database called "testdb", in contrast to CREATE DATABASE "testDB".
(As a general piece of advice: either get used to using all-lowercase identifiers, maybe with underscores between words, or get used to quoting identifiers all the time. The former is more natural for PostgreSQL, the latter allows you to stick with your convention despite it).

Simple server/client boost example not working

Learning boost, and compiled their daytime server client example. Since I cant use port 13 that is in the example I only changed the port numbers in the server and client example. Server runs fine, but the client doesnt connect it seems, and no error is given.
Input data for the client is "127.0.0.1".
Server:
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
int main()
{
try
{
boost::asio::io_service io_service;
tcp::endpoint endpoint(tcp::v4(), 8087);
tcp::acceptor acceptor(io_service, endpoint);
for (;;)
{
tcp::iostream stream;
acceptor.accept(*stream.rdbuf());
stream << "test" << make_daytime_string();
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
And the client:
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: daytime_client <host>" << std::endl;
return 1;
}
tcp::iostream s(argv[1], 8087);
std::string line;
std::getline(s, line);
std::cout << line << std::endl;
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
}
return 0;
}
What worked for me was to change the way I create the endpoint from
tcp::endpoint( tcp::v4(), port );
to
tcp::endpoint( boost::asio::ip::address::from_string("127.0.0.1"), port );
The first method creates an endpoint of 0.0.0.0 which works fine on Mac OS X, but gives the "not valid" message on Windows (XP, building with MSVC 2008).
I wouldn't mind knowing WHY the difference, but at least it works.
A few things would help to debug this for you:
What platform are you running
What compiler are your using, including version
What version of boost are you using
Also, one thing to check is whether the server is binding to 127.0.0.1 or the external interface. Try using the IP address of your external interface instead of 127.0.0.1. Check this in windows using ipconfig and in linux using ifconfig.
Hmm, all works on 1_36 boost version and msvc 2005 compiller.
Check your firewall settings.
The port option takes a string, which may be the name of the service, as "daytime", and then it will look up the corresponding port, or explicitly the port, but it must be a string:
tcp::iostream s(argv[1], "8087");