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

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?

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.

cx_Oracle fail to connect to one database using different machine

I am trying to connect to ORACLE database using cx_Oracle with python.
Code shown below. Same code able to connect to target database while running on a pc, however not able to connect using a server.
Error message: ORA-12545: Connect failed because target host or object does not exist
Since it can be connected use other pc, I think should not be the password or service name wrong.
The server having issue connecting to the database is a linux, with cx_Oracle installed, and ORACLE_HOME, LD_LIBRARY_PATH defined.
Any one can give a hint on what might goes wrong?
code used
dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='ServiceName')
conn = cx_Oracle.connect(user=r'UserName', password='Password', dsn=dsn_tns)
c = conn.cursor()
Found out it was due to network access issue.

How to interact with database file by c++

I am new to database programming.
I am trying to make an application that interacts with a database file locally so that I can use those database query.
I tried to use mySQL connection c++ 1.1.6, and run this example.
Following error is what I get:
MySQL server on '127.0.0.1' <10061> <MySQL error code: 2003, SQLState:>
I guess that I need to have a server on for connecting. What I want is just an interaction with a database file locally, do I need to make this connection also? If I really need this connection, how to make it works?

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.

Connect to SQLite using ODBC without register database

Is it possible to connect to SQLite in C++ and using ODBC API without register the database in ODBC?
I have code that uses ODBC talking to databases and don't want to rewrite for using SQLite and don’t want to register new ODBC connections.
you should be able to do it without any code changes using sqliteodbc.
Short answer, use one of the following connection strings:
Driver=SQLite ODBC Driver;Database=full-path-to-db;...
Driver=SQLite3 ODBC Driver;Database=full-path-to-db;...
Long answer: The readme file included with the sqliteodbc driver covers the methods for connecting, using a DSN-less connection string.
See http://www.ch-werner.de/sqliteodbc/html/index.html