0

Hi2,

Does anyone know whats wrong with this code?

I want to connect the MySQL database with Qt program in Linux Ubuntu.
I use XAMPP for the mysql. I have make sure that the name, pass, port, database name are all right. And the mysql in xampp is starting.

However, it just keeps not opening. When i tried the same code in Windows, it works just fine

void MainWindow::CreateDatabaseConnection()
{

  QSqlDatabase db;
  db = QSqlDatabase::addDatabase("QMYSQL");
  db.setHostName("localhost");
  db.setUserName("root");
  db.setPassword("");
  db.setPort(3306);
  db.setDatabaseName("myDatabase");

  //test connection
  if(db.open())
      qDebug()<<"database connected ";
  else
      qDebug()<<"database failed to connect ";

  qDebug() << db.lastError();
}

ok i add the "db.lastError", the console output now says:

database failed to connect QSqlError("2002", "QMYSQL: Unable to connect", "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")


Have anyone encounter the same problem?

Hikari
  • 458
  • 1
  • 11
  • 27
  • 1
    The code is correct, theres nothing more to it. Check your environment - it appears your mysql server is not running or at least not listening on localhost. – markus-nm Feb 19 '19 at 13:11
  • I have checked, and the mysql and apache are running.. – Hikari Feb 19 '19 at 16:16
  • First, leave out db.setPort(), since you are using the default anyway. When connecting to localhost, the mysql-client might be using a local file-socket instead, so there might be a file-permission issue. Try to run your application as super-user. If that doesnt work try a non-localhost mysql-server, e.g. on a virtual machine that is NAT'ted into your local network. If that also doesnt work, try to use the mysql-client binary that comes with libmysql-client. I'd be surprised to see it work when Qt doesnt - in case it doesnt, you'll probably want to re-compile the mysql-client lib. – markus-nm Feb 20 '19 at 08:49
  • I don't use xampp. Maybe check that the server is on/started? (or is that done automatically...) I get the exact same error if I haven't started the MySQL server. Just a different file path: "/tmp/mysql.sock". (N.B. I run MySQL 5.7 on the terminal on a macOS.) – TrebledJ Feb 20 '19 at 12:36
  • @markus-nm : I have delete the "setPort()". afterwards I release the qt application, I run it on the linux terminal as a Super User. but still no luck. the same error message – Hikari Feb 20 '19 at 15:07
  • Try to connect using the command-line tools of the mysql-client package. If it's already installed, simply entering "mysql" into the terminal should start a client connection to localhost (and fail with access denied if root has a password). If it fails with the error message provided in your question, then there is no mysql server running or it was misconfigured. In that case you should try to re-install mysqld. – markus-nm Feb 20 '19 at 18:23

1 Answers1

1

I dont know why but after i changed this line to

db.setHostName("localhost");

into

db.setHostName("127.0.0.1");

It can connect properly.
Can someone please explain this why it works with 127.0.0.1 but not with localhost...?
I have both of them configured in PHPmyadmin.

Hikari
  • 458
  • 1
  • 11
  • 27
  • It's happened for me too!! (this link maybe usefull https://stackoverflow.com/questions/7382602/what-is-the-difference-between-127-0-0-1-and-localhost) – Saeed Masoomi May 30 '19 at 19:54