LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I restore a MySQL database simply (https://www.linuxquestions.org/questions/linux-software-2/how-do-i-restore-a-mysql-database-simply-353731/)

dobriain 08-16-2005 03:34 AM

How do I restore a MySQL database simply
 
How do I restore a MySQL database simply

MySQL Backup Procedure

BACKUP

'mysqldump' will create a list of SQL statements which can recreate the database. It is best to redirect this output to a file.

linux:~ # mysqldump -uMyUser -pMyPass -A -q > dump_file.sql

-u Username
-p Password
-A All Databases (that the user -u has GRANT access to)
-q Quick

> Redirect console output to a file > dump_file.sql

RESTORE

What you have is a file of SQL Statements which can be restored as follows.

Start with the root user and redirect the input into the command from the file.

linux:~ # mysql -uroot -ppassword < dump_file.sql

-u Username
-p Password

< Redirect SQL file as input < RDF_Website.sql


Recreate the database user(s) with the GRANT options.

linux:~ # mysql -uroot -ppassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 170 to server version: 4.1.10a

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> grant all on MyDB01.* to 'MyUser'@'localhost' identified by 'MyPass' with grant option;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on MyDB02.* to 'MyUser'@'localhost' identified by 'MyPass' with grant option;
Query OK, 0 rows affected (0.00 sec)

Check Grants

mysql> SHOW GRANTS for 'MyUser'@'localhost';
+--------------------------------------------------------------------------------------------------------------+
| Grants for MyUser@localhost |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'MyUser'@'localhost' IDENTIFIED BY PASSWORD '*A1361938C1D8A0E651E99019408F217F32F60078'|
| GRANT ALL PRIVILEGES ON `MyDB01`.* TO 'MyUser'@'localhost' WITH GRANT OPTION |
| GRANT ALL PRIVILEGES ON `MyDB02`.* TO 'MyUser'@'localhost' WITH GRANT OPTION |
+--------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

mysql>

jtshaw 08-16-2005 08:47 AM

This looks pretty good, care to write a LinuxAnswer?

mackdav 04-19-2006 12:05 PM

I have inherited a stone-aged RedHat 8.0 system running mysql 3.23.52 which cannot be upgraded for various reasons I won't go into here. However the powers that be are finally interested in backing this system up, so I have started looking into mysqldump. In the interests of recoverability, I have created a RedHat 9 system running mysql version 3.23.54 which I am now attempting to restore my dump file to.

So. To create my dump, I am doing this:

# mysqldump --opt --all-databases --user="root" -p > all-databases.dump

On the restore system, I do this:

# mysql -u root -p < all-databases.dump

...and it ticks along for a while and finally announces:

ERROR 1062 at line 6005: Duplicate entry '1' for key 1

Looking at line 6005, it says (line numbers added by me):

6004: LOCK TABLES groups WRITE;
6005: INSERT INTO groups VALUES (0,'Administrators'),(1,'Anonymous'),(2,'File Admin'),(3,'Company');

This does not appear to be a TINYINT problem; the table does not appear to be defined with a TINYINT.

I have googled around and the lack of an answer implies I'm doing something trivially incorrect.

One answer I found elswhere suggested that I replace this line

INSERT INTO groups VALUES (0,'Administrators'),(1,'Anonymous'),(2,'File Admin'),(3,'Company');

with

INSERT INTO groups VALUES ('','Administrators'),('','Anonymous'),('','File Admin'),('','Company');

This lets me import the database; however, I still have two questions:

First, when I dump the imported database back out, all my keys are shifted; ie instead of

INSERT INTO groups VALUES (0,'Administrators'),(1,'Anonymous'),(2,'File Admin'),(3,'Company');

...it says:

INSERT INTO groups VALUES (1,'Administrators'),(2,'Anonymous'),(3,'File Admin'),(4,'Company');

Is that going to be a problem if my group keys have all changed by one? Especially since no other tables have changed? (ie this, plus the server version, is the only line that is detected as a change when I diff the original dump and the dump of the restored database).

Secondly, is there some way to make mysqldump export the file in such a way that it will import back in cleanly? It seems wrong to have to mess with the dump file just to make it import again.


All times are GMT -5. The time now is 05:58 AM.