1. 程式人生 > >CentOS 7 下 修改Mysql 5.7datadir的位置,無法啟動的問題解決

CentOS 7 下 修改Mysql 5.7datadir的位置,無法啟動的問題解決

CentOS 7 下MySQL 預設安裝在/var/lib/mysql目錄下,但是隨著儲存資料的增多,所在的硬碟空間很快被佔滿,導致無法進行插入、建表等操作。

這時需要將mysql資料儲存資料夾移植到別的資料夾下,網上有很多教程,如http://www.cnblogs.com/5201351/p/4912614.html,是一篇很好的教程,但是在實際移植過程發現,只有Mysql的資料資料夾/var/lib/mysql被改為其他目錄,無論如何修改/etc/my.cnf配置中的datadir和socket引數,都無法移動mysqld。

使用如下命令,啟東mysql

service mysqld  start

會出現如下錯誤:
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

如果使用如下命令檢視

service mysqld  status

會得到如下錯誤資訊

Redirecting to /bin/systemctl status  mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Fri 2017-10-20 09:03:33 CST; 1min 11s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 5130 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=1/FAILURE)
  Process: 5108 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 15110 (code=exited, status=0/SUCCESS)


Oct 20 09:03:33 bigdata-6 systemd[1]: Failed to start MySQL Server.
Oct 20 09:03:33 bigdata-6 systemd[1]: Unit mysqld.service entered failed state.
Oct 20 09:03:33 bigdata-6 systemd[1]: mysqld.service failed.
Oct 20 09:03:33 bigdata-6 systemd[1]: mysqld.service holdoff time over, scheduling restart.
Oct 20 09:03:33 bigdata-6 systemd[1]: start request repeated too quickly for mysqld.service
Oct 20 09:03:33 bigdata-6 systemd[1]: Failed to start MySQL Server.
Oct 20 09:03:33 bigdata-6 systemd[1]: Unit mysqld.service entered failed state.
Oct 20 09:03:33 bigdata-6 systemd[1]: mysqld.service failed.

一開始總以為是配置的問題,後來仔細檢視一下msyql的日誌檔案: /var/log/mysqld.log

出現了幾個Warning,說檔案無法建立

120609 11:31:31 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
120609 11:35:12 mysqld_safe Starting mysqld daemon with databases from /mnt/hgfs/mysql_data
120609 11:35:13 [Warning] Can't create test file /home/mysql_data/data.lower-test
120609 11:35:13 [Warning] Can't create test file /home/mysql_data/data.lower-test
/usr/libexec/mysqld: Can't change dir to '/home/mysql_data/' (Errcode: 13)
120609 11:35:13 [ERROR] Aborting

120609 11:35:13 [Note] /usr/libexec/mysqld: Shutdown complete

120609 11:35:13 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended


解決方案:

關閉mysql: service mysqld stop

1、編輯/etc/selinux/config 檔案,將其中的SELINUX禁用,SELINUX=disabled,重新啟動系統。

2、移動/var/lib/mysql到你需要的資料夾,mv /varlib/mysql  /home/mysqldata

3、在/var/lib下建立一個連結ln -s /home/mysqldata mysql (這種方法可以不用修改my.cnf配置)

4、啟動mysql: service mysqld start

記錄一下,