How to Connect MySQL with C/C++ on Ubuntu and Any Distro

Both with Connector C and Connector C++


How to Connect MySQL with C/C++ on Ubuntu and Any Distro


In this article we will see how to connect to MySQL with C and C++ in Ubuntu and also an installation procedure that works for any [GNU] distro(https:// terminalroot.com/tags#gnu)/Linux.


Installation

First of all you need to have MySQL/MariaDB installed on your system, we show this in the correct way in the article:

How to install MariaDB/MySQL on Ubuntu 22.10

You can find other necessary concepts in the article:

How to Connect to MySQL with C/C++ on Windows

Link in Portuguese!

Which describes the differences between: MySQL Connector C and MySQL Connector C++!

MySQL Connector C++ METHOD 1 | Ubuntu

To install MySQL Connector C on Ubuntu you can use the APT package manager:

sudo apt install libmysqlcppconn-dev libmysqlcppconn7v5

To test, create a test code:

#include <iostream>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <mysql_connection.h>

auto main() -> int {
   sql::Driver *driver = get_driver_instance();
   try {
     std::unique_ptr<sql::Connection> con(
         driver->connect("HOST", "USER", "PASSWORD")); // FILL IN HERE
     if (con != NULL) {
       std::cout << "MySQL Connected successfully!\n";
     }
   } catch (sql::SQLException &e) {
     std::cerr << "MySQL was NOT started or Incorrect credentials.\n";
     return 1;
   }
}

Compile and run:

g++ main.cc -lmysqlcppconn
./a.out

MySQL Connector C++ METHOD 2 | Any Distro

You can also install manually if you are on another distro, or on a version that has a problem using APT.

01. Remove the packages from your system if any, and clean them later:

sudo apt remove --purge libmysqlcppconn-dev libmysqlcppconn7v5 -y
sudo apt autoremove

02. Download the Binary at: https://dev.mysql.com/downloads/connector/cpp

  • Choose: Select Operating System: Linux - Generic
  • And in the select below: Select OS Version: All
  • Click on the file: Archive: (mysql-connector-c++-X.X.X-linux-glibcX.XX-x86-64bit.tar.gz)
  • Then click: No thanks, just start my download. and the download will be carried out!

03. After downloading, unzip the downloaded file:

tar zxvf mysql-connector-c++-*.tar.gz

04. Enter the unzipped directory and move the internal directories according to the commands below:

cd mysql-*
sudo mv include/jdbc/* /usr/include/
sudo mv lib64/* /usr/lib/x86_64-linux-gnu/

If you want, choose a different path: sudo mv lib64/* /usr/lib64/.

05. Now allow GNU GlibC to understand your installation, to do so, run the command below:

sudo ldconfig

If you chose a different path: echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib64/' >> ~/.bashrc && source ~/.bashrc.

06. To test, use the same test code:

#include <iostream>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <mysql_connection.h>

auto main() -> int {
   sql::Driver *driver = get_driver_instance();
   try {
     std::unique_ptr<sql::Connection> con(
         driver->connect("HOST", "USER", "PASSWORD")); // FILL IN HERE
     if (con != NULL) {
       std::cout << "MySQL Connected successfully!\n";
     }
   } catch (sql::SQLException &e) {
     std::cerr << "MySQL was NOT started or Incorrect credentials.\n";
     return 1;
   }
}

07. But to compile and run, use the following command:

g++ main.cpp -lmysqlcppconn
# Or: g++ main.cpp /usr/lib64/libmysqlcppconn.so.9
./a.out

If it doesn’t work, try compiling with the flag: -pthread.

If you chose an alternative path (/usr/lib64) and don’t want to add content to ~/.bashrc you can also create a symbolic link to use the flag:

sudo ln -s /usr/lib64/libmysqlcppconn.so.9 /usr/lib/x86_64-linux-gnu/libmysqlcppconn.so.9

And also if you want the description for pkg-config --list-all to appear, just create this file:

sudo vim /usr/lib/pkgconfig/mysqlcppconn.pc

And add this content inside:

prefix=/usr
exec_prefix=${prefix}
libdir=/usr/lib64
includedir=${prefix}/include

Name: mysqlcppconn
Description: MySQL Connector/C++ is a MySQL database connector for C++.
URL: https://github.com/mysql/mysql-connector-cpp
Version: 8.1.0
Libs: -L"${libdir}" -lpthread
Cflags: -I"${includedir}" -I/usr/local/include
#Requires.private: @GLFW_PKG_DEPS@
#Cflags: -I"${includedir}" -I/usr/local/include

See the content: pkg-config --list-all | grep mysqlcppconn

You can also compile from scratch, just clone the repository: https://github.com/mysql/mysql-connector-cpp and follow the procedure listed here.

If you want to uninstall EVERYTHING:

sudo unlink /usr/lib/x86_64-linux-gnu/libmysqlcppconn.so.9 2>/dev/null
sed -i '/LD_LIBRARY_PATH/d' ~/.bashrc
cd /usr/include/
sudo rm -rf cppconn/ mysql_*
cd /usr/lib/x86_64-linux-gnu/ # Or if you chose an alternative directory: cd /usr/lib64/
sudo rm -rf libmysqlcppconn8.so libmysqlcppconn8.so.2 libmysqlcppconn8.so.2.8.1.0 \
             libmysqlcppconn.so libmysqlcppconn.so.9 libmysqlcppconn.so.9.8.1.0 plugin private
sudo rm /usr/lib/pkgconfig/mysqlcppconn.pc

MySQL Connector C Ubuntu

To install MySQL Connector C on Ubuntu you can use the APT package manager:

sudo apt install libmysqlclient-dev

To test the connection, create a test file:

main.cpp

#include <iostream>
#include <mysql/mysql.h>

auto main() -> int {
   MYSQL *connect = mysql_init(NULL);
   !connect ? std::cerr << "MySQL has NOT been started.\n"
            : std::cout << "MySQL Connected successfully!\n";
   mysql_close(connect);
}

Compile and run:

g++ main.cpp -lmysqlclient
./a.out

Alternatively you can install directly:

wget -q https://terminalroot.com.br/downs/libmysqlclient-dev-8.0.34-0ubuntu0.23.04.1-amd64.tar.gz
tar zxvf libmysqlclient-dev-8.0.34-0ubuntu0.23.04.1-amd64.tar.gz
cd libmysqlclient-dev-8.0.34-0ubuntu0.23.04.1-amd64
sudo mv include/mysql/ /usr/include/
sudo mv lib/libmysqlclient.* /usr/lib/x86_64-linux-gnu/

We performed a CRUD, in addition to other tips with C++ using the MySQL Connector C in the Advanced Modern C++ Course. Purchase to get full content!


Useful links

  • https://dev.mysql.com/doc/connector-cpp/1.1/en/connector-cpp-examples-connecting.html
  • https://dev.mysql.com/doc/connector-cpp/8.1/en/connector-cpp-apps-general-considerations.html
  • https://dev.mysql.com/doc/connector-cpp/8.1/en/connector-cpp-installation-binary.html
  • https://dev.mysql.com/downloads/c-api/
  • https://dev.mysql.com/downloads/connector/cpp/
  • https://dev.mysql.com/downloads/installer/
  • https://downloads.mysql.com/archives/c-c/

mysql sql cpp clanguage


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles