MySQL更改資料庫資料儲存目錄
SQL/">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]# 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 ]
參考資料
- ofollow,noindex"> http://database.ctocio.com.cn/tips/449/7566949.shtml
- Linux/2012-12/75647.htm" target="_blank" rel="nofollow,noindex"> www.linuxidc.com/Linux/2012-12/75647.htm
- http://blog.csdn.net/hellyhe/article/details/8309470