1. Install the MySQL client
With the command:
Yum install-y mysql-server MySQL mysql-devel
This command contains the installation of the client and server
2. Visit MYSLQ
At the command line, enter:
mysql-h192.168.0.36-uroot-p123456
Appear:
Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is 45099
Server VERSION:5.5.5-10.0.12-MARIADB MariaDB Server
Copyright (c) and/or, Oracle, its affiliates. All rights reserved.
Oracle is a registered trademark of the Oracle Corporation and/or its
Affiliates. Other names trademarks of their respective
Owners
On behalf of the Mysql Client Installation succeeded!
3. code Access
#include <iostream>#include<mysql/mysql.h>#include<string>using namespacestd;intMain () {MySQL mysql; Mysql_init (&MySQL); Mysql_real_connect (&mysql,"192.168.0.36","Root","123456","UC",3306Null0); stringsql ="INSERT INTO Sysuser (Orgid,useraccount) VALUES (1, ' java1 ');"; mysql_query (&MySQL, sql.c_str ()); Mysql_close (&MySQL); return 0;}
Compile:
g++-O test main.cpp-lmysqlclient-i/usr/include/mysql/-l/usr/lib64/mysql
Note: The path to- L is below lib64
4. Query Instance Code
#include <iostream>#include<string>#include<mysql/mysql.h>using namespacestd;intMain () {MySQL mysql; Mysql_res*result =NULL; Mysql_field*field =NULL; Mysql_init (&MySQL); Mysql_real_connect (&mysql,"192.168.0.36","Root","123456","UC",3306Null0); //Mysql_real_connect (&mysql, "localhost", "root", "root", "test", 3306, NULL, 0); stringsql ="select Id,useraccount from Sysuser;"; mysql_query (&MySQL, sql.c_str ()); Result= Mysql_store_result (&MySQL); intRowCount =mysql_num_rows (Result); cout<<"ROWCOUNT:"<< RowCount <<Endl; intFieldCount =mysql_num_fields (Result); cout<<"FieldCount:"<< FieldCount <<Endl; for(inti =0; i < FieldCount; i++) {field=Mysql_fetch_field_direct (result,i); cout<<"Field Name:"<<field->name <<"\t\t"; } cout<<Endl; Mysql_row ROW=NULL; Row=mysql_fetch_row (Result); while(NULL! =row) { for(intI=0;i<fieldcount; i++) {cout<<"Row"<<i<<":"<<row[i] <<"\t\t"; } cout<<Endl; Row=mysql_fetch_row (Result); } mysql_close (&MySQL); return 0;}
C + + Access MySQL on Linux