Increasing Max Number of Connections in MySQL

Thought of blogging this so I won’t waste time googling when I need it next time.

MySQL Server Version: 5.5.46
OS: U
buntu 14.04.2

To view the current maximum number of connections, you can use the following command after login to MySQL from the console.

mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 500   |
+-----------------+-------+
1 row in set (0.08 sec)

There are two ways to increase the max number of connections.

1. MySQL command line – Doesn’t require to restart MySQL server. Will affect until the next MySQL restart.

mysql> SET GLOBAL max_connections = 500;
Query OK, 0 rows affected (0.00 sec)

2. Editing my.cnf – Requires a MySQL restart for the changes to get affected.

Open my.conf file using a text editor.

$ sudo vim /etc/mysql/my.cnf

Search for max_connections entry in the file, uncomment (or add the entry if not found) it and set a desired value.

max_connections        = 500

Save the file and restart MySQL service.

$ sudo service mysql stop
$ sudo service mysql start

Reference
[1] – https://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html
[2] – http://www.electrictoolbox.com/update-max-connections-mysql/

Advertisements

Setting AD FS 3.0 as a Federated Authenticator in WSO2 Identity Server

This post contains the steps required to configure AD FS 3.0 as a federated authenticator in WSO2 Identity server using SAML.

Note: This is will be supported out-of-the-box with Identity Server 5.1.0 M3 onwards. If you are to use this with Identity Server 5.0.0 (with SP 1) you’ll need to some modifications to the source. The details can be found at [1] & [2].

Prerequisites

  • WSO2 Identity Server 5.1.0 M3 or above Identity Server Service Pack 2 (yet to be released)
  • ADFS 3.0

Adding IS as a Relying Party in ADFS

In ADFS Management UI expand Trust Relationship, right click on Relying Party Trust and select Add Relying Party Trust…

Screen Shot 1

Follow the wizard as shown below

Screen Shot 2

Screen Shot 3

Type a desired display name for the relying party and click Next.

Screen Shot 4

Select AD FS Profile and Click Next.

Screen Shot 5

We are not using an encryption certificate so click Next.

Screen Shot 6

Set the relying party SAML 2.0 SSO service url to the commonauth endpoint of IS. e.g: https://localhost:9443/commonauth

Screen Shot 7

Add the relying party trust identifier and click Next. The value you enter here should be entered in IS IdP settings as well. Setting up the IdP is explained in the next section.

Screen Shot 8

We won’t be configuring multi-factor authentication so click Next.

Screen Shot 9

Select Permit all users to access this relying party and click Next.

Screen Shot 10

Review the settings and click Next.

Screen Shot 11

Click Close to finish adding the relying party trust. Also let the wizard to open the Claim Rules dialog

Screen Shot 11

In the Edit Claim Rule dialog we will specify which claims to be sent to the relying party. In this example I’m sending the SAM-Account-Name LDAP attribute as a NameID claim.

First click Add Rule…

Screen Shot 12

Select Send LDAP Attributes as a claim and click Next.

Screen Shot 13

Set a Claim rule name and map SAM-Account-Name to E-Mail Address. Then click Finish.

Screen Shot 14

Click Add Rule… again to transform the email address claim to NameID claim. Select Transform an Incoming Claim and click Next.

Screen Shot 15

Set the Claim rule name. Select the incoming claim type as E-Mail Address and outgoing claim type and ID format as Name ID and Unspecified respectively. Then click Finish.

Screen Shot 16

Then apply and close the claim rules dialog.

Screen Shot 17

Before we wrap up things in AD FS side, there are few configuration changes needed to be done in Relying Party Trust properties. For that right click on the Relying Party Trust we just created and select Properties.

Screen Shot 17

Goto Signature tab and click Add.

Screen Shot 18

The certificate which should be added here depends on a couple of things. If the Service Provider in IS is under the super tenant domain. The public certificate of IS should be used. Else, the public certificate of the tenant domain should be selected. The public certificate of the tenant can be exported from the Key Management feature of the IS management console. In this post, the service provider is added in the super tenant domain and the default keystore has not been change. Therefore the default wso2carbon certificate is used. You can export the certificate from wso2carbon.jks which is located at /repository/resources/security/ directory.

Screen Shot 19

When importing the default certificate you will get the following dialog. Click Yes to proceed.

Screen Shot 20

Next move to Endpoint tab. Here we have to set the SAML logout endpoint. Click Add SAML…

Screen Shot 21

Select Endpoint Type as SAML Logout and the Binding as POST. Set the Trusted URL as https://<AD_FS_server>/adfs/ls  and the Response URL as the /commonauth endpoint of IS. Once it is done save the property settings of the RP.

Screen Shot 21

Next we will move on to configuring AD FS as an Identity Provider in IS. For the configurations we will have to add the Token signing certificate of AD FS. To export the token signing certificate of IS do as follows.

In AD FS management UI, click on Certificates under Service, right click on Token-signing certificate and select View Certificate.

Screen Shot 22

Goto Details tab and click Copy to File… Then follow the Certificate Export Wizard.

Screen Shot 23

Select Base-64 encoded X.509 (.cer) and click Next.

Screen Shot 23

Save the certificate to a desired location and Finish the wizard.

Screen Shot 24

Screen Shot 25

Screen Shot 26

We will move on to configuring IS next.

Adding AD FS as a Federated Authenticator in IS

Login to IS Management console and click Add under Identity Providers. Type a unique name for the IdP and add the Token-signing certificate of ADFS by clicking the Browse button.

Screen Shot 27

Next set the SAML Web SSO Configuration under Federated Authenticators.

  • Check Enable SAML2 Web SSO
  • Identity Provider Entity Id: This can be found in FederationMetadata.xml under entityID attribute. The FederationMetadata.xml can be accessed through https://<AD_FS_server>/FederationMetadata/2007-06/FederationMetadata.xml. The Entity ID is usually in the form http://<AD_FS_server>/adfs/services/trust
  • Service Provider Entity Id should be same as what’s given in AD FS RP trust identifier. eg:wso2-is
  • SSO URL should be in the form of http://<AD_FS_server>/adfs/ls
  • Check Enable Logout
  • Logout URL should be the same as SSO URL
  • Check Enable Logout Request Signing
  • Select HTTP Binding as POST

Once the details are entered click Register to save the IdP.

Screen Shot 28

[1] – https://wso2.org/jira/browse/IDENTITY-3181
[2] – https://wso2.org/jira/browse/IDENTITY-3349

MySQL Database Backup & Restore Using Java

Hello Programmers !!

It’s been a while since I’ve written a blog post. Luckily I’m in a mood to write something at the moment. Today I thought about posting something I’ve learnt recently. It’s about database backups and restores through java.

If anyone wants to backup or restore a database without going through all these troubles, you can simply use MySQL GUI Tools Bundle freely distributed by the Oracle Corporation.

Sometimes it is necessary to integrate a backup & restore system to an application we are developing. In my case it was a java application. In this post I’m expecting to give you guys a rough idea about backing up and restoring your MySQL database through a Java Application.

1. Creating A Backup

When we install MySQL open source database it provides us a client called mysqldump for backup purposes. Here we execute this mysqldump command using the java Runtime.

Here’s a sample method to create a complete database backup including add, drop MySQL statements.

public boolean backupDB(String dbName, String dbUserName, String dbPassword, String path) {

        String executeCmd = &quot;mysqldump -u &quot; + dbUserName + &quot; -p&quot; + dbPassword + &quot; --add-drop-database -B &quot; + dbName + &quot; -r &quot; + path;
        Process runtimeProcess;
        try {

            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println(&quot;Backup created successfully&quot;);
                return true;
            } else {
                System.out.println(&quot;Could not create the backup&quot;);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return false;
    }

Basically what we are doing here is similar executing the following command in the windows command line.

mysqldump -u Username -pPassword --add-drop-database -B databaseName -r backupPath

Here  -u, -p, -B, -r are options to indicate that we are inserting the database username, password, database name and location to save the backup respectively. The --add-drop-database option is to create a complete backup. It means that when we are restoring the backup file, if the database does not exist in that particular database the restoring process itself create the database automatically.

If by any chance you want to create a backup without add, drop database command. You can use the following command.

mysqldump -u Username -pPassword databaseName -r backupPath

For multiple databases use

mysqldump -u Username -pPassword --add-drop-database -B db1 db2 db3 -r backupPath

If you want to backup All the databases. You can use the command as follows.

mysqldump -u Username -pPassword --add-drop-database -A -r backupPath

Likewise you can create your backup according to the requirements. There are many other options that you can add to your command. You can visit mysqldump Docs for more options or simply type mysqldump --help on the command line to view the options.

That’s the basic idea about backing up the database. Let’s see how we can do the restoring process through java.

2. Restoring A Backup

I had hard time finding a working code for the restoring process. After making a few changes on the commands I’ve found on the internet, I’ve manage to create a workable runtime  command for the restore process.

Note: The following method is to restore a complete database backup (backup with add, drop database command)

public boolean restoreDB(String dbUserName, String dbPassword, String source) {

        String[] executeCmd = new String[]{&quot;mysql&quot;, &quot;--user=&quot; + dbUserName, &quot;--password=&quot; + dbPassword, &quot;-e&quot;, &quot;source &quot;+source};

        Process runtimeProcess;
        try {

            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println(&quot;Backup restored successfully&quot;);
                return true;
            } else {
                System.out.println(&quot;Could not restore the backup&quot;);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return false;
    }

If the the backup doesn’t have the add, drop database, you can use the following method.

public static boolean restoreDB(String dbName, String dbUserName, String dbPassword, String source) {

        String[] executeCmd = new String[]{&quot;mysql&quot;, &quot;--user=&quot; + dbUserName, &quot;--password=&quot; + dbPassword, dbName,&quot;-e&quot;, &quot;source &quot;+source};

        Process runtimeProcess;
        try {

            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println(&quot;Backup restored successfully&quot;);
                return true;
            } else {
                System.out.println(&quot;Could not restore the backup&quot;);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return false;
    }

I have tested all the commands and the methods mentioned in this post and they worked fine in Windows environment. That’s all about creating & restoring backups. Hope you guys learnt something new.
If you have any questions please post them as a comment & I’ll try my best to answer them. I’m backing up for now then. See you guys with another post. Hopefully! 🙂

Update

Make sure to add the path of  your ‘MySQL bin folder’ to the path variable in ‘Windows Environment Variables’.

References:
http://sureshk37.wordpress.com/2009/09/07/mysql-backup-and-restore-using-java/
http://forums.mysql.com

Robocode – “Build the best – destroy the rest!”

This post is for all the geeks out there who’s crazy about programming battle robots. The program is Robocode!

In simple terms Robocode is a programming game. The game is all about programming a robot and battling with other robots. You can use JAVA or .NET for programming the battle robot. The Fun fact is that you can battle your robots with other opponents real-time and on-screen.

Robocode comes with its own installer, built-in robot editor & a Java compiler The only pre-requisite a JAVA installed operating system. One can also use his/her favorite IDE for the programming. Robocode comes to you as a free and an opensource software. So why wait. Be a Robocoder, Build the best & destroy the rest!

You can download the program here.

And also check the

for detailed information.

💡 Special Thanks goes to my friend Hansa Nanayakkara for sending me the link.

Have fun battling!! 😀

The ‘I-Game’: Answer

Hey guys!
Hope you can remember my previous post ” The ‘I-Game’ “ . In this post I like to share an answer for that problem. There can be many solutions for the problem but I found this neat piece of code while surfing the internet. Anyone whose interested in other problems & answers they can visit http://www.migids.com/ .  I have tested the code for the sample input and worked fine.

Enjoj ! 🙂


import java.util.Scanner;

public class ProblemS {

    static char[][] grid = new char[10][10];

    public static void main(String[] args) {

        // "turn off lights"
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                grid[i][j] = '.';
            }
        }

        String line = "";
        Scanner console = new Scanner(System.in);

        try {
            while (console.hasNextLine()) {
                line = console.nextLine();
                if (line == null || line.isEmpty()) {
                    break;
                }
                int[] coords = new int[2];
                coords[0] = Integer.parseInt(line.split(" ")[0]);
                coords[1] = Integer.parseInt(line.split(" ")[1]);

                // invert the coords as in document
                push(coords[1], coords[0]);
            }
        } catch (Exception e) {
        }

        // output the grid
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j <= 8; j++) {
                System.out.print(grid[i][j] + " ");
            }
            System.out.println(grid[i][9]);
        }
    }
    /**
     * Pushes the screwdriver on the target coordinates
     *
     * @param x
     * @param y
     */

    private static void push(int x, int y) {

        sw(x, y);
        sw(x - 1, y - 1);
        sw(x - 1, y);
        sw(x - 1, y + 1);
        sw(x + 1, y - 1);
        sw(x + 1, y);
        sw(x + 1, y + 1);
    }
    /**
     * Switches the light
     */
    private static void sw(int x, int y) {

        // check bound
        if (x > 10 || x < 0 || y > 10 || y < 0) {
            return;
        }
        if (grid[x][y] == '.') {
            grid[x][y] = 'X';
        } else {
            grid[x][y] = '.';
        }
    }
}

The ‘I-Game’

You all know that IEEEXtreme 4.0 finished recently. I thought of publishing some of the problems given at the competition. Give it a try if you have a little time. Most of the questions are really cool. Try to develop algorithms to solve the questions. I’ll try to publish all the questions whenever possible.

Here’s The problem called ‘I-Game’ straight from Xtreme 4.0 (Hope I’m not violating any copyright laws) Have Fun Coding….

Continue reading “The ‘I-Game’”

IEEEXtreme 4.0 @ MORA

ieeextreme 4.0

IEEEXtreme 4.0 organized by the IEEE concluded successfully today (2010-08-24) morning at 0530h local time. IEEEXtreme is a global challenge in which teams of IEEE student members compete in a 24-hour time span against each other to solve a set of programming problems.

The competition started on 2010-08-24 at 0530h local time (00:00 GMT). This year 37 teams registered for the competition from University of moratuwa. The team “beta” (Members: Varuna Parinda Jayasiri, Chethiya Abesinghe, Lalintha Gayan Polpitiya) has managed to secure the 11th place of the competition this year. Also the teams “Selachimorpha” and “SharpSharks” managed to reach top 50 in the competition.

Hats off for all the teams for the great effort they have given. Coding algorithms for 24 hours straight is not an easy task as it seems. The IEEE Student branch of University of Moratuwa was there with all the participants in the university giving them an immense support providing necessary facilities. So a BIG “THANK YOU” goes to them.

It was a great opportunity for me to take part in the organizing process of an event with that magnitude. And hopefully I’ll be and Xtremer for IEEEXtreme 5.0 !!! 😀