Installing mysql in linux

To install mysql type following command in terminal

$ sudo apt-get install mysql-server

When installation is done, you may want to setup password for root

sudo mysqladmin -u root -h localhost password 'test'
sudo mysqladmin -u root -h hostname password 'test'

Replace hostname and ‘test’ with your actually hostname and desired

<< Back to Setting up j2ee developer environment …

Setting up j2ee developer environment in ubuntu/linux

A java developer machine has following most frequent tools in his machine.
1.  Sun JDK 6.0
2.  Eclispe 3.4
3.  Tomcat 6.X
4.  MySql 5
5.  Apache 2

Currently above are the popular versions, please update version if a newer version is available. Also, I have tried to list down all the necessary links and commands so that you can save time in finding these links or commands.

if you find something missing, please post a comment.

Connecting to mysql database

Below is the code for creating mysql connection in java and php.
Please make sure that mysql-connector-java-5.0.6-bin.jar is in class path.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySqlConnectionTest {
    public static void main(String[] args) {
        Connection connection = null;

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            // We want to connect to a database name 'test' in mysql.
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");

            if (!connection.isClosed()) {
                System.out.println("Successfully connected to MySQL server...");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
           } catch (SQLException e) {
       }
    }
  }
}

Below is the code in php for mysql connection.

<?php
    $hostname='localhost'; //// specify host
    $user='root'; //// database user name
    $password='root'; //// database password
    $database='test'; //// database name
    $connection = mysql_connect("$hostname" , "$user" , "$passwprd")
    or die ("Unable connect to MySQL");
    $db = mysql_select_db($database , $connection) or die ("Can't find database.");
?>

Backup of mysql database

Use the following command to get backup of complete mysql database.

shell> mysqldump -u [username] -p[password] –all-databases > [backupfile.sql]
–if you want to take backup of selective database then u need to specify database name like
shell> mysqldump -u [username] -p[password] –databases [databasename] > [backupfile.sql]

Syntax in above commands are explained below
username – User name of mysql database
password – Password of mysql database
databasename – Name of the database you want to take backup
backupfile – Name of the backup file
http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html

Changing root user password in mysql

Changing root user password in mysql

Resetting root password on window
1. Log in to the window using admin credentials
2. Check if ur mysql server is running, if so stop it. To stop mysql server go to
Start Menu -> Control Panel -> Administrative Tools -> Services
Go to MYSQL and stop it.
3. Save the following commands in a file. lets call it mysql-password-reset.txt and save this file to c drive.

UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
FLUSH PRIVILEGES;

4.Start MYSQL using mysqld-nt with file option.
The mysqld-nt.exe process is used within the mySQL Database server application to handle SQL requests and provide general access to the database.
5. C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqld-nt –init-file=C:\mysql-password-reset.txt
6. MYSQL server will start successfully.
7. Stop the server and delete the file and start the MYSQL server in normal mode.
8. To start server in normal mode go to services and start the MYSQL service.

Resetting root password on unix system
1. Stop mysql server if running.

$ /etc/init.d/mysql stop

2. Now start MYSQL using following command.

$ mysqld_safe --skip-grant-tables &

3. Now log into mysql server using

$ mysql -u root

4. You will see mysql command line. Now you can reset your new root password

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

5. Stop mysql server

$ /etc/init.d/mysql stop

6. Your new password is set. You can use new passowrd to log in to mysql.

References
http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html

java.lang.UnsupportedClassVersionError: Bad version number in .class file

java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1853)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:875)

Why is this exception coming?

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I am getting following exception while connecting to mysql. Please let me know where am I wrong? Thank in advance.

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at MySqlTest.main(MySqlTest.java:18)

Restoring mysql dump file

We have a complex database involving foreign keys and we want to restore such database from dump file. If we try to run mysql dump file directly then there might be some errors related to foreign key constrain. The cause for such errors is that a table restore is called while it has foreign key dependency to some other table which has not been restored yet. To ignore such errors, we need to put one simple sql line (SET FOREIGN_KEY_CHECKS=0;) in the beginning of sql dump file. After adding “SET FOREIGN_KEY_CHECKS=0;” remember to add the “SET FOREIGN_KEY_CHECKS=1;” at the end of the dump file.

Technology