I learned how to use C ++ to access MySql in Linux over the past two days. I encountered a bunch of problems and recorded them.
1. Install MYSQL:
The company's computer is 64-bit and 64-bit RHEL4 is installed. The following three packages are installed:
MySQL-client-5.1.49-1.glibc23.x86_64.rpm
MySQL-devel-5.1.49-1.glibc23.x86_64.rpm
MySQL-server-5.1.49-1.glibc23.x86_64.rpm
When you start the MySQL service, the following error occurs: Starting mysql. Manager of PID-file without updating.
There are many solutions for this error on the Internet. I changed the configuration in the/etc/SELinux/config file to: SELinux = disabled,
Then restart the system. The MySQL service can be started.
2. Install eclipse
Download file online: eclipse-cpp-helios-linux-gtk.tar.gz
Because this version of eclipse requires JDK 5 or above,
So I downloaded the file: jdk-6u21-linux-i586-rpm.bin
After JDK is installed, it is located at/usr/Java/jdk1.6.0 _ 21
After the installation is complete, reconfigure the/etc/profile file and add the following three lines at the end:
Export java_home =/usr/Java/jdk1.6.0 _ 21
Export classpath = $ java_home/JRE/lib/RT. Jar
Export Path = $ path: $ java_home/bin
Then run the source/etc/profile command to refresh the configuration.
Since RHEL4 already has jdk1.4, You need to modify the link and execute the following command:
Rm/usr/bin Java
Ln-S/usr/Java/jdk1.6.0 _ 21/bin/Java/usr/bin/Java
Then execute the command: Java-version
We can see that the JDK version is already 1.6.
Then execute eclipseProgramYou can write C Programs.
3. C program Compilation
This step takes the most time.
An error always occurs during compilation:/usr/bin/ld: cannot find-lmysqlclient
Many people are asking this question online. The compilation was successful using the following command:
Gcc-O test. C-lmysqlclient-lm-I/usr/include/msqyl-L/usr/lib64/MySQL
I have always put the-lmysqlclient-LM parameters at the end, and the result always fails. Later I changed it to the front and compiled it.
4. Install MySQL on your computer
Because the home computer is 32-bit, so the installation is 32 for RHEL4, If you download the following three files:
MySQL-devel-5.1.49-1.glibc23.i386.rpm
MySQL-client-5.1.49-1.glibc23.i386.rpm
MySQL-server-5.1.49-1.glibc23.i386.rpm
During installation, because the system already has a lower version of MySQL-client package, uninstall it first.
The RPM parameter-ev -- nodeps -- allmatches is used. The last parameter is to delete all matching packages.
Due to the previous test, there are two identical packages in the system, which can only be deleted through this parameter.
After the installation is complete, the MySQL service cannot be started. I searched for solutions on the Internet, including modifying the/usr/SELinux/config file;
Modify/etc/My. CNF; Delete log index files, etc., are not successful, and finally uninstall the MySQL-server-5.1.49-1.glibc23.i386,
Download and install: MySQL-server-community-5.1.49-1.rhel4.i386.rpm, MySQL service can finally start.
Finally, I don't know why.
5. compile the project in eclipse
Because the program contains # include <mysql. h>
Therefore, you need to add the mysql. h path to eclipse.
Project-> properties-> C/C ++ build-> Settings-> gcc C Complier-> des-> include paths
Add two paths:/usr/lib/MySQL;/usr/include/MySQL
For 64-bit MYSQL:/usr/lib64/MySQL;/usr/include/MySQL
To enable the eclipse tool to correctly implement the compilation instructions:
Gcc-O test. C-lmysqlclient-lm-I/usr/include/msqyl-L/usr/lib64/MySQL
You also need to add two parameters for-lmysqlclient-LM
Project-> properties-> C/C ++ build-> Settings-> gcc C linker-> Libraries
Add two parameters mysqlclient and m to libraries (l ).
Here we can see the role of the GCC l parameter. M contains mathematical methods.
Add/usr/lib/MySQL to libraryies search path (l)
Go to the address to find the libmysqlclient. A file.
Finally, you can access the msyql database.
Run the MySQL command:
Grant all privileges on *. * To 'usr' @ '% 'identified by 'mypassword'
Log on to Linux MySQL on another machine to test database operations.
Next, I will learn about the threads, sockets, and WebService in Linux, but I still don't know what problems I will encounter.