From any ip address and browser mysql database can be connected and operated without any problems. But from my Qt application database can only be accessed when hostname is set to setHostName("localhost") not when ip of the device is entered.
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
bool ok = db.open();
I get the error:
QSqlQuery::exec: database not open
When "127.0.0.1" is replaced with "localhost" connection is established. I checked and inserted relevant data to /etc/hosts but still issue occurs. So I can not type ip address and success with my Qt application code. Any suggestions ?
Related
I have a C++ application that used to read the data from the Firebird SQL server.
when I changed port from 3050 to 3053 it shows error like
DB Error : 0 : Unable to complete network request to host "192.168.1.47".
Failed to establish a connection.
unknown Win32 error 10060
Invalid connection string attribute
conf file is changed like
# Type: string, integer
#
RemoteServiceName = gds_db
RemoteServicePort = 3053
Fb connection string is
Driver=Firebird/InterBase(r) driver;DBNAME=192.168.1.47:CWNPFB;PORT=3053;UID=SYSDBA;PWD=********
Is there any modification is required to solve this? Application can read the data if the port is 3050.
The problem is with your connection string:
You need to
Remove PORT=3053 from the connection string (this causes the "Invalid connection string attribute" message)
Modify DBNAME=192.168.1.47:CWNPFB to DBNAME=192.168.1.47/3053:CWNPFB (to specify the right port)
You might also want to comment out (or remove) the line RemoteServiceName = gds_db, because you are now instructing Firebird to listen on gds_db (== port 3050), and not on port 3053. I believe it usually listens on the last one configured in the config file, but I'm not sure that is always the case.
Is your firewall configuration correct for port 3053? Another service may runs on this special port.
You could do the following steps.
Try to run the application on the database server, please change the IP to 127.0.0.1 or localhost.
If step (1) works: check out the firewall
If step (1) doesn't work: maybe try another port, check whether Firebird is running
I am developing a test app to understand how to do SSH tunnelling to connect to a MySQL Database in C++.
I am using the libssh2 library and I am using an example from https://www.libssh2.org/examples/direct_tcpip.html but I am not 100% sure whether or not this is the correct thing to use.
I've pretty much copied the example but when connecting to MySQL on my socket, MySQL throws:
Errro 2013 (HY000): Lost connection to mysql server at 'reading communication packet', system error: 0
When I connect to mysql using mysql -uroot -p -P2222 my app reads data on the channel using the following:
int len = libssh2_channel_read(channel, buf, sizeof(len));
and the buf contains SSH-2.0- and then this is written to the forwarding socket as follows:
wr = 0;
while (wr < len)
{
i = send(forward_socket, buf + wr, len - wr, 0);
if (i <= 0)
{
perror("write");
return EXIT_FAILURE;
}
wr += i;
}
As soon as the send is done, I instantly get the mysql error. I assume it is because I am sending SSH-2.0- to MySQL which MySQL isn't expecting so its closing the connection but I can't see what's wrong, and I can't find for certain whether or not libssh2 direct_tcpip is the correct thing to use.
Finally, managed to figure out what to do, with lots of trial and error and hair pulling managed it.
Using the example from https://www.libssh2.org/examples/direct_tcpip.html but I was setting the variables with the wrong value.
Basically, in the example it has
const char *remote_desthost = "localhost"; /* resolved by the server */
unsigned int remote_destport = 22;
Because of remote_destport in the example being 22, I thought this was the SSH connection details so I set this to be my SSH settings.
It turns out this is where the connection gets forwarded to from the SSH session so I changed it to be
const char *remote_desthost = "localhost"; /* resolved by the server */
unsigned int remote_destport = 3306;
So now I can run my app from my laptop, which connects to my SSH server for my web server and then on my laptop run in the command
mysql -uroot -p -P2222 and I connect to the database on my webserver through the SSH tunnel.
I think you have misunderstood what SSH Tunnelling is.
SSH Tunnelling is nothing more than a Client Socket to Server Socket Mapping E.G a Firewalled Network port.
And to show that this is how it works. i used my personal web server and connected to a MySQL server that does not allow any external connections to it so connections from localhost only.
To Setup a SSH Tunnel using LibSSH2 follow the code at http://api.libssh.org/master/libssh_tutor_forwarding.html under heading Doing direct port forwarding with libssh
If you don't want to expose the mapping to a port on your client machine you don't necessarily have to you could manually send data down the channel and read it however if there is any form of encryption or text encoding that does not match your on the channel to mysql you will send invalid data to it.
So once you have SSH Tunnelling created then you use it as a standard network connection to the port you have Tunnelled to talk to the service if you wish to do this in C++ use the MySQL C++ Connector and once the tunnel is create connect using the C++ Connector.
If you wish to allows your application to talk to MySQL though the SSH channel without exposing it to a network port, you probably gonna have to mess with the source code for the C++ MySQL Connector. A lot, more work that you think it is you would be going though the entire connector and any writing and reading it does via the Network Socket and replace it with code to go though your SSH Tunnel Channel.
I'm trying to connect a Qt program to a database existing in another computer.
I'm using Ms Access database and i already had it connected to my programme in the same computer ( with the QODBC driver) as below:
db1 = QSqlDatabase::addDatabase("QODBC");
db1.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=BDProjet.mdb");
now i'm trying to extend my programme to be a localnetwork programme so i need to connect my programme to the same database from another computer, so for testing it i'm using the loopback adresse (127.0.0.1) , i've already tried this but it's not working:
db1 = QSqlDatabase::addDatabase("QODBC");
db1.setHostName("localhost");
db1.setPort(3306);
db1.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=j\\BDProjet.mdb");
How i'm supposed to connect my database?
Try adding;
if(!db1.open())
{
// aaaargh!
qDebug() << db1.lastError();
}
else
{
// your database commands here
}
after the code that you have.
I writing a c++ program that should connect to a mysql database.
it works successfully when I use the local database, but I get an error
"Can't connect to mysql database on '192.168.0.111' (111)"
when I try to connect to a database on another computer.
this is the function that test my connection:
void addb()
{
string mainServer="192.168.0.111";
string mainDbUser="root";
string mainDbPass="111";
MYSQL *connect; //database connection variable
connect=mysql_init(NULL);
if(!connect)
cout<<"Couldn't initiate connector\n";
if (mysql_real_connect(connect, mainServer.c_str(), mainDbUser.c_str(), mainDbPass.c_str(), "main" ,0,NULL,0))
{
cout<<" done\n";
}
else
{
cout<<mysql_error(connect)<<endl;
}
mysql_close (connect);
}
both computers are running Linux Mint OS.
I tried disabling my firewall on the second computer but i got the same problem.
note: I can access the database using phpmyadmin with my web browser.
Make sure the firewall allows MySQL port on the DB machine.
Make sure in the my.cnf the database is loaded with you have the proper network configuration (to listen to the public IP).
From the error message - I would suspect it is a firewall issue - so make sure the DB machine firewall allows incoming communication (and specifically - that SELinux enables it in addition to the firewall) and that the sending machine allows outgoing communication to this machine.
I'm using the C++ connector and one of the example code that goes along with it and it wont allow external connections to connect to my database when i change the host address from localhost to my computer's address where the server is running.
This connects perfectly without any problems:
con = driver->connect("tcp://127.0.0.1:3306", "Shawn", "my_pass");
But obviously this wont work when i change computers, so i set the host address to my computer where the server is running like this:
con = driver->connect("tcp://108.xxx.xxx.xxx:3306", "Shawn", "my_pass");
*x's stand for numbers to hide my IP
Why won't it connect?
I allowed Norton 360(takes over windows firewall) to accept any and all connections and ports from my program.
I gave the user "Shawn" the privledges necessary to accept all hosts via "%":
http://i.imgur.com/F92qeua.png
I get this mysql error:
http://i.imgur.com/OMAhtpg.png
You can also pass Socket parameter with this connection string.
or
you can try with the hostname instead of ipaddress.
if still youre issue is not resolved .Firewalls/iptables are enabled at server side so u need to stop it.