Mysql data recovery process after reinstalling Ubuntu system

1. Overview

Reinstalled Ubuntu 16.04-server for the server . Some of the previous data was lost. Fortunately, the hard disk was not formatted. At least there is some hope to restore Mysql data

2. Existing conditions

  • Mount the old hard disk on the computer and find the files in the database directory : The default database directory is: /var/lib/mysql The
    hard disk mount directory is: /media/user/f101a309-d55a-4a50-8e1c-d63534146b6f
$ ll /media/user/f101a309-d55a-4a50-8e1c-d63534146b6f/var/lib/mysql
-rw-r-----  1   122 vboxusers       56 9月   6  2019 auto.cnf
-rw-------  1   122 vboxusers     1680 11月 19 06:09 ca-key.pem
-rw-r--r--  1   122 vboxusers     1112 11月 19 06:09 ca.pem
-rw-r--r--  1   122 vboxusers     1112 11月 19 06:09 client-cert.pem
-rw-------  1   122 vboxusers     1676 11月 19 06:09 client-key.pem
-rw-r--r--  1   122 vboxusers        0 2月  25 09:40 debian-5.7.flag
drwxr-x---  2   122 vboxusers     4096 9月  17  2019 FactoryTest/
-rw-r-----  1   122 vboxusers      750 4月   1 09:07 ib_buffer_pool
-rw-r-----  1   122 vboxusers 79691776 4月   1 09:07 ibdata1
-rw-r-----  1   122 vboxusers 50331648 4月   1 09:07 ib_logfile0
-rw-r-----  1   122 vboxusers 50331648 9月   6  2019 ib_logfile1
drwxr-x---  2   122 vboxusers     4096 10月 22 15:27 kanboard/
drwxr-x---  2   122 vboxusers     4096 2月  25 09:41 mysql/
-rw-r--r--  1   122 vboxusers        6 2月  25 09:41 mysql_upgrade_info
drwxr-x---  2   122 vboxusers     4096 2月  25 09:40 performance_schema/
-rw-------  1   122 vboxusers     1676 11月 19 06:09 private_key.pem
-rw-r--r--  1   122 vboxusers      452 11月 19 06:09 public_key.pem
-rw-r--r--  1   122 vboxusers     1112 11月 19 06:09 server-cert.pem
-rw-------  1   122 vboxusers     1680 11月 19 06:09 server-key.pem
drwxr-x---  2   122 vboxusers    12288 11月 19 06:09 sys/
drwxr-x---  2   122 vboxusers    12288 10月 22 10:18 zentao/

The database FactoryTest that needs to be restored , so, no matter how it is compressed, save it first, and throw it on the server to try first.

3. Crude attempts

  • Stop mysql /etc/init.d/mysql stop
  • Drop the files found above directly into the mysql directory corresponding to the server, and modify the corresponding file permissions
  • Start mysql /etc/init.d/mysql start
  • Attempt to access and read data failed...it really wasn't that simple.

Stop the database and delete the folder that the previous violent attempt to copy into


4. After Google, see reference. 1

4.1 Recovery form

  • Need to use tool mysqlfrm
  • If not found, just install a sudo apt install mysql-utilities
  • The effect of command execution mysqlfrm --diagnostic FactoryTest/Wifi.frm
# WARNING: Cannot generate character set or collation names without the --server option.
# CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
# Reading .frm file for FactoryTest/Wifi.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement:

CREATE TABLE `FactoryTest`.`Wifi` (
  `id` int(11) NOT NULL AUTO_INCREMENT, 
  `ssid` varchar(32) NOT NULL, 
  `pwd` varchar(32) NOT NULL, 
PRIMARY KEY `PRIMARY` (`id`)
) ENGINE=InnoDB;

#...done.
  • The query statement of the table comes out, the next step is simple, copy and paste, after execution, the table is all there.

4.2 Restore data

  • Enter mysql and execute the following command (all tables):
ALTER TABLE example_table DISCARD TABLESPACE;
  • After execution, the .idb files in the database directory are gone
  • Perform the above brute force copy, only copy the .idb file: sudo cp -rn. /Var/lib/mysql/FactoryTest/
  • Remember to modify the file permissions: sudo chown mysql:mysql -R /var/lib/mysql/FactoryTest
  • Then enter mysql to execute statements for each table
ALTER TABLE example_table IMPORT TABLESPACE;

carry out

5. Reference

Guess you like

Origin blog.csdn.net/ansondroider/article/details/105292400