Showing posts with label mySQL. Show all posts
Showing posts with label mySQL. Show all posts

Sunday, April 29, 2012

How to resync MySQL Replication after updated master database

After update the table structure in master database, the slave stopping replication. After update the mysqldump with -d  for the new database to slave machine, it report duplicate entry error.

You can using following commands to fix it: (source: http://forums.mysql.com/read.php?26,24736,24884#msg-24884)

mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; 
mysql> START SLAVE; 

Use the value 1 for any SQL statement that does not use AUTO_INCREMENT or LAST_INSERT_ID(), otherwise you will need to use the value 2. Statements that use AUTO_INCREMENT or LAST_INSERT_ID() take up 2 events in the binary log.

Friday, February 24, 2012

Setup connection to MySQL in VS 2010

Author: 
Source: http://arunasujith.blogspot.com/2012/02/mysql-c-connection-example-in-visual_10.html


Creating a MySQL database connection using C++ is  really easy. But for a beginner it can be really hard to find the correct procedure cause it will gave really troubles. At the first time when I'm doing it it took me quite some time to make it work. So I'm going to explain how to make a connection to the MySQL database using C++ code. We use the library called boost to make the connection. 

Step 1 (Download the boost library)
First step is to download the boost library. You can download it from here from the official download page Or you can download the one I've compressed and uploaded which is smaller in size from here.

Step 2 (Create the project using Visual Studio 2008)
First go  to File --> New --> Project and you'll get a screen like this. 

mysql c++ connection example

Select Visual C++ --> Win 32 --> Win 32 Console Application  and give a name to the project and press OK. 
Then set Application Type to Console Application and remember to tick the empty project. and click finish.

Then Right Click the project and add new Item a cpp source file called Connect.cpp.

// Standard includes
#include 
#include 
#include 
using namespace std;

// Connector Includes

#include "cppconnection/driver.h"
#include "cppconnection/exception.h"
#include "cppconnection/resultset.h"
#include "cppconnection/statement.h"


// Link to the Connector/C++ library
#pragma comment(lib, "mysqlcppconn.lib")

// Connection details
string server   = "localhost";
string username = "root";
string password = "123"; //

int main(){
    sql::Driver     *driver; // MySQL Driver Object
    sql::Connection *connection; // MySQL connection Object
    sql::Statement  *statement;   // Statement which holds SQL commands
    sql::ResultSet  *resultSet;    // ResultSet to hold the results
    //Here is the connection
    try{
        driver = get_driver_instance();
        connection = driver->connect(server, username, password);
        statement = connection->createStatement();
        statement->execute("USE performance_schema");
        resultSet = statement->executeQuery("show tables");
        while (resultSet->next()){
            // Iterating the result set
            cout << resultSet->getString(1) << endl;               
        }
    }
    catch (sql::Exception e){
        cout << "Error message: " << e.what() << endl;
        system("pause");
        exit(1);
    }

    //Clear resources
    delete res;
    delete statement;
    delete connection;

    system("pause");
    return 0;
}

Copy the boost folder into the project folder. In this example the folder is Visual Studio 2008\Projects\MySQLConnection\MySQLConnection folder.

Then download this header and dll files and extract them into the same folder Visual Studio 2008\Projects\MySQLConnection\MySQLConnection folder.

Then the final step is right cilck on the Project and go to 
Properties --> Configuration Properties --> C/C++ --> General . 
On the right hand side set the "Additional Include Directories" value to ".\" value. (with out the quotes)

That's it build your project and run the project. And one more final thing when you run the project set the  "Solution Configuration" to Release mode. In the Debug Mode I had some error which I still don't no why.

So that's it now I think you got a clear idea about creating a connection for MySQL using C++ in Visual Studio 2008. See you around with another exciting post... :)

Monday, January 16, 2012

Allow user to access mysql database from anywhere

GRANT ALL ON *.* TO USERNAME@'%' IDENTIFIED BY 'PASSWORD';

Thursday, January 5, 2012

Connect to MySQL using C++ on Ubuntu

Source:  http://blog.trilabs.co.tz/2011/09/connect-to-mysql-using-c-on-ubuntu.html


Someone asked on Facebook how to connect to a MySQL Database Using C++. So I thought it will be cool to just share it here for everyone.

Ground Work

Here is how things look like on my computer.
  • OS:  Ubuntu 11.04  ( Linux legacy 2.6.38-11-generic #48-Ubuntu SMP Fri Jul 29 19:05:14 UTC 2011 i686 i686 i386 GNU/Linux)
  • DB: MySQL 5.1 ( mysql Ver 14.14 Distrib 5.1.54, for debian-linux-gnu (i686) using readline 6.2)
  • Compiler:  GCC 4.5 (g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2)
I had to install some C++ MySQL Connector and other helpers, actually after trying the textbook way and it did not work, I did a bit of googling and asking dev here and there.
?View Code SHELL
sudo apt-get install libmysqlcppconn4 libmysqlcppconn-dev libmysqlclient-dev

Code

To illustrate this, I am going to connect to the development database of one of the project I am working on show a list of products against store they can be bought. I am assuming your database knowledge in order. I have also added as much details as I could on the code itself.
?View Code CPP
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
 
/**
 * This structure will be used to store all cessary inforamtion to connect to the database
 */
struct connection_details
{
    char *server;
    char *user;
    char *password;
    char *database;
};
 
/**
 * Sets up connection to the database, this depends on the functions from mysql.h.
 * This function will connect and return a MYSQL pointer to the new connection using the
 * structure above connection_details.
 */
MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
     // first of all create a mysql instance and initialize the variables within
    MYSQL *connection = mysql_init(NULL);
 
    // connect to the database with the details attached.
    if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
      printf("Conection error : %s\n", mysql_error(connection));
      exit(1);
    }
    return connection;
}
 
/**
 * Performs the query.
 * This function perform the SQL quert and returns a pointer of MYSQL_RES
 */
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
   // send the query to the database
   if (mysql_query(connection, sql_query))
   {
      printf("MySQL query error : %s\n", mysql_error(connection));
      exit(1);
   }
 
   return mysql_use_result(connection);
}
 
/**
 * Main function
 */
int main()
{
  MYSQL *conn;  // the connection
  MYSQL_RES *res; // the results
  MYSQL_ROW row; // the results row (line by line)
 
  struct connection_details mysqlD;
  mysqlD.server = "localhost";  // where the mysql database is
  mysqlD.user = "jikoni";  // the root user of mysql
  mysqlD.password = "ugalinakuku"; // the password of the root user in mysql
  mysqlD.database = "discounts"; // the databse to pick
 
  // connect to the mysql database
  conn = mysql_connection_setup(mysqlD);
 
  // assign the results return to the MYSQL_RES pointer
  res = mysql_perform_query(conn, "SELECT products.product,stores.name  FROM products, stores WHERE products.sto_id = stores.id");
 
  printf("\n\t Product -> Store:\n\n\n");
 
  //To traverse the results we use a while loop in C++ and use the mysql_fetch_row function from within the mysql library set
  //where a row is a type of MYSQL_ROW.
 
  while ((row = mysql_fetch_row(res)) !=NULL)
      printf("\t-> %s -> %s \n", row[0],row[1]);
 
  /* clean up the database result set */
  mysql_free_result(res);
  /* clean up the database link */
  mysql_close(conn);
 
  return 0;
}

Compiling the code

Compile this program is a bit tricky as we need to link to the mysql libraries and headers that we have  used within the program, in our case the  mysql.h.  Luckly for us we can make use of the mysql_config, you may need to install it via your package manager system if you do not have it already. I already had it installed.
mysql_config provides you with useful information for compiling your MySQL client and connecting it to MySQL.
See what information it has by simply running it.
?View Code SHELL
# mysql_config
Usage: /usr/bin/mysql_config [OPTIONS]
Options:
        --cflags         [-I/usr/include/mysql  -DBIG_JOINS=1  -fno-strict-aliasing   -DUNIV_LINUX -DUNIV_LINUX]
        --include        [-I/usr/include/mysql]
        --libs           [-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient]
        --libs_r         [-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient_r]
        --plugindir      [/usr/lib/mysql/plugin]
        --socket         [/var/run/mysqld/mysqld.sock]
        --port           [0]
        --version        [5.1.54]
        --libmysqld-libs [-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqld -ldl -lwrap -lrt]
So to compile it run
?View Code SHELL
# g++ -o cmysql $(mysql_config --cflags) cmysql.cpp $(mysql_config --libs)
cmysql.cpp: In function ‘int main()’:
cmysql.cpp:60:19: warning: deprecated conversion from string constant to ‘char*’
cmysql.cpp:61:17: warning: deprecated conversion from string constant to ‘char*’
cmysql.cpp:62:21: warning: deprecated conversion from string constant to ‘char*’
cmysql.cpp:63:21: warning: deprecated conversion from string constant to ‘char*’
cmysql.cpp:69:129: warning: deprecated conversion from string constant to ‘char*’
Ignore the warring for now,  if there errors then fix them and run the executable, cmysql, that would have been generated. I will look into the warnings in time.
?View Code SHELL
# ./cmysql 
 
  Product -> Store:
 
 -> USB Flash Disk -> Brand Computers Ltd
 -> Dell 65-Watt 3-Prong Slim AC Adapter -> Brand Computers Ltd
 -> Dell Streak 7 Android Tablet -> Brand Computers Ltd
 -> Nokia 701 -> Brand Computers Ltd
 -> Dell Inspron 1240 -> Just Computers
 -> HP Photosmart D110A -> Just Computers
 -> Web Hosting -> Tri Labs Limited
 -> Bulk SMS -> Tri Labs Limited
 -> EVDO USB Modem -> Tri Labs Limited
 -> EVDO USB Modem -> Tri Labs Limited
 -> Joomla We Template -> Tri Labs Limited
 -> Night Gear -> Viatuzi
 -> Tech Sports Bra -> Viatuzi
 -> Women's Fiona Bra -> Viatuzi
 -> VRV Color Tank -> Viatuzi
I hope this help someone out there.