A mysql database table, a sudden power failure, restart the system failed to start the database, data files into the database directory found intact, re-initialize the database, recover data from a data file
First, copy the directory out /home/mysql-5.7.26/data/
[root@orderer home]# cd mysql-5.7.26/ [root@orderer mysql-5.7.26]# cp -R data/ ../data_bak
Second, delete /home/mysql-5.7.26/data/ directory because initialization of the database, data directory must be empty
[root@orderer mysql-5.7.26]# rm -rf data/ [root@orderer mysql-5.7.26]# mkdir data
Since I am root account operation, so the data directory permissions granted to the user mysql
[root@orderer mysql-5.7.26]# chown -R mysql:mysql data
Third, re-initialize the database
[root@orderer home]# mysqld_safe --defaults-file=/etc/mysql/my.cnf --initialize --basedir=/home/mysql-5.7.26/ --datadir=/home/mysql-5.7.26/data/ 2020-01-19T07:11:22.220590Z mysqld_safe Logging to '/home/mysql-5.7.26/log/mysqld.log'. 2020-01-19T07:11:22.266396Z mysqld_safe Starting mysqld daemon with databases from /home/mysql-5.7.26/data 2020-01-19T07:11:33.111853Z mysqld_safe mysqld from pid file /home/mysql-5.7.26/run/mysqld.pid ended
In this case, view the catalog data has been generated documents
[root@orderer mysql-5.7.26]# ll data ??? 110668 -rw-r----- 1 mysql mysql 56 1? 19 15:11 auto.cnf -rw------- 1 mysql mysql 1680 1? 19 15:11 ca-key.pem -rw-r--r-- 1 mysql mysql 1112 1? 19 15:11 ca.pem -rw-r--r-- 1 mysql mysql 1112 1? 19 15:11 client-cert.pem -rw------- 1 mysql mysql 1680 1? 19 15:11 client-key.pem -rw-r----- 1 mysql mysql 419 1? 19 15:11 ib_buffer_pool -rw-r----- 1 mysql mysql 12582912 1? 19 15:11 ibdata1 -rw-r----- 1 mysql mysql 50331648 1? 19 15:11 ib_logfile0 -rw-r----- 1 mysql mysql 50331648 1? 19 15:11 ib_logfile1 -rw-r----- 1 mysql mysql 177 1? 19 15:11 master-18-69.000001 -rw-r----- 1 mysql mysql 22 1? 19 15:11 master-18-69.index drwxr-x--- 2 mysql mysql 4096 1? 19 15:11 mysql drwxr-x--- 2 mysql mysql 8192 1? 19 15:11 performance_schema -rw------- 1 mysql mysql 1680 1? 19 15:11 private_key.pem -rw-r--r-- 1 mysql mysql 452 1? 19 15:11 public_key.pem -rw-r--r-- 1 mysql mysql 1112 1? 19 15:11 server-cert.pem -rw------- 1 mysql mysql 1676 1? 19 15:11 server-key.pem drwxr-x--- 2 mysql mysql 8192 1? 19 15:11 sys
Find mysql.log log filter "temporary password", to obtain an initial random password generated by the system, the first landing using
[root@orderer mysql-5.7.26]# cat /home/mysql-5.7.26/log/mysqld.log|grep 'temporary password' 2020-01-19T07:11:25.453456Z 1 [Note] A temporary password is generated for root@localhost: PcrY;58llX3<
Fourth, start mysql
[root@orderer home]# mysqld_safe --defaults-file=/etc/mysql/my.cnf 2020-01-19T07:16:34.019182Z mysqld_safe Logging to '/home/mysql-5.7.26/log/mysqld.log'. 2020-01-19T07:16:34.065328Z mysqld_safe Starting mysqld daemon with databases from /home/mysql-5.7.26/data
Connection mysql, using the initial random password
[root@orderer mysql-5.7.26]# mysql -uroot -p -S /home/mysql-5.7.26/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 5.7.26-log Copyright (c) 2000, 2019, 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>
Five, change the root password
mysql> alter user 'root'@'localhost' identified by 'xxxxxxx'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql>
Use the new password to log in again
[root@orderer mysql-5.7.26]# mysql -uroot -p -S /home/mysql-5.7.26/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 5.7.26-log Source distribution Copyright (c) 2000, 2019, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql>
At this point, only to see the mysql database, Next, we stopped the mysql service
[root@orderer mysql-5.7.26]# mysqladmin -uroot -p -S /home/mysql-5.7.26/mysql.sock shutdown Enter password: [root@orderer mysql-5.7.26]#
Sixth, the backup data files in the database directory files copied to the folder test data directory
[root @ orderer MySQL 5.7 . 26 ] # cp -r ../data_bak/test/ data /
View data directory
[root@orderer mysql-5.7.26]# ll data/ ??? 110672 -rw-r----- 1 mysql mysql 56 1? 19 15:11 auto.cnf -rw------- 1 mysql mysql 1680 1? 19 15:11 ca-key.pem -rw-r--r-- 1 mysql mysql 1112 1? 19 15:11 ca.pem -rw-r--r-- 1 mysql mysql 1112 1? 19 15:11 client-cert.pem -rw------- 1 mysql mysql 1680 1? 19 15:11 client-key.pem -rw-r----- 1 mysql mysql 356 1? 19 15:23 ib_buffer_pool -rw-r----- 1 mysql mysql 12582912 1? 19 15:23 ibdata1 -rw-r----- 1 mysql mysql 50331648 1? 19 15:23 ib_logfile0 -rw-r----- 1 mysql mysql 50331648 1? 19 15:11 ib_logfile1 -rw-r----- 1 mysql mysql 177 1? 19 15:11 master-18-69.000001 -rw-r----- 1 mysql mysql 573 1? 19 15:23 master-18-69.000002 -rw-r----- 1 mysql mysql 44 1? 19 15:16 master-18-69.index drwxr-x--- 2 mysql mysql 4096 1? 19 15:11 mysql drwxr-x--- 2 mysql mysql 8192 1? 19 15:11 performance_schema -rw------- 1 mysql mysql 1680 1? 19 15:11 private_key.pem -rw-r--r-- 1 mysql mysql 452 1? 19 15:11 public_key.pem -rw-r--r-- 1 mysql mysql 1112 1? 19 15:11 server-cert.pem -rw------- 1 mysql mysql 1676 1? 19 15:11 server-key.pem drwxr-x--- 2 mysql mysql 8192 1? 19 15:11 sys drwxr-x--- 2 root root 202 1? 19 15:24 test
test directory has been copied, because it is a copy of an account with root, so there will be changes to the file owner mysql
[root@orderer mysql-5.7.26]# chown -R mysql:mysql data/test/ [root@orderer mysql-5.7.26]# ll data ??? 110672 -rw-r----- 1 mysql mysql 56 1? 19 15:11 auto.cnf -rw------- 1 mysql mysql 1680 1? 19 15:11 ca-key.pem -rw-r--r-- 1 mysql mysql 1112 1? 19 15:11 ca.pem -rw-r--r-- 1 mysql mysql 1112 1? 19 15:11 client-cert.pem -rw------- 1 mysql mysql 1680 1? 19 15:11 client-key.pem -rw-r----- 1 mysql mysql 356 1? 19 15:23 ib_buffer_pool -rw-r----- 1 mysql mysql 12582912 1? 19 15:23 ibdata1 -rw-r----- 1 mysql mysql 50331648 1? 19 15:23 ib_logfile0 -rw-r----- 1 mysql mysql 50331648 1? 19 15:11 ib_logfile1 -rw-r----- 1 mysql mysql 177 1? 19 15:11 master-18-69.000001 -rw-r----- 1 mysql mysql 573 1? 19 15:23 master-18-69.000002 -rw-r----- 1 mysql mysql 44 1? 19 15:16 master-18-69.index drwxr-x--- 2 mysql mysql 4096 1? 19 15:11 mysql drwxr-x--- 2 mysql mysql 8192 1? 19 15:11 performance_schema -rw------- 1 mysql mysql 1680 1? 19 15:11 private_key.pem -rw-r--r-- 1 mysql mysql 452 1? 19 15:11 public_key.pem -rw-r--r-- 1 mysql mysql 1112 1? 19 15:11 server-cert.pem -rw------- 1 mysql mysql 1676 1? 19 15:11 server-key.pem drwxr-x--- 2 mysql mysql 8192 1? 19 15:11 sys drwxr-x--- 2 mysql mysql 202 1? 19 15:24 test
Start mysql service again, and connect mysql, view the database
[root@orderer mysql-5.7.26]# mysql -uroot -p -S /home/mysql-5.7.26/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.26-log Source distribution Copyright (c) 2000, 2019, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec) mysql>
Found that there have been test database, we switched to the test, and view the table
mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | t_product_item | | test | +----------------+ 2 rows in set (0.00 sec)
You can see the database tables, we query a table
mysql> select * from test; ERROR 1146 (42S02): Table 'test.test' doesn't exist mysql>
In this case, an error, the table does not exist, it is because we have no reason to copy the files in the data directory ibdata1 backup over, when the database engine using innodb, ibdata1 save the file metadata information database, which holds that each database each table ID
So, ibdata1 in our next catalog data backup is copied to the new database data directory covering
Seven, copy ibdata1 file backup data to the new database directory and data directory covering
Stopped mysql
[root@orderer mysql-5.7.26]# mysqladmin -uroot -p -S /home/mysql-5.7.26/mysql.sock shutdown Enter password: [root@orderer mysql-5.7.26]#
Copy the backup data under the directory ibdata1 file to the database data directory, prompts whether to overwrite the input y
[root@orderer mysql-5.7.26]# cp ../data_bak/ibdata1 data/ cp:????"data/ibdata1"? y [root@orderer mysql-5.7.26]#
We start mysql again and connect mysql
[root@orderer home]# mysqld_safe --defaults-file=/etc/mysql/my.cnf 2020-01-19T07:36:22.526939Z mysqld_safe Logging to '/home/mysql-5.7.26/log/mysqld.log'. 2020-01-19T07:36:22.574112Z mysqld_safe Starting mysqld daemon with databases from /home/mysql-5.7.26/data
[root@orderer mysql-5.7.26]# mysql -uroot -p -S /home/mysql-5.7.26/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.26-log Source distribution Copyright (c) 2000, 2019, 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.
Open the test database, and view the table
mysql> use test; Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | t_product_item | | test | +----------------+ 2 rows in set (0.00 sec) mysql>
Then check test table
mysql> select * from test; +------+--------+ | id | name | +------+--------+ | 1 | Aaaaa | | 2 | bbbb | | 3 | ccccc | | 4 | dddddd | | 5 | so that | | 6 | fffff | | 7 | rrrrr | | 8 | uuuuuu | | 10 | eerrrr | +------+--------+ 9 rows in set (0.00 sec) mysql>
The database can be read properly, we then insert a data
mysql> insert into test values(11,'hhhhhh'); Query OK, 1 row affected (0.03 sec)
Success, inquiry
mysql> select * from test; +------+--------+ | id | name | +------+--------+ | 1 | Aaaaa | | 2 | bbbb | | 3 | ccccc | | 4 | dddddd | | 5 | so that | | 6 | fffff | | 7 | rrrrr | | 8 | uuuuuu | | 10 | eerrrr | | 11 | hhhhhh | +------+--------+ 10 rows in set (0.00 sec) mysql>
Can restore normal reading and writing of data is completed, bringing the data files.