您的位置:首页 > 数据库 > MySQL

更改mysql数据库目录

2015-12-28 13:25 465 查看
转载自:/article/3583787.html

版权声明:这是自封为沉默王二的挨踢工作者,用文字打造的一个高品质的博客栏目。让你拥有一段快乐的阅读时光,不再是奢侈的梦想!

目录(?)[+]

更改mysql数据库目录,是一个辛酸历程,其实我想交代更多的内容,只是限于文章的实用性,我决定去繁就简。

今天早上,客户告诉我期货交易平台登陆不上去,这肯定是项目出了bug。


一、show processlist

<code class="language-sql hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">show</span> processlist;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>




发现部分sql等待表级锁(且这么叫吧,懒得找翻译软件了),我首先想到的是“某几张表是myisam引擎,容易发生表级锁,之前就有类似的问题发生,先kill掉sql进程,然后将表引擎由myisam改为innodb。”

当然了,我按照这个想法准备要执行表引擎的更换了,但出现了突发状况。


二、got error 28 from storage engine

如题所示,当我准备打开表查看数据的时候,出现了“got error 28 from storage engine”错误消息提示,我的翻译是“存储引擎出现了28错误”。

当然我百度了,找到这样一篇mysql的“Got error 28 from storage engine”错误,说实话,这篇文章没有给我什么鸟的帮助,但现在回想起来,确实是我自己大意了,虽然说清空“/tmp”目录的做法不值得推荐,但至少应该去清空一些磁盘,把数据库备份一下。

当然了,我想到去备份数据,使用了“mysqldump”命令尝试了一次数据备份,但是遗憾的是,报了一个错误,提示我无法保存。这是显而易见的,因为磁盘本身就不足了,没有清空磁盘,搞毛线的备份。但这是事后诸葛亮的想法,当时的我显然没有针对性的对数据进行备份!再次奉劝各位,数据库出问题时,即使有自动化的数据备份,但尽量在修正问题之前再做一次实时的备份!

三、df -h

没有备份成功,我就转向了另外一个方向,查看数据磁盘空间。
<code class="language-shell hljs coffeescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[root<span class="hljs-property" style="box-sizing: border-box;">@iZ23gsv94suZ</span> run]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># df -h</span>
Filesystem      Size  Used Avail Use% Mounted <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">on</span>
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/dev/xvda1       20G  19G   16M  100% /</span>
tmpfs           <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.9</span>G     <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.9</span>G   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>% /dev/shm
/dev/xvdb1       <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">99</span>G  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>M   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">99</span>G  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>% /mnt</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>


看到use等于“100%”,我已经确定问题出在哪里了,但是我在这里继续犯了一个错误。

“莫非是阿里云的磁盘没有挂载成功,就是/dev/xvdb1对应的这个100G磁盘没有挂载成功,否则怎么把20G的这个盘给沾满了呢?”

后知后觉的我现在恍然大悟,对于windows操作系统,C盘满了,怎么也不会把数据移动到D盘来,然而当时我怎么都在怀疑阿里云的磁盘没有挂载成功!这就是一个天大的错误!

四、阿里云磁盘挂载

由于上面这一步认为阿里云磁盘没有挂载成功,所以当时糊涂的我一心要重新挂载磁盘,于是我又在Linux 系统挂载数据盘这个泥潭里深陷不已,久久不能自拔,由于硬盘确实已经挂在成功,所以我再怎么费尽心思也只能得到“磁盘已挂载成功”的答案。但是,这里还是不得不说,一个人的精力实在有限,我几乎已经是一个全栈工程师,自己对自己在意的标签是“打杂工,偏java”,这就让我不得不感慨万千。

由于是个创业团队,我又是一个负责人,所以我要做的工作有:

java编程
web编程
Linux编程(shell脚本、项目部署)
C#客户端协调(调查bug、业务探讨、相关代码审核)
安卓编程(目前处于学习阶段)
项目管理(麻雀虽小肝胆俱全,我必须要让每个人开心快乐的工作,效率最大化)
财务、行政(发发工资、请大家聚餐活动、游玩、工作环境布置、浇花、打扫卫生)
需求分析(客户沟通、需求调查、业务建模)
个人能力提升(编程能力、为人处事能力)

等等等等,只要能想得到的,没有不做的,实在是无法集中重心专业负责哪一块,就导致哪一块都不精通,这也是导致本次问题出现一个关键因素。

所以,如果朋友们有好的解决方案,请不吝赐教,我希望通过互联网认识更多朋友,在未来的创业道路上一路前行,互惠互利。

五、mysql数据库目录更改

既然阿里云的磁盘已经挂在,那么接下来的工作就是把mysql的默认数据目录迁移到100G的盘上,而不是系统盘!

这个过程现在说起来很简单,但是在执行的过程中,却是千辛万苦!

linux 更改mysql的数据库目录,这篇文章提供了相应的办法,按理说,按照里面提到的方法执行下去就OK,但这里,我不得不强调,项目千差万别,每个人解决问题的办法也不尽相同,所以,动数据库之前,千万要先备份!

显然,我按照这篇文章所说,是没有把目录迁移成功,所以你也需要这样,请千万注意!

①、mv

Linux的mv命令很强大,但风险系数也很高!

强烈建议使用cp命令,而非mv命令,除非你一万个确定方案可行。


②、chown -R mysql:mysql /home/data/mysql/

<code class="language-shell hljs haskell has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-title" style="box-sizing: border-box;">chown</span> -<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">R</span> mysql:mysql /home/<span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>/mysql/  # 改变数据库的归属为mysql</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>


这个语句慎用,反正坑哭了我!

③、 /mnt

前面曾提到

/dev/xvdb1 99G 255M 99G 1% /mnt


不知道,你是否注意,对于我一个Linux的雏鸟来说,显然没有意识到这个100G的磁盘,其路径是“/mnt”,也就是说,mysql的数据目录需要重置到该“/mnt”目录下。

④、目录迁移


1、cp -afpr /var/lib/mysql/ /mnt/


使用cp命令,将mysql的数据默认目录,移动到新的挂载盘/mnt下面
<code class="language-shell hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[root@iZ23gsv94suZ mysql]<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># pwd</span>
/mnt/mysql
[root@iZ23gsv94suZ mysql]<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
auto<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.cnf</span>  ib_logfile0  ibtmp1            iZ23gsv94suZ<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.pid</span>  mysql-bin<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.000001</span>  mysql<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sock</span>          RPM_UPGRADE_HISTORY      
ibdata1   ib_logfile1  iZ23gsv94suZ<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.err</span>  mysql             mysql-bin<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.index</span>   performance_schema  RPM_UPGRADE_MARKER-LAST</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

2、vim /etc/my.cnf
<code class="language-shell hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    [mysql]
    default-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">character</span>-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">set</span>=utf8
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">socket</span>          = /mnt/mysql/mysql.sock
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#</span>
    [mysqld]
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># The TCP/IP Port the MySQL Server will listen on</span>
    port=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3306</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">socket</span>          = /mnt/mysql/mysql.sock</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>


把mysql配置文件的socket目录更改到/mnt下对应的路径

3、vim /etc/init.d/mysql

<code class="language-shell hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span>
   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># datadir=/var/lib/mysql</span>
datadir=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/mnt/mysql</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>


将该文件中的datadir修改为对应的/mnt目录

4、ln -s /mnt/mysql/mysql.sock /var/lib/mysql/mysql.sock


使用ln命令,指定一条链接给mysql.sock,具体的作用,我不知,你可百度。

按照以上四步,就可以转移mysql的数据库目录了,但是,我的过程远比这个艰辛。

搞来搞去,我的mysql是搞崩溃了,没有办法,需要重新安装。这里,你可以参照centOS下安装mysql5.7升级mysql到5.7。然后,数据恢复你可以使用source命令,如果涉及到二进制日志恢复,你可以参照mysql之备份和导入数据(包括二进制日志)

但最重要的是,操作数据库之前,想尽一切办法先备份数据库!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: