Monday, December 27, 2010

Installing MySQL Server and MySQL++ API on Linux (Ubuntu 10.04 LTS)


This tutorial covers how we can install MySQL server and MySQL++ API on Ubuntu. The MySQL++ API will help your C++ code to communicate with and retrieve data from your database. So here we go,

To install MySQL Server

Step I:
Go to Terminal and write
$ sudo apt-get install mysql-server
During the installation it will prompt you for the password, set the password and continue (this will be the password of your server)

Step II (Optional):
To be able to connect to mysql from internet, remove the restriction on the configuration file. Open the file by typing the following command at terminal.

$ gksudo gedit /etc/mysql/my.cnf
Find the line bind-address = 127.0.0.1 and comment it out and save the file again.
Like this --> #bind_address =127.0.0.1


Step III:
Once you're done with the installation, write on terminal

$ sudo apt-get install mysql-query-browser
Step IV:
After installing MySQL Query Browser go to Applications > Programming > MySQL Query Browser

  • Now in Stored Connection type: loopback 127.0.0.1
  • Enter the password, that you must have set during the step I.
  • Click on Connect. Now your mysql server is up.
To install MySQL++

Okay, now we are done with the installation of server, now comes the installation of mysql++, the C++ API which makes your code to communicate with your Database.

Step I:
Go to Terminal and type

$ sudo apt-get update
Step II:
Now, download the required package for mysql++, type.
$ sudo apt-get install libmysqlclient15-dev

Step III:
Now move to home directory and to download .tar file, type
$ cd~


Step IV:
to Untar the file, type

$ tar xvfz mysql++-3.1.0.tar.gz

Step V:
Now configure and Install.
$ cd mysql++-3.1.0

$ ./configure --prefix=/usr

$ make

$ sudo make install
And now your done with the installation of the stuff.

C++ Code to Connect with Database
Here is the c++ code snippet that will help you to connect and communicate with your database.
Sample Code


Points to remember:
  • In the header file, you have to give the complete path, where you've saved your mysql++.h header file, normally this is the case, which has been shown in the above snapshot.
  • To compile and run this .cpp file go to terminal and type

$ g++ file_name.cpp -o result -I/usr/include/mysql -lmysqlpp
  • To run this file
$ ./result
And you're Done :)