Install connector c++ under linux (ubuntu) to connect to mysql

 

table of Contents

1 Download the connector c++ source code

2 Install dependent files

1. Compilation tool Cmake

2. Mysql Client Libary

3. boost C++ library

4. openssl library

3 Compile

1. Create build folder

2. Enter the build folder and execute the command

3. Compile

4. Put the compiled file in the correct location

4 Use

5. Matters needing attention


Since I was using c++ to write algorithms and needed to connect to the mysql database, at the beginning, I directly used the c api to connect to the database, and found it very cumbersome, so I started to use connect c++ to connect and access the database.

1 Download the connector c++ source code

It is true that you can directly download the connector c++ library and use it directly, but it is not recommended, because using the library directly may cause inexplicable errors due to system reasons. I first downloaded the ubuntu 18.04 library directly, but because my system is 19.10, However, the official library did not provide the 19.10 library, so I was forced to use the 18.04 library. At the beginning, it could be used normally, but after a few days, it suddenly became unavailable, reported a memory error, and was particularly pitted. Therefore, it is recommended to use the source code and compile it on the system. After that, use.

Official website download address

If you feel the download is slow, or want to contribute C coins to me, you can download this:

 

 

As shown in the figure, select Source Code and the corresponding system. Then download it.

2 Install dependent files

1. Compilation tool Cmake

Requires Cmake3.0 or above

2. Mysql Client Libary

Is the c api of mysql

3. boost C++ library

The boost library is only needed when you need to compile legacy JDBC. Generally, it is not needed. If needed, you need boost version 1.59.0 or higher

4. openssl library

Requires version 1.0 or higher

#安装cmake
sudo apt-get install cmake
#安装mysql,现在是2020年,默认安装mysql8.0
sudo apt-get install mysql-client mysql-server
#安装mysql的c api
sudo apt-get install libmysqlclient-dev
# 安装openssl
sudo apt-get install openssl
# 安装boost
sudo apt-get install libboost-dev

3 Compile

1. Create build folder

Create a build folder in the source folder

2. Enter the build folder and execute the command

cmake   ../    -DCMAKE_BUILD_TYPE=Release  -DWITH_JDBC=true 

3. Compile

sudo make
sudo make install

4. Put the compiled file in the correct location

Compile with the above method, and the installation location is not specified, so the compiled file will be placed in the default

/usr/local/mysql

Under path

Enter connnector-c++, there are

Copy all files (including folders) in the jdbc folder in the include folder to the /usr/local/include folder

sudo cp -r include/jdbc/* /usr/local/include/

Then copy all the libraries in the lib64 folder to the /usr/lib directory

sudo cp lib64/* /usr/lib/

At this point, the installation is complete and you can start using it.

4 Use

#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/driver.h>

using namespace sql;
using namespace std;

#define DBHOST "tcp://127.0.0.1:3306"
#define USER "root"
#define PASSWORD "123456"

int main(){

  Driver *driver;
  Connection *conn;
  driver = get_driver_instance();
  conn = driver->connect(DBHOST, USER, PASSWORD);
  cout<<"DataBase connection autocommit mode = "<<conn->getAutoCommit()<<endl;
  delete conn;
  driver = NULL;
  conn = NULL;
  return 0;
}

use

g++ mysqlconn.cpp -lmysqlcppconn

Compile

 

5. Matters needing attention

A day later, I installed on two cloud servers again, but there were many pitfalls in the middle. One is Alibaba Cloud, and the installed system is centos7. The software installed is very old, cmake is only 2.8, and you need to download the source code yourself and compile the higher version of cmake. The centos installation command is different from ubuntu, so be careful. The other is the cloud server of Tianyi Cloud, which uses ubuntu1604. During the installation of these two, I encountered a pit. After I finished compiling the connector c++, when I compiled the application program I wrote, when linking Error:

This means that there is a symbol in the libmysqlcppconn dynamic library that is undefined and needs to rely on other libraries. When I first encountered this error, I was at a loss. I did a lot of searching for this error, but I didn't find any useful information. I spent the whole morning. Finally found a way:

Add an option to the compiler g++:

g++ ... -Wl,-allow-shlib-undefined

The meaning of this option is to allow undefined symbols in the dynamic library. After adding it, mine can be used normally. As for whether it will affect the use, I haven't found it yet.

Finally, I need to say, comrades, protect your hair.

Guess you like

Origin blog.csdn.net/qq_39545674/article/details/106618299