天天看點

MySQL 更改資料庫資料存儲目錄

MySQL資料庫預設的資料庫檔案位于 /var/lib/mysql 下,有時候由于存儲規劃等原因,需要更改 MySQL 資料庫的資料存儲目錄。

下文總結整理了實踐過程的操作步驟。

1 确認MySQL資料庫存儲目錄

[root@DB-Server tmp]# mysqladmin -u root -p variables | grep datadir
Enter password: 
| datadir | /var/lib/mysql/      

2 關閉MySQL服務

在更改MySQL的資料目錄前,必須關閉MySQL服務。

方式1:

[root@DB-Server ~]# service mysql status
MySQL running (9411)[ OK ]
[root@DB-Server ~]# service mysql stop
Shutting down MySQL..[ OK ]
[root@DB-Server ~]#      

方式2:

[root@DB-Server ~]# /etc/rc.d/init.d/mysql status
MySQL running (8900)[ OK ]
[root@DB-Server ~]# /etc/rc.d/init.d/mysql stop
Shutting down MySQL..[ OK ]
[root@DB-Server ~]#      

3 建立新的資料庫存儲目錄

[root@DB-Server ~]# cd /u01
[root@DB-Server u01]# mkdir mysqldata      

4 移動MySQL資料目錄到新位置

[root@DB-Server ~]# mv /var/lib/mysql /u01/mysqldata/      

5 修改配置檔案my.cnf

并不是所有版本都包含有 my.cnf 這個配置檔案,在 MySQL 5.5 版本,我就找不到 my.cnf 這個配置檔案, 而有些 MySQL 版本該檔案位于 /usr/my.cnf,如果 /etc/ 目錄下沒有 my.cnf 配置檔案,請到 /usr/share/mysql/ 下找到 *.cnf 檔案,拷貝其中一個到 /etc/ 并改名為 my.cnf 中。

指令如下:

[root@DB-Server mysql]#cd ~
[root@DB-Server mysql]# find / -name "my.cnf"
[root@DB-Server mysql]# cd /usr/share/mysql
[root@DB-Server mysql]# ls *.cnf
[root@DB-Server mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf      

編輯 /etc/my.cnf 檔案,修改參數 socket

 MySQL 5.5 版本

# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket          = /u01/mysqldata/mysql/mysql.sock
 
# Here follows entries for some specific programs
 
# The MySQL server
[mysqld]
port            = 3306
socket          = /u01/mysqldata/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M      

6 修改啟動腳本 /etc/init.d/mysql

将參數 datadir 修改為 datadir=/u01/mysqldata/mysql/

7 啟動MySQL服務并驗證MySQL資料庫路徑

[root@DB-Server ~]# service mysql start
Starting MySQL..[  OK  ]
[root@DB-Server ~]# mysqladmin -u root -p variables | grep datadir
Enter password: 
| datadir        | /u01/mysqldata/mysql/      

我的疑問

1 在修改資料庫的存儲目錄前,/var/lib/mysql/ 目錄下根本沒有 mysql.sock 檔案,安裝上面配置後,就會生成 mysql.sock 檔案。

關于 mysql.sock 檔案,搜尋了一下資料:mysql.sock 是用于 socket 連接配接的檔案。也就是隻有你的守護程序啟動起來這個檔案才存在。

但是你的 mysql 程式(這個程式是用戶端,伺服器端是 mysqld)可以選擇是否使用 mysql.sock 檔案來連接配接(因為這個方法隻适合在 Unix 主機上面連接配接本地的 mysqld),對于非本地的任何類型的主機。

那麼這個檔案是否一定需要的呢? 這個需要進一步了解清楚。

2 我在網上看有些網友總結的修改 MySQL 資料路徑,有些需要給建立的目錄的權限做一些處理,而有些有不用對目錄權限進行授權,我沒有處理,也沒有什麼問題。到底要不要對新的資料庫目錄授權呢?

3 我在 MySQL_5.6.20 這個版本測試時,不修改 my.cnf,隻修改啟動腳本 /etc/init.d/mysql,也完全沒有啥問題。也沒有 myssql.sock 檔案生成。

4 注意如果沒有禁用 SElinux,修改MySQL的資料路徑後啟動MySQL服務會遇到一些錯誤。關于這個的解釋是背景服務都需要有對相應目錄的對應權限,而 mysql 的預設路徑 /var/lib/mysql 已經添加了相應的政策,修改路徑後由于沒有相應的政策,導緻背景程序讀取檔案被 SElinux 阻止,進而出現權限錯誤。 是以要麼關閉 SElinux 或修改檔案安全上下文。

[root@DB-Server mysql]# /etc/init.d/mysql start
Starting MySQL....The server quit without updating PID file (/u01/mysqldata/mysql//DB-Server.localdomain.pid).[FAILED]
[root@DB-Server mysql]# 
[root@DB-Server mysql]# chcon -R -t mysqld_db_t /u01/mysqldata/mysql/
[root@DB-Server mysql]# /etc/init.d/mysql start
Starting MySQL.[ OK ]      

參考資料

  • http://database.ctocio.com.cn/tips/449/7566949.shtml
  • www.linuxidc.com/Linux/2012-12/75647.htm
  • http://blog.csdn.net/hellyhe/article/details/8309470