19 мар. 2014 г.

Восстановление данных InnoDB Mysql.

Чем дольше в Google ищешь, тем больше результатов и мнений, но ни одного прямого решения.
Итак, как же восстановить покусанные данные.
У меня началось с этого:
/etc/init.d/mysql startRather than invoking init scripts through /etc/init.d, use the service(8)utility, e.g. service mysql startSince the script you are attempting to invoke has been converted to anUpstart job, you may also use the start(8) utility, e.g. start mysqlstart: Job failed to start

Лезу в /var/lib/mysql, проверяем есть ли там хоть что-то:
root@doki:~# ls /var/lib/mysql
ibdata1  ib_logfile0  ib_logfile1  mysql  performance_schema  test  tv

Как видно, данные есть. В директории tv лежат, так называемые "фреймы", а в ibdata1 лежат данные БД.

Т.к. у нас не запускается сервер, то нам его надо запустить в принудительном режиме:
root@doki:/var/lib/mysql# mysqld --console --innodb_force_recovery=6

После этого он у меня ругается строкой:
140319  9:42:33 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.

А теперь то, что не сказано ни в одном мануале - открываем новый терминал и подключаемся к БД:
root@doki:~# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql>

Подключились.
Теперь делаем бекап нашей СУБД:


root@doki:~# mysqldump tv -u root -p > /home/tv.sql

Убиваем службу командой: killall mysqld

Далее переименуем /var/lib/mysql:
root@doki:~# mv /var/lib/mysql /var/lib/mysql_

Создадим новую директорию mysql и дадим ей права для службы mysql:
root@doki:~# mkdir /var/lib/mysql
root@doki:~# chmown -R mysql:mysql /var/lib/mysql
root@doki:~# chmod -R 700 /var/lib/mysql

Создадим новую базу mysql:
root@doki:~# mysql_install_db
Installing MySQL system tables...
140319 10:59:17 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
OK
Filling help tables...
140319 10:59:18 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h doki password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/scripts/mysqlbug script!

База создалась, добавим пользователя root:
root@doki:~# /usr/bin/mysqladmin -u root password 'new-password'

Добавился пользователь root с паролем new-password
Теперь логинимся в mysql под новым паролем и создаем базу данных tv:

root@doki:~# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database tv;
Query OK, 1 row affected (0.00 sec)

mysql> exit
Bye

Внесем данные  базу:
root@doki:~# mysql tv < /home/tv.sql

Все готово.