Wednesday, December 9, 2020

Restore MySql Database From Just frm and idb File

Hello,

Few days back, I encountered a situation where we needed to restore MySql Database but we only had frm and idb file. Didn't have .sql file or log file from MySql server which is very much needed when you want to restore. So here in this blog I am going to explain how you can still restore MySql Database from only frm and idb file.

First you will need a tool called dbsake which you can install with following command.

curl -s http://get.dbsake.net > dbsake

Once installed, run following command to check if it's installed properly or not. 

chmod u+x dbsake


./dbsake --version


If it shows version and all, it's installed. Now first by using this tool we will get schema of table from frm file. Run following command. 


./dbsake frmdump /PATH_TO_FRM/file.frm


It will give you create table schema like this.


CREATE TABLE `table_name` (

  `column1` int(11) NOT NULL AUTO_INCREMENT,

  `column2` varchar(255) DEFAULT NULL,

  `column3` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`column1`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;


Copy it and run it in your sql editor. If you are migrating it from MySql 5 to 6 you may want to add ROW_FORMAT


CREATE TABLE `table_name` (

  `column1` int(11) NOT NULL AUTO_INCREMENT,

  `column2` varchar(255) DEFAULT NULL,

  `column3` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`column1`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=compact;


Now it will create table. It's time to load data into it. 


First we will delete the table space which is idb file created. Run following command.


ALTER TABLE table_name DISCARD TABLESPACE


Now copy the old idb file and replace the existing one in your MySql server.


Now attach table space again.


ALTER TABLE table_name IMPORT TABLESPACE


That's it and now if you browse the data in table, you can see all your old rows. 

Tuesday, September 29, 2020

MySql Export / Import Large Database

Hello,

In this blog I am going to explain a trick which I recently used for Export / Import large database. 

Let me first explain the issue I faced. The database was over 250 MB. I was using MySql WorkBench to export in single self contained file. But it was breaking in between as some tables were really huge. 

So here is what I did. First in MySql WorkBench, in stead of exporting it to self contained single file, export it to Dump project folder. Here it will create sql file for each table in the folder. So in my case it created approximately 180 sql files. 

Now open terminal and go to that folder. First step here is we will concat all those files and make a single file.

Run the following command in terminal from folder where you have all the sql files.


cat *.sql  > .single_file.sql


Above command will concat all the files into one file and create that file in same folder. Now we will use this file to import into our database.


/Applications/MAMP/Library/bin/mysql -u DB_USER -p DB < .single_file.sql


That's it. With this trick you can Import / Export large database. Please note that, for this you should have terminal access to your MySql server.

Monday, September 7, 2020

Connect to Web Socket From Your Android App using OkHttp

Hello,

In this blog I am going to explain how you can connect with Web Socket from your Android App using OkHttp. This will be useful when you want to send some data real time from your Android app to Web app. 

First add following line in your build.gradle file dependencies. 

implementation 'com.squareup.okhttp3:okhttp:3.11.0'

Now let the gradle build and sync. Next we will make a class which extends WebSocketListener from OkHttp3 and a new class and name it MyWebSocketListener.java and add following code to it. 

import okhttp3.Response;

import okhttp3.WebSocket;

import okhttp3.WebSocketListener;

import okio.ByteString;

public final class MyWebSocketListener extends WebSocketListener {

    private static final int NORMAL_CLOSURE_STATUS = 5000;

    public MyWebSocketListener() {  

    }

    @Override

    public void onOpen(WebSocket webSocket, Response response) {

        webSocket.close(NORMAL_CLOSURE_STATUS, "Exit");

    }

    @Override

    public void onMessage(WebSocket webSocket, String text) {

        //String Meesage received

    }

    @Override

    public void onMessage(WebSocket webSocket, ByteString bytes) {

        //Meesage received in form of binary data

    }

    @Override

    public void onClosing(WebSocket webSocket, int code, String reason) {

        webSocket.close(NORMAL_CLOSURE_STATUS, null);

        //Closing socket

    }

    @Override

    public void onFailure(WebSocket webSocket, Throwable t, Response response) {

        t.printStackTrace();

    }

}

Now we have listener created. Let's create client and attach it to Web Socket and Listener.

val listener = MyWebSocketListener()

val webSocket =   OkHttpClient.Builder().build().newWebSocket(request, listener)


That's it, now you can use this instance to send or receive message.