C ++ under Linux operating MySQL

Then inserting about C ++ operations for MySQL.

Front installed MySQL database on Red Hat, then I need to use C ++ on Linux for MySQL read and write operations.

Let's simulate a relatively real production environment.

I have two Linux machines, installing a MySQL database, an application server as I use the Application. Or traditional, my database server might use HP minicomputer, IBM AIX. FIG overall results were as follows:

So I prepared two Linux VM, one of which is used to install MySQL front of RedHat, the other one is a new installation of RH.

I need to connect and manipulate MySQL database in this new VM. We first need to ensure that I am able to telnet MySQL service port number on the machine.

Because I need to use the library lib provided by MySQL, so I will be in MySQL server header files and libraries are copied lib. I put this directory / daniel / mysql

[daniel@daniel2 mysql]$ pwd
/daniel/mysql
[daniel@daniel2 mysql]$ ll
total 8
drwxrwxr-x. 3 daniel daniel 4096 Dec 29 17:18 include
drwxrwxr-x. 7 daniel daniel 4096 Dec 29 15:26 lib
[daniel@daniel2 mysql]$ 

I need to connect to a database on another machine, I can not tell people the root password, but created a database, then create a user, the user has permission to DBA similar in the corresponding database, you can change the implementation by all excision investigation, and can create a table, modify table structure.

delimiter $$

CREATE DATABASE `CRM` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */$$

Create users, assign permissions

create user 'user1'@'%' identified by '123456';

GRANT ALL ON CRM.* TO 'CRM'@'%';

Create tables and stored procedures. (For simplicity I only give table structure and stored procedures in MySQL Workbench screenshots)

 

Next, I need to use MySQL API functions provided by the operation.

Specific code in the code can be seen here https://github.com/danielhu1983/Tools/tree/master/MySQLSample

1. MySQL connection class

Mysql_init call connection initialization

Mysql_real_connect call database connection

Finally, close the connection using mysql_close

2. MySQL DBHelper class

Init provide an interface, call the init method of MySQL connection, connect method

Exec provides interface to execute SQL commands to call mysql_real_query

3. main function

MySQL DBHelper call the init function and exec functions.

In the Makefile,

I need to find the header files need to specify the path alone again.

MYSQL_ROOT = /daniel/mysql

HEADER_PATH=-I$(MYSQL_ROOT)/include

This is only used to compile the final link of the time also need to specify the file so used. At this time I from MySQL Server machine execute the following command to install, and get the output result

[daniel@daniel ~]$ mysql_config --libs
-L/mysql/mysqlroot/lib -Wl,-R,/mysql/mysqlroot/lib -lmysqlclient -lpthread -lm -lrt -lssl -lcrypto -ldl
[daniel@daniel ~]$ 

The upper part highlighted in red, on the machine I did not install MySQL to be changed to the corresponding directory, such as my Makefile file is written so

LIBS=-L$(MYSQL_ROOT)/lib -Wl,-R,$(MYSQL_ROOT)/lib -lmysqlclient -lpthread -lm -lrt -lssl -lcrypto -ldl

Then execute make command in the machine, the resulting executable file is MySQL.1.0.0.1

Before using, I need to configure the environment variables in my environment, open .bash_profile file in the home directory

export MYSQL_HOME=/daniel/mysql

export LD_LIBRARY_PATH=$MYSQL_HOME/lib:$LD_LIBRARY_PATH

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH

Here I've specified LD_LIBRARY_PATH path, so that the program can find the MySQL database from the corresponding lib / daniel / mysql / lib directory

SQL code I use is to call a stored procedure, the final result might look like this

[daniel@daniel2 MySQLSample]$ ./MySQL.1.0.0.1 
1 Daniel 
2 Daniel 

Complete call it a day.

Guess you like

Origin www.cnblogs.com/danielhu/p/12125672.html