如何在删除ibdata1和ib_logfile的情况下恢复MySQL数据库

论坛 期权论坛     
选择匿名的用户   2021-5-22 22:23   53   0
<div class="blogpost-body" id="cnblogs_post_body">
<p>昨天,有个朋友对公司内部使用的一个MySQL实例开启binlog,但是在启动的过程中失败了(他也没提,为何会失败),在启动失败后,他删除了ibdata1和ib_logfile,后来,能正常启动了,但所有的表通过show tables能看到,但是select的过程中却报“Table doesn&#39;t exist”。</p>
<p>于是,建议他试试可传输表空间。</p>
<p>同时,自己也测试了下,确实可行。</p>
<p>测试版本 MySQL 5.6.32 社区版</p>
<p> </p>
<p><span style="font-size:16px;"><strong><span style="color:#ff0000;">恢复的基本步骤</span></strong></span></p>
<p>1. 将原来的数据文件COPY到其它目录下。</p>
<p>2. 创建同名表,表结构必须保持一致。</p>
<p>3. 导出表空间</p>
<div class="cnblogs_code">
  <pre class="blockcode">mysql<span style="color:#808080;">&gt;</span> <span style="color:#0000ff;">ALTER</span> <span style="color:#0000ff;">TABLE</span> t DISCARD TABLESPACE;</pre>
</div>
<p>4. 将原来的数据文件COPY回来</p>
<p>5. 导入表空间</p>
<div class="cnblogs_code">
  <pre class="blockcode">mysql<span style="color:#808080;">&gt;</span> <span style="color:#0000ff;">ALTER</span> <span style="color:#0000ff;">TABLE</span> t IMPORT TABLESPACE</pre>
</div>
<p> </p>
<p>下面的演示会略为复杂,主要是还原整个场景,并针对上述步骤中的2,4做了一个测试。</p>
<p> </p>
<p><span style="font-size:16px;"><strong><span style="color:#ff0000;">首先,创建测试数据</span></strong></span></p>
<p>在这里创建两张表。之所以创建两张相同的表是为了方便后续的测试。</p>
<div class="cnblogs_code">
  <pre class="blockcode">mysql<span style="color:#808080;">&gt;</span> <span style="color:#0000ff;">create</span> <span style="color:#0000ff;">table</span> t1(id <span style="color:#0000ff;">int</span>,hiredate <span style="color:#0000ff;">datetime</span><span style="color:#000000;">);
Query OK, </span><span style="color:#800000;font-weight:bold;">0</span> rows affected (<span style="color:#800000;font-weight:bold;">0.14</span><span style="color:#000000;"> sec)

mysql</span><span style="color:#808080;">&gt;</span> <span style="color:#0000ff;">create</span> <span style="color:#0000ff;">table</span> t2(id <span style="color:#0000ff;">int</span>,hiredate <span style="color:#0000ff;">datetime</span><span style="color:#000000;">);
Query OK, </span><span style="color:#800000;font-weight:bold;">0</span> rows affected (<span style="color:#800000;font-weight:bold;">0.01</span><span style="color:#000000;"> sec)

mysql</span><span style="color:#808080;">&gt;</span> <span style="color:#0000ff;">insert</span> <span style="color:#0000ff;">into</span> t1 <span style="color:#0000ff;">values</span>(<span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">,now());
Query OK, </span><span style="color:#800000;font-weight:bold;">1</span> row affected (<span style="color:#800000;font-weight:bold;">0.06</span><span style="color:#000000;"> sec)

mysql</span><span style="color:#808080;">&gt;</span> <span style="color:#0000ff;">insert</span> <span style="color:#0000ff;">into</span> t1 <span style="color:#0000ff;">values</span>(<span style="color:#800000;font-weight:bold;">2</span><span style="color:#000000;">,now());
Query OK, </span><span style="color:#800000;font-weight:bold;">1</span> row affected (<span style="color:#800000;font-weight:bold;">0.00</span><span style="color:#000000;"> sec)

mysql</span><span style="color:#808080;">&gt;</span> <span style="color:#0000ff;">insert</span> <span style="color:#0000ff;">into</span> t2 <span style="color:#0000ff;">values</span>(<span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">,now());
Query OK, </span><span style="color:#800000;font-weight:bold;">1</span> row affected (<span style="color:#800000;font-weight:bold;">0.00</span><span style="color:#000000;"> sec)

mysql</span><span style="color:#808080;">&gt;</span> <span style="color:#0000ff;">insert</span> <span style="color:#0000ff;">into</span> t2 <span style="color:#0000ff;">values</span>(<span style="color:#800000;font-weight:bold;">2</span><span style="color:#000000;">,now());
Query OK, </span><span style="color:#800000;font-weight:bold;">1</span> row affected (<span style="color:#800000;font-weight:bold;">0.00</span> sec)</pre>
</div>
<p> </p>
<p><span style="font-size:16px;"><strong><span style="color:#ff0000;">关闭数据库</span></strong></span></p>
<p># /usr/test/mysql-5.6.32-linux-glibc2.5-x86_64/bin/mysqladmin shutdown -uroot -p123456 -h127.0.0.1 -P3310</p>
<p> </p>
<p><span style="font-size:16px;"><strong><span style="color:#ff0000;">删除ibdata1,ib_logfile0和ib_logfile1</span></strong></span></p>
<div class="cnblogs_code">
  <pre class="blockcode">[root&#64;localhost data]# cd /data/<span style="color:#000000;">
[root&#64;localhost data]# </span><span style="color:#0000ff;">ls</span><span style="color:#000000;">
auto.cnf  ib_logfile0  localhost.localdomain.err  mysql_upgrade_info  test
ibdata1   ib_logfile1  mysql                      performance_schema
[root&#64;localhost data]# </span><span style="color:#0000ff;">rm</span> -<span styl
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP