How to Connect MySQL with C/C++ on Ubuntu and Any Distro
Both with Connector C and Connector C++
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:
To test, create a test code:
Compile and run:
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:
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:
04. Enter the unzipped directory and move the internal directories according to the commands below:
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:
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:
07. But to compile and run, use the following command:
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
:
And also if you want the description for pkg-config --list-all
to appear, just create this file:
And add this content inside:
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:
MySQL Connector C Ubuntu
To install MySQL Connector C on Ubuntu you can use the APT package manager:
To test the connection, create a test file:
main.cpp
Compile and run:
Alternatively you can install directly:
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/
Comments