简体   繁体   中英

How do I set up a embedded MySQL client in Qt

I'm trying to set up my embedded Linux machine as a MySQL client, in order to connect to a external MySQL server (running on a remote machine). sqlite is not an option.

I understand, thanks to Basile Starynkevitch that I have to use libmysqlclient (because that is the only library to run such a connection and dealing with the MySQL client/server protocol on the client side).

Qt stats that:

You need the MySQL header files and as well as the shared library libmysqlclient.so. Depending on your Linux distribution you may need to install a package which is usually called "mysql-devel".

Did someone did this and can point out to the right package?

Yes, assuming what you want to do is use the QtSql API to access a MySQL database without connecting to an external MySQL server. With the embedded server library, the server runs in the same process as your client Qt Application, similar to how SQLite works.

One caveat though: the libmysqld embedded server library is deprecated as of MySQL 5.7.17 and will be removed in MySQL 8.0. (as mentionned on http://dev.mysql.com/doc/refman/5.7/en/libmysqld.html )

Your question is confusing and seems contradictory.

Either you want to work with an external MySQL server, and that means that your application opens a ( tcp(7) socket) connection to some remote machine running mysqld . Then you have to use libmysqlclient (because that is the only library to run such a connection and dealing with the MySQL client/server protocol on the client side).

If that mysqld server is running as a different process on the same embedded Linux system you should have some way to start it (probably as part of the init scripts on it). Then you still use a socket communication to it, and you still need libmysqlclient . A possible difference with a remote machine running mysqld might be (but I am not sure) the socket family. Perhaps libmysqlclient is using unix(7) sockets in the special case of connecting to a server on the same machine.

Or you don't want any external server. You might consider libmysqld but as Romain answered it is deprecated and is becoming unsupported (so I feel that would be a very bad choice). Then all the database code is running on your embedded Linux computer, which also has all the data storage. In that case (relational database & data & storage on the same embedded Linux computer ), I would recommend using sqlite instead because it is well supported and quite stable.

If your mysqld daemon is running on a remote machine you cannot (realistically) avoid libmysqlclient (otherwise you'll need to rewrite most of it).

I ended up doing the following:

  1. Installing MySQL on my embedded Linux and tested it with

     mysql --host=1.2.3.4 --user=Foo --password=FooPass testdb 

    When an MySQL server is runing on 1.2.3.4.

  2. I recompiled Qt with the -sql-mysql option, so the new compiled version will include Qt MySQL plugin.

  3. Test if Qt MySQL plugin is supported with the next code:

     QStringList driverslist = QSqlDatabase::drivers(); QString str; foreach (str, driverslist) { qDebug() << str } 

    Expected output is:

     QMYSQL3 QMYSQL 
  4. Test that driver loaded as expected with the next code:

     QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("1.2.3.4"); db.setDatabaseName("TestDB"); db.setUserName("Foo"); db.setPassword("FooPass"); bool ok = db.open(); if(ok) { // "Connected" } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM