Skip to main content

Backup and Restore MySQL Database Using mysqldump

mysqldump is an effective tool to backup MySQL database. It creates a *.sql file with DROP table, CREATE table and INSERT into sql-statements of the source database. To restore the database,  execute the *.sql file on destination database.

1. Backup a single database

# mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
e.g. mysqldump -u root -prium mydatabase > mydumpfile.sql

The sugarcrm.sql will contain drop table, create table and insert command for all the tables in the sugarcrm database. Following is a partial output of sugarcrm.sql, showing the dump information of accounts_contacts table:
--
-- Table structure for table `accounts_contacts`
--

DROP TABLE IF EXISTS `accounts_contacts`;
SET @saved_cs_client     = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `accounts_contacts` (
`id` varchar(36) NOT NULL,
`contact_id` varchar(36) default NULL,
`account_id` varchar(36) default NULL,
`date_modified` datetime default NULL,
`deleted` tinyint(1) NOT NULL default '0',
PRIMARY KEY  (`id`),
KEY `idx_account_contact` (`account_id`,`contact_id`),
KEY `idx_contid_del_accid` (`contact_id`,`deleted`,`account_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;

--
-- Dumping data for table `accounts_contacts`
--

LOCK TABLES `accounts_contacts` WRITE;
/*!40000 ALTER TABLE `accounts_contacts` DISABLE KEYS */;
INSERT INTO `accounts_contacts` VALUES ('6ff90374-26d1-5fd8-b844-4873b2e42091',
'11ba0239-c7cf-e87e-e266-4873b218a3f9','503a06a8-0650-6fdd-22ae-4873b245ae53',
'2008-07-23 05:24:30',1),
('83126e77-eeda-f335-dc1b-4873bc805541','7c525b1c-8a11-d803-94a5-4873bc4ff7d2',
'80a6add6-81ed-0266-6db5-4873bc54bfb5','2008-07-23 05:24:30',1),
('4e800b97-c09f-7896-d3d7-48751d81d5ee','f241c222-b91a-d7a9-f355-48751d6bc0f9',
'27060688-1f44-9f10-bdc4-48751db40009','2008-07-23 05:24:30',1),
('c94917ea-3664-8430-e003-487be0817f41','c564b7f3-2923-30b5-4861-487be0f70cb3',
'c71eff65-b76b-cbb0-d31a-487be06e4e0b','2008-07-23 05:24:30',1),
('7dab11e1-64d3-ea6a-c62c-487ce17e4e41','79d6f6e5-50e5-9b2b-034b-487ce1dae5af',
'7b886f23-571b-595b-19dd-487ce1eee867','2008-07-23 05:24:30',1);
/*!40000 ALTER TABLE `accounts_contacts` ENABLE KEYS */;
UNLOCK TABLES;

2. Restore a database

# mysql -f -u root -p[root_password] [database_name] < dumpfilename.sql

# mysql -f -u root -ptmppassword mydatabase < mydumpfile.sql
When you use mysqldump to backup a table you might encounter the error below:

mysqldump: Got error: 1016: Can't open file: './rantandraveTest/project_224_terms.frm' (errno: 24) when using LOCK TABLES

There are two solutions to avoid this error
1) Set the following value to some higher number in your mysql database
  set open-files-limit=20000
2) or, while taking the mysql dump, use --lock-table=FALSE option
     mysqldump --lock-table=FALSE -uroot -prium -f mydatabase > mydumpfile.sql



Comments

Popular posts from this blog

How to install JSVC on Linux WidenHome Log | WidenHome Log

How to install JSVC on Linux WidenHome Log | WidenHome Log In our team, we have a lot of Java standalone applications which should be run as daemon on Unix/Linux system, and we found JSVC is the best choice for us to wrap Java programs to daemons. This article records the steps on how to install JSVC executable on Linux, which is our stage/prod environment. Download JSVC source package First of all, we need to download JSVC source package from this URL: http://commons.apache.org/daemon/download_daemon.cgi , for example, I downloaded commons-daemon-1.0.5-src.tar.gz file. Or, download it via wget: wget -c http://apache.etoak.com/commons/daemon/source/commons-daemon-1.0.5-src.tar.gz Build JSVC executable Unzip the source package and build JSVC executable. chmod 755 commons-daemon-1.0.5-src.tar.gz tar zxvf commons-daemon-1.0.5-src.tar.gz cd commons-daemon-1.0.5-src/src/native/unix Before building the JSVC executable, please make sure you have set JAVA_HOME variable correctly. And make sur...

Java中的Serializable浅谈

from  http://www.cnblogs.com/vicenteforever/articles/1471775.html 对象的串行化(Serialization) 一、串行化的概念和目的 1.什么是串行化             对象的寿命通常随着生成该对象的程序的终止而终止。有时候,可能需要将对象的状态保存下来,在需要时再将对象恢复。我们把对象的这种能记录自己的状态以便将来再生的能力。叫作对象的持续性(persistence)。对象通过写出描述自己状态的数值来记录自己 ,这个过程叫对象的串行化(Serialization) 。串行化的主要任务是写出对象实例变量的数值。如果交量是另一对象的引用,则引用的对象也要串行化。这个过程是递归的,串行化可能要涉及一个复杂树结构的单行化,包括原有对象、对象的对象、对象的对象的对象等等。对象所有权的层次结构称为图表(graph)。 2.串行化的目的             Java对象的单行化的目标是为Java的运行环境提供一组特性,如下所示: 1)       尽量保持对象串行化的简单扼要 ,但要提供一种途径使其可根据开发者的要求进行扩展或定制。 2)       串行化机制应严格遵守Java的对象模型 。对象的串行化状态中应该存有所有的关于种类的安全特性的信息。 3)       对象的串行化机制应支持Java的对象持续性。 4)       对象的串行化机制应有足够的 可扩展能力以支持对象的远程方法调用(RMI)。 5)       对象串行化应允许对象定义自身 的格式即其自身的数据流表示形式,可外部化接口来完成这项功能。 什么情况下需要序列化 a)当...

Log4j Configuration

First, include  Log4j   jar file in your project (e.g. log4j-1.2.8.jar) From  http://www.javabeat.net/tips/82-baisc-steps-to-configure-log4j-using-xml-and.html Configure Log4j This example demonstrated how to configure  Log4j  setup using the Proerties file and  XML file . These are the two most widely used techniques for configuring the  Log4j  for your application. But, in the recent days configuring with  properties files  are considered to be old technique and recommended using  XML . This example program uses simple satndalone java program for running the example. But, in most of the  project  scenarios it will be used in the web application. However the configuration file will be the same. log4j.properties # Set root logger level to DEBUG and its only appender to Appender1. log4j.rootLogger=INFO, Appender1,Appender2 # Appender1 is set to be a ConsoleAppender. log4j.appender.Appender1=org.apache...