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.
First of all you need to have MySQL/MariaDB installed on your system, we show this in the correct way in the article:
You can find other necessary concepts in the article:
Link in Portuguese!
Which describes the differences between: MySQL Connector C and 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
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.
sudo apt remove --purge libmysqlcppconn-dev libmysqlcppconn7v5 -y
sudo apt autoremove
Linux - Generic
All
(mysql-connector-c++-X.X.X-linux-glibcX.XX-x86-64bit.tar.gz)
tar zxvf mysql-connector-c++-*.tar.gz
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/
.
sudo ldconfig
If you chose a different path:
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib64/' >> ~/.bashrc && source ~/.bashrc
.
#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;
}
}
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
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!