Establishing a MySQL connection without the MySql Connector for C++ - c++

How can I connect to a MySql server from my MFC C++ (MSVC2013) without the MySql Connector?
I think it seems to be a stupid question, but no, I haven't found anything about this on Google, and the Oracle connector is very buggy and is a pain...

You should be using CDatabase class to connect to your DB. Here is the example:
CDatabase cDb;
CString strConnectionString = _T("ODBC;DSN=DrumisD4WData;DRIVER={SQL Server};SERVER=DEV001-W2K8\\DRUMIS64;DATABASE=master");
TRY
{
cDb.OpenEx(strConnectionString, CDatabase::noOdbcDialog);
cDb.Close();
}
CATCH(CDBException, e)
{
AfxMessageBox(e->m_strError);
}
END_CATCH;

Related

How to Use CDatabase to connect to SQL Server database with ODBC?

My program uses ADO to connect to SQL Server in Visual C++ 2008. Now it seems that ADO is out-dated and MS recommends to use ODBC again.
Therefore, I now study how to use ODBC to connect to SQL Server. I see there is a class CDatabase that can do that. However, there are no good article in introducing how to use CDatabase to connect to SQL Server via ODBC.
Basd on my research, it seems one can connect to SQL Server via ODBC in the following way:
Via a direct connection string like this one:
Driver={SQL Server Native Client 11.0};Server=myServerAddress;
Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Using ODBC administrator to create a DSN(Data Source Name), and then connect with DSN.
THen if 1 works, why do we need to create DSN?
You don't need a ODBC DSN. A DSN simply allows one to externalize the ODBC configuration such that server name, driver. etc. can be configured in a common way for all ODBC applications.
It is not a requirement to use a DSN when you store connection string in an external file so that environment-specific values can be configured without code changes. Regardless of the technique used, be sure to protect secrets rather than storing as clear text.

How to connect to a MySQL database through ODBC from Qt application?

I have a freshly-installed MySQL server, which listens on localhost:3306. What is the correct way to connect to it from my Qt application?
It turned out that I need to add MySQL to the ODBC data sources. I did that after following this video tutorial - https://youtu.be/K3GZidOwGmM.
After I had added the DSN, I successfully connected to the MySQL server using this code:
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={MySQL ODBC 5.3 Unicode Driver};DATABASE=test;");
db.setUserName("root");
db.setPassword("password");
if (!db.open()) {
qDebug() << db.lastError().text();
} else {
qDebug("success");
}
Note: You will need to replace MySQL ODBC 5.3 Unicode Driver with the actual value listed in your DSN window. I got mine from here:

c++ mysql connection failure

I try to connect to my MySql database with c++ but it won't connect and I cant figure out why. The Instance of the MYSQL object was initialised and I also tried to connect to my database with Java and Php and it worked...
The Mysql server is not hosted by me, it's hosted by an German server provider so I suggest on the server side will be everything ok.
Also the libs and includes are linked correct since the initialisation of my MYSQL instance works fine.
The mysql version I use is 5.0.X
this is the line of code where I try to connect to the database:
MYSQL *connect = mysql_init(NULL);
mysql_real_connect(connect, "xxxxx.db.1and1.com", "dboXXXXXX", "XXXXXX", "dbXXXXXX", 0, NULL, 0);

mysql/c++ connector error: Connection using old (pre-4.1.1) authentication protocol refused

So I am a newbie for C++/Mysql and I am sorry if I ask something stupid.
I have encountered the error for connecting mysql from c++
SQLException: Connection using old (pre-4.1.1) authentication protocol refused (client option 'secure_auth' enabled)
I read many existing solutions for this problem and they seem to suggest restarting the server with some updated settings. However, I am trying to connect to a remote server, and I do not have the root access to that server.
Here are some more details on my server, client and connector:
The server version is 5.5.32
The client version is Ver 14.14 Distrib 5.1.69, for redhat-linux-gnu (x86_64) using readline 5.1. It is strange since this is newer than 4.1.1
I also installed MYSQL/C++ Connector 1.1 . I downloaded its source code and built it using CMake without error.
I guess that a simple fix would be to disable "secure_auth" from the connector. However, I don't know how to do this. Is there some parameters that I can pass from my C++ code?
So does anyone have any suggestions? Thanks!
I had the same problem and solved it by using Connector/C++ Connection Options.
sql::Driver *driver;
sql::Connection *con
driver = get_driver_instance();
sql::ConnectOptionsMap connectionProperties;
connectionProperties["hostName"] = sql::SQLString("tcp://192.168.0.10:3306");
connectionProperties["userName"] = sql::SQLString("root");
connectionProperties["password"] = sql::SQLString("password");
connectionProperties["useLegacyAuth"] = true;
con = driver->connect(connectionProperties);
Make sure you have connectionProperties["useLegacyAuth"] = true; and that works for me.
Pre 4.1.1 refers to the password you're trying to authenticate against. If you can't change that to use the more modern (longer) scheme, you can try
Using an older client that doesn't have secure_auth enabled by default,
Finding where to pass skip_secure_auth (may or may not work), or
Patch your client with the one-line fix at http://bugs.mysql.com/bug.php?id=75425 so that secure_auth=FALSE works
Options 2 and 3 are easy if you can use a .cnf file to connect.

Is MySQL C++ Connector access to remote database possible?

I am accessing a MySQL database within a C++ app using MySQL C++ Connector. It works fine if I have the C++ and the MySQL on the same machine. So, something like the following code works fine:
sql::Connection *_con;
sql::mysql::MySQL_Driver *_driver;
_driver = sql::mysql::get_mysql_driver_instance();
_con = _driver->connect("tcp://127.0.0.1:3306", "user", "password");
However, I can't seem to access the database if it is located on another machine. So, something like this:
sql::Connection *_con;
sql::mysql::MySQL_Driver *_driver;
_driver = sql::mysql::get_mysql_driver_instance();
_con = _driver->connect("tcp://somesite.com:3306", "user", "password");
Is it just not possible or am I doing something wrong?
Did you accidentally setup your users so that they can only access your DB from the local machine?
Did you do
create user 'user'#'127.0.0.1' ...
or
create user 'user'#'%' ....
If you did the first then you won't be able to log on from a different machine.
Did you also grant the privileges correctly?
See the MySQL docs for a more in depth explanation on how to do this correctly
I have done this through a VPN so I am assuming it is possible. Are you using the correct port?