Rabu, 17 Juni 2009

Restore MySQL database, stored procedure missing ?

Backing up and Restoring mysql database could be done through several ways. You can backup and restore by exporting SQL script using MySQL command line or copying MySQL data directory from the original server to the destination server.

Backup and Restore using MySQL command line

You can backup mysql database using mysqldump command line. Using this method, you can backup and restore all information on the database, including the triggers and the stored procedures that you have on the database.

This command line has many parameters, but there are only some of them that we are interested. For example to backup database ialfdb, you use the command:

mysqldump ialfdb –u root –p --add-drop-table > ialfdb.sql

The above command instruct the system to export SQL statements from the database ialfdb, connecting using username root (-u root), with password that will be asked later (-p), add drop table statement for every table that will be recreated again (—add-drop-table), save the resulting SQL statement to a file named ialfdb.sql in the current directory ( > ialfdb.sql ).

If the system was failed to executed mysqldump program then try to run it using the full path (typing the program’s exact location), for example: c:\xampp\mysql\bin\mysqldump or c:\program files\mysql\bin\mysqldump.

You can specify the destination directory for SQL file for the output by typing the file with it’s full path, for example c:\data\ialfdb.sql.

When the command is executed, it will ask you for a password, that is the password for user specified by –u parameter (root).

Enter password:

Just type the user’s password. Depending to the size of the database, it will take a while to process your request. After the process is done, you will be back to the command prompt.

Below is a screen capture of the command line session. I used it on Windows operating system, but there will be no big different on other operating system.

image

 

To restore the database, you should use the command line mysql. Again we are only interested in a few of all the parameter that the program has. To restore our backup file above (ialfdb.sql), use the command line:

mysql ialfdb –u root –p < ialfdb.sql

The command line above tell the system to restore the SQL scripts on the file ialfdb.sql ( < ialfdb.sql ), using user root to connect to the database ( –u root ), with password that will be asked later.

If the system was failed to executed mysql program then try to run it using the full path (typing the program’s exact location), for example: c:\xampp\mysql\bin\mysql or c:\program files\mysql\bin\mysql.

You can specify the source directory of the SQL script file by typing the file with it’s full path, for example c:\data\ialfdb.sql.

When the command is executed, it will ask you for a password, that is the password for user specified by –u parameter (root).

Enter password:

Just type the user’s password. Depending to the size of the database, it will take a while to process your request. After the process is done, you will be back to the command prompt.

Below is a screen capture of the command line session. I used it on Windows operating system, but there will be no big different on other operating system.

image

Backup and Restore by Copying the Data Directory

You could also backup and restore MySQL database by copying the data directory from a server to other server. By default, MySQL data directory resides on the data directory below the server installation path, for example c:\program files\mysql\data or c:\xampp\mysql\data. There will be directories for your database, in our case is the ialfdb directory.

Simply copy the data directory from the source server to the destination server using Windows Explorer.

Location of database directory of MySQL database

The problem is when your database contains stored procedures and functions. You cannot simply the database directory, but you need also to copy the proc tables under the mysql directory. The files for that tables are: proc.myi, proc.myd, and proc.frm.

The proc table files

Of course, other stored procedure and function which are belong to (created at) other database will be copied also in the new database.

That’s all, hope this article is useful for you. Please leave me a comment if you have something to discuss.

Akhmad Daniel Sembiring
vITraining.com
Ligarwangi.com - toserba online

Tidak ada komentar: