MySQL is the most popular open source database management system that is written in c++. It supports various workloads and cloud providers.
Aug 07, 2024
Go to mysql homepage, download and install mysql:
https://dev.mysql.com/downloads
After installing, you should have mysql root password configured, the installing process might have guided you to set one, otherwise set it by youself.
For example, Kubuntu:
(Kubuntu is a
linux desktop operating system written in c++ qt (kubuntu.org))
.
$ su # apt install ./mysql-XXX-YYY.deb # It will guide you to set something on installing process. # apt update # apt install mysql-server # systemctl status mysql # Check if mysql is started
Explore data with mysql command and sql commands.
$ mysql -u root -p mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ mysql> use mysql Database changed mysql> show tables; mysql> select * from db; mysql> select * from user;
Create database
mysql> create database cpp_info; mysql> use cpp_info Database changed
Create table
mysql> create table cpp_info -> ( -> id int unsigned key auto_increment, -> name varchar(160) -> );
Insert info
mysql> insert into cpp_info(name) values -> ('cpp'), -> ('c++'), -> ('boost');
Query
mysql> select * from cpp_info; +----+-------+ | id | name | +----+-------+ | 1 | cpp | | 2 | c++ | | 3 | boost | +----+-------+
Connect mysql with c++ boost boost::mysql.
(boost::mysql is c++ mysql
client library. (boost::mysql
home)) .
Install c++ boost libraries first.
Connect, query mysql with c++ boost::mysql.
c++ code, save as cpp-mysql.cpp
:
#include <boost/mysql.hpp> #include <iostream> namespace mysql = boost::mysql; namespace asio = boost::asio; using std::string_literals::operator""s; int main(int argc, char * argv[]) try { if (argc != 4) throw std::runtime_error{""s + argv[0] + " <username> <password> <mysql host>"}; auto ctx = asio::io_context{}; auto ssl_ctx = asio::ssl::context{asio::ssl::context::tls_client}; auto conn = mysql::tcp_ssl_connection{ctx.get_executor(), ssl_ctx}; auto resolver = asio::ip::tcp::resolver{ctx.get_executor()}; auto ep = resolver.resolve(argv[3], mysql::default_port_string); auto params = mysql::handshake_params( argv[1], // username argv[2], // password "cpp_info" // database ); conn.connect(*ep.begin(), params); mysql::results results; conn.execute("select * from cpp_info", results); std::cout << results.rows()[0][1] << std::endl; // cpp for (const auto & row: results.rows()) { std::cout << "=>\t"; for (const auto & column: row) { std::cout << column << "\t"; } std::cout << std::endl; } conn.close(); } catch (const std::exception & e) { std::cerr << "[c++ exception], ERROR: " << e.what() << std::endl; return 1; }
Compile it:
$ g++ cpp-mysql.cpp -std=c++23 -lssl -lcrypto -lboost_charconv -o cpp-mysql
Run program:
$ ./cpp-mysql [c++ exception], ERROR: ./cpp-mysql <username> <password> <mysql host> $ ./cpp-mysql root your-mysql-password localhost cpp => 1 cpp => 2 c++ => 3 boost
c++ std::exception:
std::cout.write(err.data(), err.size());
std::cout << std::endl;
caught:
================================================== # The c++ programming language. # # # # Home: cppfx.xyz # # Join c++ Discord: yZcauUAUyC # # Deck # ==================================================